Link to home
Start Free TrialLog in
Avatar of vvkp
vvkp

asked on

Passing parameters in batch files.

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.
Avatar of Lee W, MVP
Lee W, MVP
Flag of United States of America image

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

Avatar of vvkp
vvkp

ASKER

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.
Avatar of vvkp

ASKER

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.
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.
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
)
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
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
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
ASKER CERTIFIED SOLUTION
Avatar of t0t0
t0t0
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial