We help IT Professionals succeed at work.

Explain Windows Batch Script

Explain Windows Batch Script

I need the following elements of the script below to be explained by an Expert:

"tokens=*"
'dir /b'
%1

Thank you

@echo off
SET count=1
FOR /f "tokens=*" %%G IN ('dir /b') DO (call :subroutine "%%G")
GOTO :eof

:subroutine
 echo %count%:%1
 set /a count+=1
timeout 60
 GOTO :eof

pause

Open in new window

Comment
Watch Question

Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
"tokens=*"

I’m assuming you understand what a FOR loop does (if not just ask).  So, tokens=* tells the FOR not to parse up each line it reads, and just place the whole line in the loop variable, %%G in this case.

'dir /b'

The DIR command lists all files and folders in the current folder.  The /B option selects “bare” format, showing only the name of the file (or full path if used with the /S option).  Som no date or size information is passed to the FOR loop, just the file name.

%1

This is the way parameters to subroutines are referenced in a subroutine.  When the subroutine was called a parm was passed, in this case %%G, and so in the subroutine %1 will be replaced with the value of the first parm when the subroutine was called.

On mobile right now so can’t type too much easily but hope this helps, come back with additional places you need clarification, happy to help.

~bp

Author

Commented:
if I understand, tokens specifies the columns if it is CSV file or text separated by comma, semi colon, or spaces

%%G is a variable which will contain the list of the folder names and file names in the current directory.

%1 : will have the first value of %%G , which is a folder or file name in the current directory .... Will that increment behind the scene ? example %2,%3, etc....?

I also want  to know when GOTO :eof  applies.  I see the Subroutine is Called then the next line is : GOTO :eof
@echo off
SET count=1
FOR /f "tokens=*" %%G IN ('dir /b') DO (call :subroutine "%%G")
GOTO :eof

:subroutine
 echo %count%:%1
 set /a count+=1
timeout 60
 GOTO :eof

pause

Open in new window

David Johnson, CDSimple Geek from the '70s
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
line 4  will execute when there are no tokens left and jumps over the subroutine and ends
line 10 returns from the subroutine
line 12 will never execute
@echo off
SET count=1
FOR /f "tokens=*" %%G IN ('dir /b') DO (call :subroutine "%%G")
GOTO :END

:subroutine
 echo %count%:%1
 set /a count+=1
rem timeout 60
 GOTO :eof
:END
@echo END called
pause

Open in new window

Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
%1 : will have the first value of %%G , which is a folder or file name in the current directory .... Will that increment behind the scene ? example %2,%3, etc....?

%%G will take on the various file names returned by the DIR command, one by one as the loop "iterates".  As it calls the subroutine in each iteration of the loop it will pass the current value of %%G (which will be the current file being processed) and in the subroutine %1 will reference the current passed value (will be each file name returned by DIR, one by one)

I also want  to know when GOTO :eof  applies.  I see the Subroutine is Called then the next line is : GOTO :eof

GOTO :EOF (which can also be replaced by EXIT /B for a little efficiency) has the effect of a "RETURN" in many other languages.  It basically goes to the end of the BAT script and resumes execution there, which causes a return type effect.

Every CALLed subroutine needs at least on of these to return to the caller properly.

When you have subroutines after your "mainline" code, you need to make sure the BAT script doesn't  keep executing at the end of the mainline code and start executing the subroutine logic.  Do do this you use either the GOTO :EOF or EXIT /B to stop execution at the end of the mainline, before it "falls through" to the subroutine(s) code.


»bp

Author

Commented:
@echo off
SET count=1
FOR /f "tokens=*" %%G IN ('dir /b') DO (call :subroutine "%%G")
GOTO :END

:subroutine
 echo %count%:%1
 set /a count+=1
rem timeout 60
 GOTO :eof
:END
@echo END called
pause

Open in new window





- if I understand, tokens specifies the columns if it is CSV file or text separated by comma, semi colon, or spaces ?

- line 4: will send the script to the END [Exit Door] when the subroutine finishes up ?

-line 10 : GOTO :eof  will loop back to the subroutine so that it can process the next folder or file name ?

Thank you
Test your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016
Commented:
- if I understand, tokens specifies the columns if it is CSV file or text separated by comma, semi colon, or spaces ?

Yes, that's a good general statement, but the FOR /F is actually one of the more powerful and complicated DOS commands.  It can (and is often) be used to read and parse text input from a text file, but it can also be used to parse the output of a command (via STDOUT) as well as just text strings.  Take a look at the references in the links at the bottom of this post for additional info.

- line 4: will send the script to the END [Exit Door] when the subroutine finishes up ?

True that is just a "normal" GOTO targetting a defined label as the destination.  In this case it's at the end of the script so it will do some cleanup tasks and then the script with end normally.

-line 10 : GOTO :eof  will loop back to the subroutine so that it can process the next folder or file name ?

Yes, GOTO :EOF uses the special built in lable :EOF to jump to the end of the BAT script (conceptually), and since we are in a CALLed function it acts like a "return" from the subroutine.  You don't need to define the :EOF label anyplace in your BAT file, it's a special internal label.

References:


»bp

Author

Commented:
will get to this topic sometime in the future
Thank you
Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
Welcome.


»bp