Get Batch Parameter as File Path

Q

How to get path name components of a batch file parameter? A file name is passed as the parameter.

✍: FYIcenter.com

A

If a batch parameter represents a file name, you can use batch parameter modifiers on %1, %2 or others, to get its file patch components.

Here is a batch file, Batch-Parameter-Modifiers.bat, that dumps the %1 parameter with different modifiers:

@echo off
rem Batch-Parameter-Modifiers.bat - Show components of a batch parameter

echo 1          = %1      
echo ~1         = %~1      
echo ~f1        = %~f1     
echo ~d1        = %~d1     
echo ~p1        = %~p1     
echo ~n1        = %~n1     
echo ~x1        = %~x1     
echo ~s1        = %~s1    
echo ~a1        = %~a1     
echo ~t1        = %~t1     
echo ~z1        = %~z1     
echo ~$PATH:1   = %~$PATH:1
echo ~dp1       = %~dp1      
echo ~pd1       = %~pd1      
echo ~nx1       = %~nx1      
echo ~dp$PATH:1 = %~dp$PATH:1
echo ~ftza1     = %~ftza1    

If you run the above batch file with an absolute file name, you get the following output:

C:\fyicenter>Batch-Parameter-Modifiers.bat "\Program Files\7-Zip\7z.exe"

1          = "\Program Files\7-Zip\7z.exe"
~1         = \Program Files\7-Zip\7z.exe
~f1        = C:\Program Files\7-Zip\7z.exe
~d1        = C:
~p1        = \Program Files\7-Zip\
~n1        = 7z
~x1        = .exe
~s1        = C:\PROGRA~1\7-Zip\7z.exe
~a1        = --a------
~t1        = 10/04/2016 09:51 AM
~z1        = 446976
~$PATH:1   = C:\Program Files\7-Zip\7z.exe
~dp1       = C:\Program Files\7-Zip\
~pd1       = C:\Program Files\7-Zip\
~nx1       = 7z.exe
~dp$PATH:1 = C:\Program Files\7-Zip\
~ftza1     = --a------ 10/04/2016 09:51 AM 446976 C:\Program Files\7-Zip\7z.exe

If you run the above batch file with a program file name, if may find the path name for you in the %PATH variable:

C:\fyicenter>Batch-Parameter-Modifiers.bat java.exe

1          = java.exe
~1         = java.exe
~f1        = C:\fyicenter\java.exe
~d1        = C:
~p1        = \fyicenter\
~n1        = java
~x1        = .exe
~s1        = C:\FYICEN~1\java.exe
~a1        =
~t1        =
~z1        =
~$PATH:1   = C:\ProgramData\Oracle\Java\javapath\java.exe
~dp1       = C:\fyicenter\
~pd1       = C:\fyicenter\
~nx1       = java.exe
~dp$PATH:1 = C:\ProgramData\Oracle\Java\javapath\
~ftza1     = C:\fyicenter\java.exe

If you run the above batch file with a text message, the output is not that useful:

C:\fyicenter>Batch-Parameter-Modifiers.bat "Hello world!"

1          = "Hello world!"
~1         = Hello world!
~f1        = C:\fyicenter\Hello world!
~d1        = C:
~p1        = \fyicenter\
~n1        = Hello world!
~x1        =
~s1        = C:\FYICEN~1\Hello world!
~a1        =
~t1        =
~z1        =
~$PATH:1   =
~dp1       = C:\fyicenter\
~pd1       = C:\fyicenter\
~nx1       = Hello world!
~dp$PATH:1 =
~ftza1     = C:\fyicenter\Hello world!

 

What Is "for" Loop Parameter

Get Batch File Path Name Components

Working with Parameters and Variables

⇑⇑ Windows Batch File Tutorials

2017-12-05, 224👍, 0💬