Related: How to parse a batch parameter A. When you call a batch file, you can enter data after the command that the batch file refers to as %1, %2, etc. For example, in the batch file hello.bat, the following command @echo hello %1 boy would output hello john boy if you called it as hello john The following table outlines how you can modify the passed parameter.
The following table shows how you can combine some of the parameters.
To see all the parameters in action, put them into the batch file testing.bat, as follows. @echo off<br> echo fully qualified name %~f1<br> echo drive %~d1<br> echo path %~p1<br> echo filename %~n1<br> echo file extension %~x1<br> echo short filename %~sn1<br> echo short file extension %~sx1<br> echo drive and directory %~dp1<br> echo filename and extension %~nx1 Then, run the file with a long filename. For example, the batch file run on the file c:\temp\longfilename.long would produce the following output. fully qualified name c:\TEMP\longfilename.long<br> drive c:<br> path \TEMP\<br> filename longfilename<br> file extension .long<br> short filename LONGFI~1<br> short file extension .LON<br> drive and directory c:\TEMP\<br> filename and extension longfilename.long This method also works on the second and subsequent parameters. You simply substitute the parameter for 1 (e.g., %~f2 for the second parameter’s fully qualified path name). Related: Using URLs in Batch Files The %0 parameter in a batch file holds information about the file when it runs and indicates which command extensions you can use with the file (e.g., %~dp0 gives the batch file’s drive and path). Learn more: How many parameters can I pass to batch file? |
|