简体   繁体   English

CMD如何将参数传递给脚本并保存到参数之一的文件名

[英]CMD how to pass parameters to script and save to file name of one of the parameters

I want to pass the following parameters to a script: 我想将以下参数传递给脚本:

myscript.cmd "Foo" "Bar" "File"

Where the 3rd parameter should be with added extension .log 其中第三个参数应带有扩展名.log

In this example it should be File.log 在此示例中,它应该是File.log

SET logFile = "%~3" + .log
echo logFileName is "%logFile%"

echo %date% %time% got parameters "%~1" "%~2" >> "%logFile%"

Should be something like the script above, but not being able to make it work 应该类似于上面的脚本,但无法使其正常工作

Three problems, quotes (included in the value of the variable), spaces (you have included spaces in the name of the variable) and concatenation (there is no concatenation operator in batch files) 三个问题,引号(包含在变量的值中),空格(变量名称中包含空格)和串联(批处理文件中没有串联运算符)

You have defined a variable named logFile_ , with an aditional space (represented by the underscore) in its name, and the assigned value is the literal _"File"_+_.log (as before, underscores represent spaces) 您已经定义了一个名为logFile_的变量,其名称中带有一个logFile_空格(由下划线表示),并且分配的值是文字_"File"_+_.log (如前所述,下划线表示空格)

The line 线

SET logFile = "%~3" + .log
           ^ ^^   ^^^^  Unneeded/problematic characters

Should be 应该

SET "logFile=%~3.log"

No aditional spaces and the quotes are not included in the value, they just delimit the assignment to prevent problems with special characters or aditional spaces at the end of the line 没有附加空格,并且引号不包含在值中,它们只是对赋值进行了界定,以防止行尾出现特殊字符或附加空格的问题

Remove the spaces around the = and remove extraneous quotes. 删除=周围的空格并删除多余的引号。

SET logFile=%~3.log
echo logFileName is "%logFile%"

echo %date% %time% got parameters "%~1" "%~2" >> "%logFile%"

That works for me. 这对我行得通。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM