Using batch parameter modifiers to get specific portions or file info from a full filename

From Using batch parameters [microsoft.com]:

Cmd.exe provides the batch parameter expansion variables %0 through %9. When you use batch parameters in a batch file, %0 is replaced by the batch file name, and %1 through %9 are replaced by the corresponding arguments that you type at the command line

These batch parameter modifiers variable arguments are immensely useful.

That document is missing some examples, so I’ve put together a simple table:

%1

Original argument

“C:\Users\DMoore\Documents\Document Name.txt”

%~1

Expands %1 and removes any surrounding quotation marks (“”).

C:\Users\DMoore\Documents\Document Name.txt

%~f1

Expands %1 to a fully qualified path name.

C:\Users\DMoore\Documents\Document Name.txt

%~d1

Expands %1 to a drive letter.

C:

%~p1

Expands %1 to a path.

\Users\DMoore\Documents\

%~n1

Expands %1 to a file name.

Document Name

%~x1

Expands %1 to a file extension.

.txt

%~s1

Expanded path contains short names only.

C:\Users\DMoore\DOCUME~1\DOCUME~1.TXT

%~a1

Expands %1 to file attributes.

–a——

%~t1

Expands %1 to date and time of file.

19/08/2009 02:53 p.m.

%~z1

Expands %1 to size of file. (bytes)

9

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>