批量变量不递增!

时间:2011-05-23 13:04:51

标签: batch-file dos

SET /a _count = 1

set /p drive=Please type drive name[C/D/E/F/..]?: 
echo Creating Directory %drive%:\AKI_Data_Feeds
mkdir %drive%:\AKI_Data_Feeds

for /f %%a in (companyList.txt) do (
echo Creating Directory %drive%:\AKI_Data_Feeds\%%a
mkdir %drive%:\AKI_Data_Feeds\%%a

for /f %%b in (branch1.txt) do (
echo Creating Directory %drive%:\AKI_Data_Feeds\%%a\%%b
mkdir %drive%:\AKI_Data_Feeds\%%a\%%b


if "%_count%"=="1" (

for /f %%c in (branch1_1.txt) do (
echo Creating Directory %drive%:\AKI_Data_Feeds\%%a\%%b\%%c
mkdir %drive%:\AKI_Data_Feeds\%%a\%%b\%%c

for /f %%e in (branch2.txt) do (
echo Creating Directory %drive%:\AKI_Data_Feeds\%%a\%%b\%%c\%%e
mkdir %drive%:\AKI_Data_Feeds\%%a\%%b\%%c\%%e
)

)

SET /a _count +=1
echo %_count%

)
if "%_count%"=="2" (
for /f %%d in (branch1_2.txt) do (
echo Creating Directory %drive%:\AKI_Data_Feeds\%%a\%%b\%%d
mkdir %drive%:\AKI_Data_Feeds\%%a\%%b\%%d

for /f %%e in (branch2.txt) do (
echo Creating Directory %drive%:\AKI_Data_Feeds\%%a\%%b\%%c\%%e
mkdir %drive%:\AKI_Data_Feeds\%%a\%%b\%%c\%%e
)

)

SET /a _count +=1

)

)
)

echo Successfully created.
pause  

_count没有增加

1 个答案:

答案 0 :(得分:6)

你需要延迟扩展来解决这个问题 由于所有百分比变量在解析块时都会扩展,因此它们的值似乎保持不变。

延迟扩展后,变量会在执行时扩展。

setlocal EnableDelayedExpansion
set count=0
(
   set /a count+=1
   echo %count% fails
   echo !count! works
)