FOR IN DO and GOTO

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
tebee
Posts: 13
Joined: 21 May 2011 08:41

FOR IN DO and GOTO

#1 Post by tebee » 21 May 2011 09:17

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

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: FOR IN DO and GOTO

#2 Post by Ed Dyreen » 21 May 2011 13:15

I don't think you can use labels inside a for loop.

Cleptography
Posts: 287
Joined: 16 Mar 2011 19:17
Location: scriptingpros.com
Contact:

Re: FOR IN DO and GOTO

#3 Post by Cleptography » 21 May 2011 13:58

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

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: FOR IN DO and GOTO

#4 Post by aGerman » 21 May 2011 14:07

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

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: FOR IN DO and GOTO

#5 Post by dbenham » 21 May 2011 14:33

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 :wink: :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

tebee
Posts: 13
Joined: 21 May 2011 08:41

Re: FOR IN DO and GOTO

#6 Post by tebee » 21 May 2011 14:51

Thank's dbenham
the question was put raw but you got the point.

Tebee

Post Reply