MSDOS For Loop

JoJoGabor
JoJoGabor used Ask the Experts™
on
Help i'm going mad. I have the following script to zip files from a certain directory on several machines and copy the zip file to a local directory. However I can't get the for loop to work. it simply bombs out as soon as it starts the for loop. Can anyone help?

rem **** Get date
:: set environment variable yyyymmdd to current yymmdd
for /f "tokens=2,3,4 delims=/-. " %%i in ('date/t') do set yyyymmdd=%%k%%i%%j


if NOT exist "c:\logs" (
      mkdir c:\logs
      )


for %%machine in (machine1 machine2 machine3) do (
      net use t: \\%machine%\c$
      c:\apps\WinZip7.0\wzzip c:\logs\tempzip t:\app\log\*.*
      move c:\logs\tempzip.zip c:\logs\%machine%-logs%yyyymmdd%.zip
      net use t: /d )



THanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
I would try like this :

:: prepare the list
> machines.txt echo machine1
>> machines.txt echo machine2
>> machines.txt echo machine3

:: make sure the drive letter is "free"
net use t: /d

:: loop
for /F %%m in (machines.txt) do (
    net use t: \\%%m\c$
    c:\apps\WinZip7.0\wzzip c:\logs\tempzip t:\app\log\*.*
    move c:\logs\tempzip.zip c:\logs\%%m-logs%yyyymmdd%.zip
    net use t: /d
)

:: clean up
del /q machines.txt

Commented:
You're close enough to taste it and can use the knowlege in future scripts.

The bottom line is for will ONLY work with single letter variables.  Every "%%" in a for loop is converted to "%" before sent to the parser.  The parser will only expand it if the first letter after it is valid. in:
   for /f "tokens=2,3,4 delims=/-. " %%i in ('date/t') do set yyyymmdd=%%k%%i%%j
tokens 2, 3, 4 setup %i %j and %k so that:
   set yyyymmdd=%%k%%i%%j
gets sent to the parser as
   set yyyymmdd=%k%i%j
and the parser sends to the interpreter with expanded %k, %i, and %j  -  all is kosher.


In this one:
   for %%machine in (machine1 machine2 machine3) do ( ... )
the FOR tries to setup %m for you, but before it can see the "in" list it MUST figgure out what you mean by "achine"
It can't so it dies.

This one will work just fine if %%m is put where you want the names:
for %%m in (machine1 machine2 machine3) do (
     net use t: \\%%m\c$
    c:\apps\WinZip7.0\wzzip c:\logs\tempzip t:\app\log\*.*
    move c:\logs\tempzip.zip c:\logs\%%m-logs%yyyymmdd%.zip
    net use t: /d )


Good Luck,

2K
(\o/)

Commented:
btw, this one uses the same machine you're on for date everywhere, that's fine.

If you decentralize this and run date-related stuff on several machines you should know the 'data/t' command gives a different format, different day:mont order, or different number of digits in years, on different OS versions and different Language and Region settings.  There are no short easy fixes to that, you just have to decide if format is important to you and deal with the differences.  

my 2¢,

2K
(\o/)

Author

Commented:
Great - that works. I like this solution as it separates the machine variables from the script for the end-user. K_2K - thanks for the info, great answer, but I just prefer the simple solution of the previous answer.

Thanks to both of you!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial