1.

Solve : Is GOTO illegal in a FOR LOOP - i.e. FOR %%X (*.*) DO ( loop code )?

Answer»

My TEST.CMD file shares a folder with ToBAK.CMD.

The code appended below fails, it produces
Quote

- 12 - 1 - ToBAK.cmd - ToBAK.cmd - ToBAK
- 12 - 2 - ToBAK.cmd - %Y - %~nY
Press any key to continue . . .

By simply changing "SET MAX=2" to "SET MAX=1", the "GOTO L2" does not execute and I get the "correct" result :-
Quote
- 12 - 1 - ToBAK.cmd - ToBAK.cmd - ToBAK
- 13 - 1 - test.cmd - test.cmd - test
Press any key to continue . . .

My Conclusions :- execution of "GOTO L2" results in
a) Total loss of the value of %%Y the second time around;
b) Subsequent failure of the FOR (*.*) loop to process the second file.

Code: [Select]@ECHO OFF
setlocal enabledelayedexpansion
SET MAX=2
SET N=11
FOR %%Y in (*.*) DO (
SET Z=%%Y
SET /A N+=1
set P=0
:L2
SET /A P+=1
ECHO - !N! - !P! - !Z! - %%Y - %%~nY
IF !P! LSS !MAX! GOTO L2
)
pause

N.B. The above code illustrates the peculiarity I am fighting.
The real code I attempting has the same basic form with the exception that instead of a harmless "ECHO %%Y" etc. I am trying to do more DANGEROUS things involving the creation of new sub-folders and relocating precious archives into the new structure.

I am now thinking I can replace the 4 LINES of ":L2 ... GOTO LOOP L2" code with a single line "CALL :LOOP2" and that can have the secondary iteration code.

Any advice will be appreciated, but much as I want to finish my present project, I wish now to learn about this peculiarity, and anything I have done wrong, and how to avoid this sort of problem in future.

Regards
Alan
You could try and do something like the following:

Code: [Select]FOR /f "usebackq" %%Y in (*.*) do (
SET Z=%%A& SET /A N+=1& SET P=0 & CALL :L2 %%A
)

:L2
Thanks

I have made a start upon using CALL :LOOP2 whilst awaiting further feedback.

I am hoping for an explanation of why GOTO trashes a FOR LOOP so I will know what to avoid in future.

Incidentally, I recognised "usebackq" and ran "FOR /?" to find out more
- I still have not got to grips with it.
I did alter my code to
FOR /f "usebackq" %%Y in (*.*) DO (
and all I got was
Quote
The system cannot find the file *.*.
Press any key to continue . . .

Regards
Alan
what information are you trying to set in the variable Z? Right now this will run against any files in the current directory for the batch file. if you are trying to pull data from a specific file you will need to replace (*.*) with the file location that has the information you are looking for. I never wanted Z in the first place.

The only reason I used "SET Z=%%Y" was because I observed that %%Y became undefined when GOTO L2 was executed,
therefore before it lost its value I used "SET Z=%%Y" to see if Z was more permanent.
I found that where GOTO killed %%Y, it did no damage to !Z!

I thought I had a perfect solution by using !Z! wherever I had used %%Y.
BUT THEN I added a second file in the folder and found that GOTO L2 not only killed the value of %%Y, it also caused "FOR %%Y ..." to forget where it was, and the subsequent file(s) never got processed.

The two lines of output were
- 12 - 1 - ToBAK.cmd - ToBAK.cmd - ToBAK
- 12 - 2 - ToBAK.cmd - %Y - %~nY
The second line shows that !Z! is still defined as "ToBAK.cmd"
but %%Y and %%~nY have become undefined and useless.

The eventual purpose of my project is to take a folder with disc partition image files such as
I:\Backups\2009_03_31_19_06_40_198D.TIB
I:\Backups\2009_04_05_18_49_38_184D.TIB
I:\Backups\2009_06_06_14_00_32_370D.TIB
Then for each such file create a companion folder such as
I:\Backups\2009_03_31_19_06_40_198D
Each of these companion folders will hold sub-folders such as
I:\Backups\2009_03_31_19_06_40_198D\2
I:\Backups\2009_03_31_19_06_40_198D\3
I:\Backups\2009_03_31_19_06_40_198D\4
Then "incremental" files such as
I:\Backups\2009_03_31_19_06_40_198D2.TIB
I:\Backups\2009_03_31_19_06_40_198D3.TIB
I:\Backups\2009_03_31_19_06_40_198D4.TIB
will be moved to
I:\Backups\2009_03_31_19_06_40_198D\2\2009_03_31_19_06_40_198D2.TIB
I:\Backups\2009_03_31_19_06_40_198D\3\2009_03_31_19_06_40_198D3.TIB
I:\Backups\2009_03_31_19_06_40_198D\4\2009_03_31_19_06_40_198D4.TIB

Regards
Alan
ok I think i understand what you are looking for let me see if I can get it working for you. Thanks, but I have the original code fixed as below.

The final implementation is coming along NICELY,
but I am taking it slow with lots of debug,
because my post above has glossed over many intricacies.

All I really need now is an explanation of what rule(s) I violated by using a GOTO within a FOR loop.

I appreciate that GOTO is not a nice WAY to break out of a loop
but I was staying inside the loop and cannot see why that gives disastrous results.

Code: [Select]@ECHO OFF
setlocal enabledelayedexpansion
SET MAX=3
SET N=11
FOR %%Y in (T*.*) DO (
SET Z=%%Y
SET /A N+=1
set P=0
CALL :L2 %%Y
)
pause
EXIT /B

:L2
SET /A P+=1
ECHO - !N! - !P! - !Z! - %1 - %~n1
IF !P! LSS !MAX! GOTO L2

Regards
Alan


Discussion

No Comment Found