Avatar of jskfan
jskfan
Flag for Cyprus asked on

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

Windows BatchScripting Languages

Avatar of undefined
Last Comment
Bill Prew

8/22/2022 - Mon
Bill Prew

"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
jskfan

ASKER
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, CD

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

Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Bill Prew

%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
jskfan

ASKER
@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
ASKER CERTIFIED SOLUTION
Bill Prew

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
jskfan

ASKER
will get to this topic sometime in the future
Thank you
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Bill Prew

Welcome.


»bp