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
Batch file for循环中的变量展开_Batch File_Variables_For Loop_Expansion - Fatal编程技术网

Batch file for循环中的变量展开

Batch file for循环中的变量展开,batch-file,variables,for-loop,expansion,Batch File,Variables,For Loop,Expansion,我知道这是一个常见的问题,但我无法解读其他问题的答案和建议,以使我的具体脚本工作 @echo on setlocal EnableDelayedExpansion set directory=%1 set file_list=%2 for /f "tokens=*" %%i in (%file_list%) DO ( set newName=!%%i:~0,5! move "%directory%\%%i" "%directory%\!%newName%!s.jpg" ) endlocal

我知道这是一个常见的问题,但我无法解读其他问题的答案和建议,以使我的具体脚本工作

@echo on
setlocal EnableDelayedExpansion

set directory=%1
set file_list=%2

for /f "tokens=*" %%i in (%file_list%) DO (
set newName=!%%i:~0,5!
move "%directory%\%%i" "%directory%\!%newName%!s.jpg" )

endlocal
这是在echo打开的情况下给我的输出

set newName=!image.png:~0,5!  
move "\\server\path\to\image.png" "\\server\path\to\!!s.jpg"
很明显,当我需要newName时,它是空的,所以我甚至不能在尝试将它减少到5个字符时开始解决最可能出现的语法问题

谢谢

set "newName=%%i"
set "newName=!Newname:~0,5!"
不能像
%%i
那样对
元变量进行子串。您需要将其分配给一个标准环境变量,然后使用指定的方法(delayedexpansion模式下的(
!var..!
),因为在代码块中需要变量的更改值)


引号只是对所涉及的字符串进行了分隔,以确保不包括行中任何不可见的空格。

Hi Adam,您是否看到Internet或StackOverFlow上的某个示例,其中指出可以为变量指定子字符串?只是想确保没有任何关于StackOverFlow的错误答案,我们应该注意。