My TEST.CMD file shares a folder with ToBAK.CMD.
The code appended below fails, it produces
- 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 :-
- 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.
@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