0

Goal: Given string "CAR080 CAR085 CAR087" I need to iterate through all three cars and perform copying of files from the carname folder.

Need to copy files from individual car folders by looping through it. Code:

 @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.
dragon
  • 11
  • 2
  • 1
    Just use a normal `FOR` command. You don't need to use `FOR /F` in this instance. You also need to use delayed expansion because you are inside a code block. – Squashman Oct 12 '16 at 12:47
  • kindly explain in detail i am new to batch script. – dragon Oct 12 '16 at 13:05

1 Answers1

1

Just simplify. This is equivalent to your code but without variables being set inside the for block, so you don't need delayed expansion (read here) to retrieve the value from the changed variables

@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%"
    )
Community
  • 1
  • 1
MC ND
  • 69,615
  • 8
  • 84
  • 126