MS DOS – Command Line Parameters

Background

Looking at someone’s code is a good way to tighten up one’s own education.

I was looking at a good man’s code and immediately knew, I had a huge learning curve.

 

Table

Pattern Meaning Sample
Script
%~dp0 Command Line Folder C:\batchfile\helloWorld\
%~n0 Command Line File helloWorld
%~f0 Command Line Folder And File C:\batchfile\helloWorld\helloWorld.cmd
Command Line Arguments
%1 Command Line Argument #1 Peter
%2 Command Line Argument #2 Paul
%3 Command Line Argument #3 Mary
Command Line Arguments (Full )
%* Command Line Arguments Full Peter Paul Mary

Script



@ECHO OFF

rem SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
SETLOCAL

set "_scriptCommandLinefolder=%~dp0"

set "_scriptCommandLineFile=%~n0"

set "_scriptCommandLineFolder_And_File=%~f0"

set "_scriptCurrent=%~f0"

set _SCRIPT_DRIVE=%~d0

set _SCRIPT_PATH=%~p0

ECHO.
ECHO.

echo _scriptFolder:- %_scriptCommandLineFolder%

echo _scriptFile:- %_scriptCommandLineFile%

echo _scriptFolderFile:- %_scriptCommandLineFolder_And_File%


ECHO.
ECHO.

echo _scriptCurrent:- %_scriptCurrent%

ECHO.
ECHO.


echo _scriptDrive:- %_SCRIPT_DRIVE%

echo _scriptPath:- %_SCRIPT_PATH%

rem command line parameter
ECHO.
ECHO.

set "_arg01=%1"
set "_arg02=%2"
set "_arg03=%3"

set "_argAll=%*"

IF NOT "%_arg01%"=="" (
	echo Command Line Argument 1:- %_arg01%
)

IF NOT "%_arg02%"=="" (
	echo Command Line Argument 2:- %_arg02%
)

IF  NOT "%_arg03%"=="" (
	echo Command Line Argument 3:- %_arg03%
)

IF  NOT "%_argAll%"=="" (
	echo "Command Line Arguments ( All) '%_argAll%' "
)

ECHO.

ENDLOCAL

Output

 

Source Code

Git

Gist

DanielAdeniji/commandLineArgs.cmd
Link

References

  1. Stack Overflow
    • DOS-Batch – Command Line
      • Batch scripting: What’s the difference between %0 and %~f0?
        Link
      • What does %~d0 mean in a Windows batch file?
        Link
    • Dos-Batch – If Statement
      • Batch not-equal (inequality) operator
        Link
      • Can I have an IF block in DOS batch file?
        Link
  2. Rob Van Der Woude’s Scripting Pages
    • Command line parameters
      Link

 

Dedication

Dedicating to RS; a good, quiet, matured, seasoned, and tender man.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s