批处理文件没有正确添加空格的文件夹结构?

时间:2015-10-06 20:39:32

标签: powershell batch-file

批处理文件运行PowerShell脚本。问题是路径位置有空格。批处理文件只能看到空格前的第一个单词。

SET targetDir="\\server.com\xxx\First Folder with spaces\Second folder with spaces"
SET archiveDir="\\server.com\xxx\First Folder with spaces\Second folder with spaces\Archive"
SET siteID=ABC


REM Run the import Powershell script for files.
powershell.exe -File D:\localPath\powershell\PowerShellScript.ps1 -Path "%targetDir%" -Filter *.dlu -Site %siteID% -ArchiveDir "%archiveDir%"

因此,当我运行PowerShell脚本时,它会从批处理文件中获取参数

param
( 
[string] $Path = $(Throw "You must specify a directory path containing the files to import."),
[string] $Filter = $(Throw "You must provide a file pattern/filter (e.g. *.dlu) to be used for locating the files to import"),
[string] $Site = $(Throw "You must provide a site identifier for the import."),
[string] $ArchiveDir = $null # If not provided, the imported files will be deleted rather than archived to another location.
)

但是当我将路径输出到屏幕时,我得到:

Path= C:\First
Site= ABC
Archive= C:\First 

我做错了什么?

如果我使用硬编码运行.bat文件,那就完美了。

REM SET targetDir="\\server.com\xxx\First Folder with spaces\Second folder with spaces"
REM SET archiveDir="\\server.com\xxx\First Folder with spaces\Second folder with spaces\Archive"
REM SET siteID=ABC

REM Run the import Powershell script for files.
powershell.exe -File D:\localPath\powershell\PowerShellScript.ps1 -Path "\\server.com\xxx\First Folder with spaces\Second folder with spaces" -Filter *.dlu -Site ABC -ArchiveDir "\\server.com\xxx\First Folder with spaces\Second folder with spaces\Archive"

2 个答案:

答案 0 :(得分:1)

这是因为你正在复制引号。您可以在SET targetDir=语句中设置引号,然后在调用powershell.exe时再次执行引号。结果命令是:

powershell.exe -File D:\localPath\powershell\PowerShellScript.ps1 -Path ""\\server.com\xxx\First Folder with spaces\Second folder with spaces"" -Filter *.dlu -Site ABC -ArchiveDir ""\\server.com\xxx\First Folder with spaces\Second folder with spaces\Archive""

答案 1 :(得分:1)

@MobyDisk已经告诉了问题的根本原因是什么。

您应该更改批处理文件中的SET命令行,如下所示:

SET "targetDir=\\server.com\xxx\First Folder with spaces\Second folder with spaces"
SET "archiveDir=\\server.com\xxx\First Folder with spaces\Second folder with spaces\Archive"

请注意移动的开头引号"。此更改可避免引号成为变量值的一部分 (你也可以简单地删除所有引号,但是你可能会遇到一些特殊字符,例如^&。路径中的任何%符号仍然需要加倍不要无意中删除。)