Link to home
Start Free TrialLog in
Avatar of JoJoGabor
JoJoGabor

asked on

MSDOS For Loop

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
ASKER CERTIFIED SOLUTION
Avatar of _nn_
_nn_

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of K_2K
K_2K

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/)
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/)
Avatar of JoJoGabor

ASKER

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!