2015-11-01 63 views
1

我有兩個問題與我的批處理文件,我認爲它們是由於EnableDelayedExpansion使用EnableDelayedExpansion時變量的問題

我基於this post from SO我的腳本。

我需要EnableDelayedExpansion作爲我腳本的另一部分,所以我需要保留它。

這是我的腳本:

@echo off 
set myPath=Subfolder1 
set folderList=input.txt 
set originalPath=%~dp0 

cd %myPath% 
setlocal EnableDelayedExpansion 

:process 

for /F "tokens=*" %%S in (%~dp0\%folderList%) do (
    echo Folder %%S 
    REM echo Folder %%S prints: Folder folderName 
    set testPath=C:\BatchTests\%%S\ 
    echo test path: %testPath% 
    REM echo test path: %testPath% prints: test path: 
) 
echo %originalPath% 
REM echo %originalPath% prints: C:\BatchTests\ 
cd %originalPath% 
pause 

testPath似乎總是空的,而行cd %originalPath%

我在做什麼錯?什麼是正確的方式使用/設置testPath

The second issue I am having is different, so I opened a separate question here.

+5

用'!testPath!'試試吧! – jeb

+0

當使用'pushd%myPath%'臨時修改它並且'popd'恢復原來的目錄時,您不需要將工作目錄存儲在變量中... – aschipfl

+0

@ jeb - 那有效!那是另一種使用變量的方法嗎? – Dzyann

回答

1

要在批次中使用延遲擴展,您需要零件。

首先,您必須使用setlocal EnableDelayedExpansion啓用它。
然後您可以用感嘆號而不是百分號來展開任何變量。

setlocal EnableDelayedExpansion 
set var=origin 
(
    set var=New Value 
    echo Percent: %var% 
    echo delayed: !var! 
) 

輸出是

百分比:原點
延遲:新值時命令或塊被解析

百分比擴展進行評價。
執行命令時評估延遲擴展。