1

I want the program to process an arbitrary number of command-line arguments, but I'm unsure how to implement a for loop to go through multiple shift commands. Also, the program shouldn't close the command prompt window when it ends.

Here is what I have so far:

@echo off
if "%1"=="" goto skip
if "%1"=="-ti" time /t
if "%1"=="-da" date /t
if "%1"=="-c" cls
if "%1"=="-ip" ipconfig
if "%1"=="-d" dir
if "%1"=="-p" path
if "%1"=="-v" ver
if "%1"=="-t" type
if "%1"=="-m" md
exit /b
:skip
echo No arguments!

I'd like to have the following command-line arguments run properly:

C:\>batchfile –d –ti –da –ip –v
C:\>batchfile
C:\>batchfile –d –m CSC3323 –d
C:\>batchfile –d –t batchfile.bat –m CSC3323 –d
2

1 Answer 1

2

Use this code to loop through your arguments in a file called batchfile.bat:

@echo off
echo arguments: "%*"
:loop
if "%~1"=="" goto skip
if "%~1"=="-ti" time /t
if "%~1"=="-da" date /t
if "%~1"=="-c" cls
if "%~1"=="-ip" ipconfig
if "%~1"=="-d" dir
if "%~1"=="-p" path
if "%~1"=="-v" ver
if "%~1"=="-t" type
if "%~1"=="-m" md
shift
goto loop
:skip
echo No arguments!
pause

and call the batchscript like this:

start batchfile.bat -ti -d -ti -da -ip -v 
start batchfile.bat
start batchfile.bat -d -m CSC3323 -d 
start batchfile.bat -d -t batchfile.bat -m CSC3323 -d 
Sign up to request clarification or add additional context in comments.

1 Comment

Why using start? and I guess you, or better the OP, forgot "%~1" the type command...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.