无法在Windows批处理脚本中迭代for循环

时间:2016-10-12 12:45:24

标签: batch-file

目标:给出字符串" CAR080 CAR085 CAR087"我需要遍历所有三辆车,并从carname文件夹中复制文件。

需要通过循环来复制各个汽车文件夹中的文件。     代码:

 @echo off
    set basesource=xyz\

    set year=%date:~10,4%
    set destination=C:\ARWISdata\year%year%
    set tempDir=DATAARWIS\DATARP_%year%

    set s= CAR080 CAR085 CAR087
    set t=%s%
    echo t:%t%
    :loop
    for /f "tokens=1*" %%a in ("%t%") do (
    set temp1=%%a
    set SLASH=\
    echo basesource:%basesource%
    echo temp1:%temp1%
    set source=%basesource%%temp1%%SLASH%%tempDir%
    echo source:%source%

    IF EXIST %source% (echo source exists)
    echo destination: %destination%

    IF EXIST %destination% (echo destination exists) ELSE (mkdir C:\ARWISdata\year%year%)

    for /f %%a in ('xcopy /S /E /Y /D "%source%" "%destination%" ') do (
        echo.Copying file '%%a'
    )
    set t=%%b

       )
    if defined t goto :loop

but it is not giving me exact solution. As it needs to take first CAR080 and perform copy operation then in next cycle it should take CAR085 and perform copying and then finally CAR087.

Need the code urgently.

1 个答案:

答案 0 :(得分:1)

简化。这相当于您的代码,但没有在for块中设置变量,因此您不需要延迟扩展(读取here)以从已更改的变量中检索值

@echo off
    setlocal enableextensions disabledelayedexpansion

    set "baseSource=xyz"

    set "year=%date:~10,4%"
    set "tempDir=DATAARWIS\DATARP_%year%"

    set "destination=C:\ARWISdata\year%year%"
    2>nul mkdir "%destination%"

    for %%a in (CAR080 CAR085 CAR087) do (
        xcopy "%baseSource%\%%a\%tempDir%" "%destination%"
    )