strange SET behaviour

  • Thread starter Thread starter Michal Sankot
  • Start date Start date
M

Michal Sankot

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
 
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
 
Michal said:
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
 
Back
Top