使用Double Delayed Expansion设置批处理文件中的变量

时间:2019-05-22 11:15:59

标签: batch-file batch-rename

因此,我尝试使用双延迟扩展来重命名目录中的文件。我遇到的问题是使用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

1 个答案:

答案 0 :(得分:0)

@jeb有答案,所以以为我会以正式答案结束该问题。

将呼叫回显newNewValue = %newValue%更改为echo newNewValue = !newValue!