|
View Full Version : strange SET behaviour
Michal Sankot 03-22-2004, 04:45 PM Heya,
I have problems using SET command, as it behaves rather strangely.
What it does, is that it doesn't assign value to a variable (which one
would axpect it to do).
My cmd script is:
-------------
set RESULTS_DIR=results
for /l %%G in (1,1,2) do (
for /f %%H in ('dir /b small_%%G.log*.*') do (
set FILE_TO_MOVE=%%H
:doMove
echo moving %FILE_TO_MOVE% ...
move %FILE_TO_MOVE% %RESULTS_DIR%
if not exist %RESULTS_DIR%\%FILE_TO_MOVE% (
sleep 1
goto doMove
)
)
)
-------------
It should move 2 logs small_#.log*.* to results folder and when some
of them isn't ready yet, it should wait 1 second and try it again.
Strangley enough "set FILE_TO_MOVE=%%H" doesn't assign anything to
FILE_TO_MOVE and it keeps cycling and printing out, it's moving the
file. Second "for" command does return name of log file, cause
otherwise it wouldn't get to printing out that it's doing it.
Is there some stragnge rule, that SET A=%%B doesn't work in for cycles
?
Michal
Jerold Schulman 03-22-2004, 04:55 PM On 22 Mar 2004 08:45:33 -0800, sankotm@seznam.cz (Michal Sankot) wrote:
>Heya,
>I have problems using SET command, as it behaves rather strangely.
>What it does, is that it doesn't assign value to a variable (which one
>would axpect it to do).
>
>My cmd script is:
>-------------
>set RESULTS_DIR=results
>for /l %%G in (1,1,2) do (
> for /f %%H in ('dir /b small_%%G.log*.*') do (
> set FILE_TO_MOVE=%%H
> :doMove
> echo moving %FILE_TO_MOVE% ...
> move %FILE_TO_MOVE% %RESULTS_DIR%
> if not exist %RESULTS_DIR%\%FILE_TO_MOVE% (
> sleep 1
> goto doMove
> )
> )
>)
>-------------
>It should move 2 logs small_#.log*.* to results folder and when some
>of them isn't ready yet, it should wait 1 second and try it again.
>
>Strangley enough "set FILE_TO_MOVE=%%H" doesn't assign anything to
>FILE_TO_MOVE and it keeps cycling and printing out, it's moving the
>file. Second "for" command does return name of log file, cause
>otherwise it wouldn't get to printing out that it's doing it.
>
>Is there some stragnge rule, that SET A=%%B doesn't work in for cycles
>?
>
>Michal
setlocal ENABLEDELAYEDEXPANSION
and replace all %variable% with !variable!
Jerold Schulman
Windows: General MVP
JSI, Inc.
http://www.jsiinc.com
Jerold Schulman 03-22-2004, 04:55 PM On 22 Mar 2004 08:45:33 -0800, sankotm@seznam.cz (Michal Sankot) wrote:
>Heya,
>I have problems using SET command, as it behaves rather strangely.
>What it does, is that it doesn't assign value to a variable (which one
>would axpect it to do).
>
>My cmd script is:
>-------------
>set RESULTS_DIR=results
>for /l %%G in (1,1,2) do (
> for /f %%H in ('dir /b small_%%G.log*.*') do (
> set FILE_TO_MOVE=%%H
> :doMove
> echo moving %FILE_TO_MOVE% ...
> move %FILE_TO_MOVE% %RESULTS_DIR%
> if not exist %RESULTS_DIR%\%FILE_TO_MOVE% (
> sleep 1
> goto doMove
> )
> )
>)
>-------------
>It should move 2 logs small_#.log*.* to results folder and when some
>of them isn't ready yet, it should wait 1 second and try it again.
>
>Strangley enough "set FILE_TO_MOVE=%%H" doesn't assign anything to
>FILE_TO_MOVE and it keeps cycling and printing out, it's moving the
>file. Second "for" command does return name of log file, cause
>otherwise it wouldn't get to printing out that it's doing it.
>
>Is there some stragnge rule, that SET A=%%B doesn't work in for cycles
>?
>
>Michal
setlocal ENABLEDELAYEDEXPANSION
and replace all %variable% with !variable!
Jerold Schulman
Windows: General MVP
JSI, Inc.
http://www.jsiinc.com
Matthias Tacke 03-22-2004, 05:13 PM Michal Sankot wrote:
>Heya,
>I have problems using SET command, as it behaves rather strangely.
>What it does, is that it doesn't assign value to a variable (which one
>would axpect it to do).
>
>My cmd script is:
>-------------
>set RESULTS_DIR=results
>for /l %%G in (1,1,2) do (
>for /f %%H in ('dir /b small_%%G.log*.*') do (
>set FILE_TO_MOVE=%%H
>:doMove
>echo moving %FILE_TO_MOVE% ...
>move %FILE_TO_MOVE% %RESULTS_DIR%
>if not exist %RESULTS_DIR%\%FILE_TO_MOVE% (
>sleep 1
>goto doMove
>)
>)
>)
>-------------
>It should move 2 logs small_#.log*.* to results folder and when some
>of them isn't ready yet, it should wait 1 second and try it again.
>
>Strangley enough "set FILE_TO_MOVE=%%H" doesn't assign anything to
>FILE_TO_MOVE and it keeps cycling and printing out, it's moving the
>file. Second "for" command does return name of log file, cause
>otherwise it wouldn't get to printing out that it's doing it.
>
>Is there some stragnge rule, that SET A=%%B doesn't work in for cycles
Hello Michal.
The strange rule is: the value are evaluated only once at the begin of
a cycle. You could:
- move the part from label :doMove into a seperate sub and call it.
- use delayed expansion what sometimes has some strange effects
- use in your case always %%H.
BTW if the file is not accessed by another task - IMO the sleep doesn't
make sense. The move either had worked or not. A repitition would cause
the same result.
HTH
--
Greetings
Matthias________________________________________
For help on nt commands enter in a cmd window:
W2K>HH windows.chm::ntcmds.htm XP>HH ntcmds.chm
Matthias Tacke 03-22-2004, 05:13 PM Michal Sankot wrote:
>Heya,
>I have problems using SET command, as it behaves rather strangely.
>What it does, is that it doesn't assign value to a variable (which one
>would axpect it to do).
>
>My cmd script is:
>-------------
>set RESULTS_DIR=results
>for /l %%G in (1,1,2) do (
>for /f %%H in ('dir /b small_%%G.log*.*') do (
>set FILE_TO_MOVE=%%H
>:doMove
>echo moving %FILE_TO_MOVE% ...
>move %FILE_TO_MOVE% %RESULTS_DIR%
>if not exist %RESULTS_DIR%\%FILE_TO_MOVE% (
>sleep 1
>goto doMove
>)
>)
>)
>-------------
>It should move 2 logs small_#.log*.* to results folder and when some
>of them isn't ready yet, it should wait 1 second and try it again.
>
>Strangley enough "set FILE_TO_MOVE=%%H" doesn't assign anything to
>FILE_TO_MOVE and it keeps cycling and printing out, it's moving the
>file. Second "for" command does return name of log file, cause
>otherwise it wouldn't get to printing out that it's doing it.
>
>Is there some stragnge rule, that SET A=%%B doesn't work in for cycles
Hello Michal.
The strange rule is: the value are evaluated only once at the begin of
a cycle. You could:
- move the part from label :doMove into a seperate sub and call it.
- use delayed expansion what sometimes has some strange effects
- use in your case always %%H.
BTW if the file is not accessed by another task - IMO the sleep doesn't
make sense. The move either had worked or not. A repitition would cause
the same result.
HTH
--
Greetings
Matthias________________________________________
For help on nt commands enter in a cmd window:
W2K>HH windows.chm::ntcmds.htm XP>HH ntcmds.chm
|
|
|