使用双延迟扩展在批处理文件中设置变量
Use Double Delayed Expansion to SET a variable in a batch file
所以我尝试使用双重延迟扩展来重命名目录中的文件。我遇到的问题是使用 SET 命令进行双重延迟扩展。此方法与 ECHO 命令一起按预期工作。为什么这不适用于 SET?
下面是我的代码的摘录,重新编写后可以在独立脚本中使用。此脚本与我的主要代码具有相同的输出并使用相似的值。
@echo off
TITLE Bug In File Renaming
SETLOCAL enableextensions enabledelayedexpansion
:: The directory 'C:\Users\admin\Desktop\testFolder\Images'
:: contains 7 .png files named
:: Game-Prize-BANOFFEE_PIE.png
:: Game-Prize-BLUEBERRY_PIE.png
:: Game-Prize-BRANDY_BUTTER.png
:: Game-Prize-BUTTER_CREAM_CUP_CAKE.png
:: Game-Prize-CANDIED_NUTS.png
:: Game-Prize-CARAMELS.png
:: Game-Prize-CARROT_CAKE.png
SET renameFileDirectory=C:\Users\admin\Desktop\testFolder\Images
SET renameFilePrefix=Game-Prize-
SET renameFileType=.png
SET newValuesList[0]=VALUE1
SET newValuesList[1]=VALUE2
SET newValuesList[2]=VALUE3
SET newValuesList[3]=VALUE4
SET newValuesList[4]=VALUE5
SET newValuesList[5]=VALUE6
SET newValuesList[6]=VALUE7
echo. newValuesList & echo [%newValuesList[0]%, %newValuesList[1]%, %newValuesList[2]%, %newValuesList[3]%, %newValuesList[4]%, %newValuesList[5]%, %newValuesList[6]%]
SET /a i=0
FOR %%G IN (%renameFileDirectory%\%renameFilePrefix%*%renameFileType%) DO (
call echo newValue = %%newValuesList[!i!]%%
call SET newValue=%%newValuesList[!i!]%%
call echo newNewValue = %newValue%
SET /a i+=1
)
PAUSE
----OUTPUT----
newValuesList
[VALUE1, VALUE2, VALUE3, VALUE4, VALUE5, VALUE6, VALUE7]
newValue = VALUE1
newNewValue =
newValue = VALUE2
newNewValue =
newValue = VALUE3
newNewValue =
newValue = VALUE4
newNewValue =
newValue = VALUE5
newNewValue =
newValue = VALUE6
newNewValue =
newValue = VALUE7
newNewValue =
Press any key to continue . . .
我认为有问题的主线是call SET newValue=%%newValuesList[!i!]%%
这条线路可以吗?据我所知,这是双重延迟扩展,应该将变量 'newValue' 设置为与 'newValuesList[0]' 相同的值(当 i=0 时),应该是 VALUE1
@jeb 有答案,所以我想我会把它作为官方答案发布来结束这个问题。
将调用回显 newNewValue = %newValue%
更改为 echo newNewValue = !newValue!
所以我尝试使用双重延迟扩展来重命名目录中的文件。我遇到的问题是使用 SET 命令进行双重延迟扩展。此方法与 ECHO 命令一起按预期工作。为什么这不适用于 SET?
下面是我的代码的摘录,重新编写后可以在独立脚本中使用。此脚本与我的主要代码具有相同的输出并使用相似的值。
@echo off
TITLE Bug In File Renaming
SETLOCAL enableextensions enabledelayedexpansion
:: The directory 'C:\Users\admin\Desktop\testFolder\Images'
:: contains 7 .png files named
:: Game-Prize-BANOFFEE_PIE.png
:: Game-Prize-BLUEBERRY_PIE.png
:: Game-Prize-BRANDY_BUTTER.png
:: Game-Prize-BUTTER_CREAM_CUP_CAKE.png
:: Game-Prize-CANDIED_NUTS.png
:: Game-Prize-CARAMELS.png
:: Game-Prize-CARROT_CAKE.png
SET renameFileDirectory=C:\Users\admin\Desktop\testFolder\Images
SET renameFilePrefix=Game-Prize-
SET renameFileType=.png
SET newValuesList[0]=VALUE1
SET newValuesList[1]=VALUE2
SET newValuesList[2]=VALUE3
SET newValuesList[3]=VALUE4
SET newValuesList[4]=VALUE5
SET newValuesList[5]=VALUE6
SET newValuesList[6]=VALUE7
echo. newValuesList & echo [%newValuesList[0]%, %newValuesList[1]%, %newValuesList[2]%, %newValuesList[3]%, %newValuesList[4]%, %newValuesList[5]%, %newValuesList[6]%]
SET /a i=0
FOR %%G IN (%renameFileDirectory%\%renameFilePrefix%*%renameFileType%) DO (
call echo newValue = %%newValuesList[!i!]%%
call SET newValue=%%newValuesList[!i!]%%
call echo newNewValue = %newValue%
SET /a i+=1
)
PAUSE
----OUTPUT----
newValuesList
[VALUE1, VALUE2, VALUE3, VALUE4, VALUE5, VALUE6, VALUE7]
newValue = VALUE1
newNewValue =
newValue = VALUE2
newNewValue =
newValue = VALUE3
newNewValue =
newValue = VALUE4
newNewValue =
newValue = VALUE5
newNewValue =
newValue = VALUE6
newNewValue =
newValue = VALUE7
newNewValue =
Press any key to continue . . .
我认为有问题的主线是call SET newValue=%%newValuesList[!i!]%%
这条线路可以吗?据我所知,这是双重延迟扩展,应该将变量 'newValue' 设置为与 'newValuesList[0]' 相同的值(当 i=0 时),应该是 VALUE1
@jeb 有答案,所以我想我会把它作为官方答案发布来结束这个问题。
将调用回显 newNewValue = %newValue%
更改为 echo newNewValue = !newValue!