Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Batch file 检查.bat文件中字符串的开头 我尝试了以下代码: set target = "%~dp0\Backup1" set /s checktarget = %1 for %%I in (%*) do ( if "%checktarget:~0,3%" == "tg." ( set target = %checktarget:~4:3% ) else ( echo "%%I to %target%" ) )_Batch File - Fatal编程技术网

Batch file 检查.bat文件中字符串的开头 我尝试了以下代码: set target = "%~dp0\Backup1" set /s checktarget = %1 for %%I in (%*) do ( if "%checktarget:~0,3%" == "tg." ( set target = %checktarget:~4:3% ) else ( echo "%%I to %target%" ) )

Batch file 检查.bat文件中字符串的开头 我尝试了以下代码: set target = "%~dp0\Backup1" set /s checktarget = %1 for %%I in (%*) do ( if "%checktarget:~0,3%" == "tg." ( set target = %checktarget:~4:3% ) else ( echo "%%I to %target%" ) ),batch-file,Batch File,我的目标是检查第一个参数是否是备份的目标。 目的地应该带有一个“tg.”,后跟一个目录。 我使用以下命令运行脚本: test.bat tg.target B1 B2 B3 但当我运行这个时,输出是: "tg.target to " "B1 to " "B2 to " "B3 to " 我想要的是: "B1 to target" "B2 to target" "B3

我的目标是检查第一个参数是否是备份的目标。
目的地应该带有一个“tg.”,后跟一个目录。 我使用以下命令运行脚本:

test.bat tg.target B1 B2 B3
但当我运行这个时,输出是:

"tg.target to "
"B1 to "
"B2 to "
"B3 to "
我想要的是:

"B1 to target"
"B2 to target"
"B3 to target"
几个小时以来,我一直在想办法解决这个问题。 有人知道为什么它只把我作为目标吗?
它甚至不使用设置为默认值的目标。

您应该实现的第一件事是向批处理文件传递参数的更安全的方法。i、 e.
“测试蝙蝠”“tg目标”“B1”“B2”“B3”
。完成此操作后,您应该能够检索批处理文件中的每个参数,而不必使用双引号,如
%~0
->
test.bat
%~1
->
tg.target
%~2
->B1
%~3
->
B2
,以及
%~4
->B3
。然后,您应该学习如何更安全地使用值定义变量,
设置“VariableName=VariableValue”
。(例如,
设置“target=%~dp0Backup1”
注意,
%~dp0
已经有一个尾随的反斜杠)。然后你应该看看这里是否有输入错误,
%checktarget:~4:3%
,因为我认为在扩展变量时,
字符并不常见。还有,这里是否还有另一个,
set/s
,因为我不记得曾经看到过
/s
选项。一旦所有这些都完成,您的问题将是重复的,因为您有延迟的扩展问题。在
set target=%checktarget:~4:3%
中,equals符号周围应该没有空格。有一个名为target的变量,它的末尾有一个空格,您正在将它设置为一个以空格开头的值。可能不是您想要的。请尝试
help setlocal
help set
help if