windows批处理文件:对于L循环变量不跟踪

时间:2017-07-21 15:31:11

标签: batch-file cmd batch-processing

我需要遍历扩展名为001 = 159的文件。我创建了一个for / L循环来遍历此目录并设置变量。问题是它不能设置变量,因此程序不会起作用。任何帮助表示赞赏。

ECHO ON
SET LOCAL ENABLEEXTENSIONS
SET LOCAL ENABLEDELAYEDEXPANSION

chdir /d D:
SET /P practice="Enter Practice 3 letters "
SET String=%Practice%
CALL :UpCase String
SET String
echo String
pause
SET /P FileNumber="Enter Amount of files "


SET /P year="Enter Year "
SET /P typeSet="Enter 1-NoConvert 2-NeedConvert "
if %typeset% equ 1 set Type=NoConvert
if %typeset% equ 2 set Type=NeedConvert

set /P WaveType="Enter Wave Number "
set wave=Wave%waveType%Documents
echo FileNumber %FileNumber%
pause
FOR /L %%i IN (1,1,%FileNumber%) DO (
echo %%i
set Ext=_00%%i

pause

set src_folder=D:\
set index=Index_%String%_%year%_%type%_%Ext%.idx

echo indexFile %index%
echo %wave%

if not exist D:\ChartMaxx\%wave%\%String%_%year%_%type%_%Ext% mkdir 
D:\ChartMaxx\%wave%\%String%_%year%_%Type%_%Ext%
set dst_folder=D:\ChartMaxx\%wave%\%String%_%year%_%Type%_%Ext%


) 
@REM End For L Loop

:UpCase
:: Subroutine to convert a variable VALUE to all UPPER CASE.
:: The argument for this subroutine is the variable NAME.
FOR %%i IN ("a=A" "b=B" "c=C" "d=D" "e=E" "f=F" "g=G" "h=H" "i=I" "j=J" 
"k=K" "l=L" "m=M" "n=N" "o=O" "p=P" "q=Q" "r=R" "s=S" "t=T" "u=U" "v=V" 
"w=W" "x=X" "y=Y" "z=Z") DO CALL SET "%1=%%%1:%%~i%%"
GOTO:EOF

2 个答案:

答案 0 :(得分:0)

FOR /L %%i IN (1,1,%FileNumber%) DO (
    SET Ext=00%%i
    pause
    set index=Index_%String%_%year%_%type%_!Ext:~-3!.idx
)

结果:set index=Index_GLP_2014_NeedConvert_!Ext:~-3!.idx

_!Ext:~-3!代替001显示。

答案 1 :(得分:0)

了解这对你有何用处,(我已经删除了一些膨胀)

@ECHO OFF
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION

SET /P "String=Enter Practice 3 letters "
CALL :UpCase String %String%

SET /P "FileNumber=Enter Amount of files "

SET /P "year=Enter Year "

CHOICE /C 12 /M "1. NoConvert, 2. NeedConvert"
IF ERRORLEVEL 2 (SET "Type=NeedConvert") ELSE SET "Type=NoConvert"

SET /P "WaveType=Enter Wave Number "
SET "wave=Wave%WaveType%Documents"

FOR /L %%i IN (1,1,%FileNumber%) DO (
    SET "Ext=_00%%i"
    SET "index=Index_%String%_%year%_%Type%_!Ext!.idx"
    SET "dst_folder=D:\ChartMaxx\%wave%\%String%_%year%_%Type%_!Ext!"
    IF NOT EXIST "!dst_folder!\" MD "!dst_folder!"
) 

EXIT/B

:UpCase
FOR %%i IN ("a=A" "b=B" "c=C" "d=D" "e=E" "f=F" "g=G" "h=H" "i=I" "j=J" "k=K"
    "l=L" "m=M" "n=N" "o=O" "p=P" "q=Q" "r=R" "s=S" "t=T" "u=U" "v=V" "w=W"
    "x=X" "y=Y" "z=Z") DO SET "%1=!%1:%%~i!"

您唯一需要检查/更改的是,您是否需要_变量前面的其他 Ext ;也许SET "Ext=00%%i"会解决这个问题。