windows 批处理文件:将带有空格的参数传递给函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6685229/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 17:25:14  来源:igfitidea点击:

batch file: pass parameter with white spaces to function

windowsbatch-filespaces

提问by Matthias Pospiech

I am using a batch file for backups. I pass the options to a function which calls the packaging executable. This works unless the parameters contain whitespaces. This is the relevant code:

我正在使用批处理文件进行备份。我将选项传递给调用打包可执行文件的函数。除非参数包含空格,否则这有效。这是相关代码:

SET TARGET="%SAVEDIR%\XP.User.Documents.rar"
SET FILES="%DIRUSER%\Eigene Dateien\*"      
SET EXLUCDE="%DIRUSER%\Documents\CDs" 
call:funcBackup %TARGET% %FILES% %EXLUCDE%

:funcBackup
    SET TARGET=%~1
    SET FILES=%~2
    SET EXCLUDE=%~3     
    echo."%PACKER% a -r -x"%EXCLUDE%" "%TARGET%" "%FILES%""
    ::call %PACKER% a -r -x"%EXCLUDE%" "%TARGET%" "%FILES%"
goto:eof

On XP(german version) %DIRUSER% expands to "Dokumente und Einstellungen"

在 XP(德语版)上 %DIRUSER% 扩展为“Dokumente und Einstellungen”

In that case TARGET is correct, but FILES == "Dokumente" and EXCLUDE == "und", which means that the script fails because of the whitespaces in %DIRUSER%.

在这种情况下 TARGET 是正确的,但 FILES == "Dokumente" 和 EXCLUDE == "und",这意味着脚本由于 %DIRUSER% 中的空格而失败。

How can I fix this?

我怎样才能解决这个问题?

回答by jeb

The problem seems to be your style of assigning the variables.
I suppose you set the DIRUSER variable like the other ones

问题似乎出在您分配变量的风格上。
我想你像其他人一样设置 DIRUSER 变量

set DIRUSER="Dokumente und Einstellungen"

But the content of DIRUSER is then "Dokumente und Einstellungen", so the quotes are a part of the content.

但是 DIRUSER 的内容是 then "Dokumente und Einstellungen",所以引号是内容的一部分。

But then SET FILES="%DIRUSER%\Eigene Dateien\*"expands to SET FILES=""Dokumente und Einstellungen"\Eigene Dateien\*".

但随后SET FILES="%DIRUSER%\Eigene Dateien\*"扩展到SET FILES=""Dokumente und Einstellungen"\Eigene Dateien\*".

You could use the extended style of set.
set "var=content"This sets the content of varto contentwithout any quotes and also all appended spaces behind the last quote are ignored.

您可以使用set.
set "var=content"这会将varto的内容设置为content不带任何引号,并且最后一个引号后面的所有附加空格都将被忽略。

So your code would be

所以你的代码是

set "SAVEDIR=D:\backup"
set "diruser=Dokumente und Einstellungen"
SET "TARGET=%SAVEDIR%\XP.User.Documents.rar"
SET "FILES=%DIRUSER%\Eigene Dateien\*"      
SET "EXLUCDE=%DIRUSER%\Documents\CDs" 
call:funcBackup "%TARGET%" "%FILES%" "%EXLUCDE%"
goto :eof

:funcBackup
    SET "TARGET=%~1"
    SET "FILES=%~2"
    SET "EXCLUDE=%~3"
    echo."%PACKER% a -r -x"%EXCLUDE%" "%TARGET%" "%FILES%"
    ::call %PACKER% a
goto :eof   

回答by Costa

Switching your arg calls in the function from %~1, %~2and %~3to %~f1, %~f2and %~f3respectively should do the trick. It'll pass the fully qualified path name for each arg.

将函数中的 arg 调用分别从%~1%~2%~3切换到%~f1%~f2%~f3应该可以解决问题。它将为每个 arg 传递完全限定的路径名​​。

More info: http://www.windowsitpro.com/article/server-management/how-do-i-pass-parameters-to-a-batch-file-

更多信息:http: //www.windowsitpro.com/article/server-management/how-do-i-pass-parameters-to-a-batch-file-