循环通过SET变量并执行任务

时间:2016-09-07 18:09:38

标签: batch-file

我试图弄清楚如何遍历SET变量列表并执行一个简单的脚本。这是以批处理模式运行的。有关如何调整以下代码以使其循环遍历SET dir变量列表的任何提示吗?

SET YYYY=%date:~10,4%
ECHO Its %YYYY%

SET dir=dir1, dir2, dir3, dir4,...dirN
ECHO %dir%

:: Loop through each SET dir and perform the following.

:: Create a current year dir in remote directory
C:
cd "C:\my\dir\here\%dir%"
if not exist %YYYY% mkdir %YYYY%

:: Copy files from local to remote for each sub directory
E:
cd "E:\FTP\localdrive\%dir%"
XCOPY "E:\FTP\localdrive\%dir%" "C:\my\dir\here\%dir%\%YYYY%" /S /Y

在用户提出有价值的评论后,这似乎有效,但未能制作目录2到4。

 SET YYYY=%date:~10,4%
    ECHO Its %YYYY%

    SET dir=dir1, dir2, dir3, dir4

    FOR /F "tokens=1 delims=," %%a in (%dir%) do (
       if not exist %dir md E:\my\dir\here\%dir%
     )

修改到下面但仍然失败 - " %% a此时出乎意料"我认为是md E:\ my \ dir \ here \ %% a

SET YYYY=%date:~10,4%
ECHO Its %YYYY%

SET dir=dir1, dir2, dir3, dir4

FOR /F "tokens=1 delims=," %%a in (%dir%) do (
   md E:\my\dir\here\%%a
 )

2 个答案:

答案 0 :(得分:0)

 SET dir=dir1, dir2, dir3, dir4,...dirN

 for %%a in (%dir%) do (
    echo doing my stuff for %%a
 )

答案 1 :(得分:0)

仅供参考,替代 FOR 循环方法:

@Echo Off
(Set _yyyy=%DATE:~10,4%)
Echo= It's %_yyyy%

(Set _fold=dir1 dir2 dir3 dir4)
Echo= %_fold%

:: Loop through each Set _fold.
Call :Loop %_fold%

Pause
Exit/B

:Loop
:: Copy files from local to remote for each sub directory
If Not Exist "C:\my\dir\here\%1\%_yyyy%\" MD "C:\my\dir\here\%1\%_yyyy%"
RoboCopy "E:\FTP\localdrive\%1" "C:\my\dir\here\%1\%_yyyy%" /S
Shift
If '%1 Equ ' GoTo :EOF
GoTo :Loop