Get the last command line argument in a Windows batch file

I need to get the last argument passed to the windows script package, how can I do this?

+6
source share
2 answers

The easiest and possibly most reliable way is to simply use cmd own parsing for arguments and shift until there are more.

Since this destroys the use of %1 , etc., you can do this in a routine:

 @echo off call :lastarg %* echo Last argument: %LAST_ARG% goto :eof :lastarg set "LAST_ARG=%~1" shift if not "%~1"=="" goto lastarg goto :eof 
+7
source

The result will be the number of arguments:

 set count=0 for %%a in (%*) do set /a count+=1 

To get the last argument, you can do

 for %%a in (%*) do set last=%%a 

Note that this will crash if there are unbalanced quotes on the command line - the command line is reprocessed for , and not directly, using the parsing used for %1 , etc.

+8
source

Source: https://habr.com/ru/post/886800/


All Articles