?
Solved

Passing parameters in batch files.

Posted on 2009-04-23
9
Medium Priority
?
744 Views
Last Modified: 2012-05-06
Hello friends,
I saw two batch files....
One is   DOIT.bat which contains.....
****************************
cd C:\test\
for /f %%a IN ('dir /b *.txt') do call   RUNNER.bat %%~na
****************************
Then another  batch file  is   RUNNER.bat which contains...
**********
echo %1-- is a file name
******************
Now my question is there any way to combine those two into one batch file getting the same functionality? Please let me know thanks in advance.
0
Comment
Question by:vvkp
  • 5
  • 2
  • 2
9 Comments
 
LVL 97

Expert Comment

by:Lee W, MVP
ID: 24221944
Easily.  

Instead of "call RUNNER.bat" change that to "call :RUNNER"

Then add a label called :RUNNER
But to keep things from getting messy at the end of the for loop, you'll also need a GOTO.  See code sample below.

@echo off
cd\test
for /f %%a IN ('dir /b *.txt') do call :RUNNER %%~na %%~xa
Goto End
:RUNNER
echo %1-test
:End

Open in new window

0
 

Author Comment

by:vvkp
ID: 24221967
Great... but the problem is when I try with your code by keeping 4 .txt files in a folder I am getting five displays like
TestFile1 - is a filename
TestFile2 - is a filename
TestFile3 - is a filename
TestFile4 - is a filename
 - is a filename
where TestFile1 to TestFile4 are four .txt files.
0
 

Author Comment

by:vvkp
ID: 24222210
Hello Leew,
I figured it why I am getting that ....
you forgot to put colon  :  in front of  END in the GoTo statement 4th line.
That is why I am getting the error probably..I am not sure because I am novice in dos batch files.
When I put colon in front of END it seems working fine. Please confirm.
Really I appreciate your help.
0
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
LVL 97

Expert Comment

by:Lee W, MVP
ID: 24222359
If you are correct it's probably because you named it something.bat instead of using .cmd extension.  They are almost identical in functionality, but there are a couple of minor differences I simply don't recall.

Please post a screenshot of the error your getting as I tested this before posting and it worked fine as posted.
0
 
LVL 16

Expert Comment

by:t0t0
ID: 24222734
EXAMPLE 1 - No need to us separate batch file or sub-routine to get and display filename

cd C:\test\
for /f "tokens=*" %%a in ('dir /b *.txt') do (
   echo %%a -- is a file name
)
0
 
LVL 16

Expert Comment

by:t0t0
ID: 24222761
EXAMPLE 2 - Combining get and display filename (uses sub-routine)

@echo off
cd C:\test\
for /f "tokens=*" %%a in ('dir /b *.txt') do (
   call :runner "%%a"
)
exit /b

:runner
echo %~1 -- is a file name
exit /b
0
 
LVL 16

Expert Comment

by:t0t0
ID: 24222774
EXAMPLE 3 -  No need to CD in c:\test\

@echo off
for /f "tokens=*" %%a in ('dir /b c:\test\*.txt') do (
   call :runner "%%a"
)
exit /b

:runner
echo %~1 -- is a file name
exit /b
0
 
LVL 16

Expert Comment

by:t0t0
ID: 24222777
EXAMPLE 4 - Single-line FOR loop

@echo off
for /f "tokens=*" %%a in ('dir /b c:\test\*.txt') do call :runner "%%a"
exit /b

:runner
echo %~1 -- is a file name
exit /b
0
 
LVL 16

Accepted Solution

by:
t0t0 earned 200 total points
ID: 24222912
NOTE: - Spaces in filename

In the above examples, I have included "TOKENS=*" in the FOR loop. This ensures you get the full filename even if it contains spaces - otherwise you would get the following results:

FILENAME          %%a (without "tokens=*")
MyABC.txt         MyABC.txt
My ABC.txt        My

FILENAME         %%a (with "tokens=*")
MyABC.txt         MyABC.txt
My  ABC.txt       My ABC.txt


FURTHERMORE: In the FOR loop, I have enclosed the %%a in double quotes so that should %%a contain spaces, %1 sees it as a single parameter - otherwise you would get the following results:

FILENAME          %1 (with %%a)
MyABC.txt         MyABC.txt
My ABC.txt        My                            %2 would equal just 'ABC.txt'

FILENAME         %1 (with "%%a*")
MyABC.txt         "MyABC.txt"
My  ABC.txt       "My ABC.txt"

FINALLY: Inside RUNNER, I have stripped away the double-quotes from %1 by using %~1 instead %1 - otherwise you would get the following results:

FILENAME          %%a                   %1                     %~1
MyABC.txt         MyABC.txt           MyABC.txt         MyABC.txt
My ABC.txt        "My ABC.txt"       "My ABC.txt"      My ABC.txt
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Screencast - Getting to Know the Pipeline
Suggested Courses
Course of the Month15 days, 11 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question