Avatar of jskfan
jskfanFlag 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
Avatar of Bill Prew
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
Avatar of jskfan
jskfan
Flag of Cyprus image

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

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

Avatar of Bill Prew
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
Avatar of jskfan
jskfan
Flag of Cyprus image

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
Avatar of Bill Prew
Bill Prew

Blurred text
THIS SOLUTION IS 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
Avatar of jskfan
jskfan
Flag of Cyprus image

ASKER

will get to this topic sometime in the future
Thank you
Avatar of Bill Prew
Bill Prew

Welcome.


»bp
Scripting Languages
Scripting Languages

A scripting language is a programming language that supports scripts, programs written for a special run-time environment that automate the execution of tasks that could alternatively be executed one-by-one by a human operator. Scripting languages are often interpreted (rather than compiled). Primitives are usually the elementary tasks or API calls, and the language allows them to be combined into more complex programs. Environments that can be automated through scripting include software applications, web pages within a web browser, the shells of operating systems (OS), embedded systems, as well as numerous games. A scripting language can be viewed as a domain-specific language for a particular environment; in the case of scripting an application, this is also known as an extension language.

30K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo