-1

I am trying to write a script to copy log files from different sources and PC´s to 1 destination using robocopy.

for /f "tokens=*" %%a in (pc.txt) do (
set source0="\\%%a\D$\log"
set source1="\\%%a\C$\XX\log"
set source2="\\%%a\C$\XXX\log"
set dest="\\%COMPUTERNAME%\C$\XXX\logscript\%%a"
robocopy %source0% %dest% /create
robocopy %source1% %dest%
robocopy %source2% %dest%
)

The problem I am facing is robocopy does not get the variables the at the first run, at the 2nd run its working but when i add more PC´s to my pc.txt it only uses some pc´s names.

eschi1
  • 3
  • 4

1 Answers1

0

You need to use delayed expansion:

SETLOCAL EnableDelayedExpansion

for /f "tokens=*" %%a in (pc.txt) do (
    set source0="\\%%a\D$\log"
    set source1="\\%%a\C$\XX\log"
    set source2="\\%%a\C$\XXX\log"
    set dest="\\%COMPUTERNAME%\C$\XXX\logscript\%%a"

    REM Note that variables are surrounded with exclamation marks instead.
    robocopy !source0! !dest! /create
    robocopy !source1! !dest!
    robocopy !source2! !dest!
)

ENDLOCAL

Without this, the entire contents of the FOR loop are parsed only on the first pass (so your source and dest variables are not set yet). When delayed expansion is enabled, the variables are evaluated on each pass.

Jason Faulkner
  • 6,378
  • 2
  • 28
  • 33