Page 1 of 1
FOR IN DO and GOTO
Posted: 21 May 2011 09:17
by tebee
Hi, i am trying to scan daughter folders chasing for unique name.ext files and make some conversion on them (before doing more).
Actually i am trying to embed some GOTO inside a FOR IN DO loop, using delayed expansion, but i get weird result:
ECHO ON
SETLOCAL EnableDelayedExpansion & SETLOCAL EnableExtensions
FOR /F %%G IN (FOLDERLIST.TXT ^| 'FINDSTR /B /U' "-") DO (
IF NOT EXIST %%G\*.CR? IF NOT EXIST %%G\*.NEF GOTO :NF
CALL :FOUND %%G *
:NF
DIR>NUL & ECHO>NUL
)
GOTO :eof
:FOUND
DIR /B %1\%2
GOTO :eof
Eventually it stops after GOTO :NF execution, like if the closing bracket has some special behaviors.
Also, the | FINDSTR /B /U does not work...
Thanks
Re: FOR IN DO and GOTO
Posted: 21 May 2011 13:15
by Ed Dyreen
I don't think you can use labels inside a for loop.
Re: FOR IN DO and GOTO
Posted: 21 May 2011 13:58
by Cleptography
Not sure if I can help here but I will try.
Ed is right not sure you can call or goto a label from within a for loop.
The if statements you are using check for two matching conditions and if
they are not met, not sure if that is what you are going for. I don't think you
need the delayed values either.
But something like this...
UNTESTED:
Code: Select all
ECHO ON
SETLOCAL EnableDelayedExpansion & SETLOCAL EnableExtensions
FOR /F "TOKENS=*" %%G IN ('"TYPE FOLDER.TXT | FINDSTR /B "-""') DO (
IF NOT EXIST %%G\*.CR (CALL :NF
) ELSE IF NOT EXIST %%G\*.NEF (CALL :NF
) ELSE CALL :FOUND %%G *
))
EXIT /B
:FOUND
DIR /B %1\%2
GOTO :eof
:NF
DIR>NUL && ECHO>NUL
)
GOTO :eof
Re: FOR IN DO and GOTO
Posted: 21 May 2011 14:07
by aGerman
Option /U doesn't exist for FINDSTR. What should it be good for?
You cannot use labels in a multi line block, but you could reverse the logic:
Code: Select all
IF EXIST %%G\*.CR? IF EXIST %%G\*.NEF CALL :FOUND %%G *
Now you don't need the label.
Regards
aGerman
Re: FOR IN DO and GOTO
Posted: 21 May 2011 14:33
by dbenham
aGerman wrote:You cannot use labels in a multi line block, but you could reverse the logic:
Code: Select all
IF EXIST %%G\*.CR? IF EXIST %%G\*.NEF CALL :FOUND %%G *
Now you don't need the label.
Not quite the same logic
:FOUND will only be called if both types of files are found (.CR? and .NEF)
I think a variable is needed to exactly replicate the original logic:
Code: Select all
set foundIt=
IF EXIST %%G\*.CR? SET foundIt=true
IF EXIST %%G\*.NRF SET foundIt=true
IF DEFINED foundIt (CALL :FOUND %%G *) ELSE (DIR>NUL & ECHO>NUL)
But I can't imagine what purpose the ELSE clause serves - it doesn't do anything productive as written.
I am guessing that tebee wanted the \V option - \V and \U look very similar when viewing help in a command window. I am assuming that lines that start with "-" are to be ignored. If that is the case, then the code can be greatly simplified - the FOR /F loop has an "eol" option that ignores lines beginning with a specified character.
Code: Select all
for /f "eol=-" %%g in (folderlist.txt) do (
set foundIt=
if exist %%g\*.cr? set foundIt=true
if exist %%g\*.nrf set foundIt=true
if defined foundIt (call :found %%G *) else (dir>nul & echo>nul)
)
Dave Benham
Re: FOR IN DO and GOTO
Posted: 21 May 2011 14:51
by tebee
Thank's dbenham
the question was put raw but you got the point.
Tebee