Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

batch parameters: everything after %1

Duplicate:

  • Is there a way to indicate the last n parameters in a batch file?
  • how to get batch file parameters from Nth position on?

Clarification: I knew of the looping approach - this worked even before Command Extensions; I was hoping for something fun and undocumented like %~*1 or whatever - just like those documented at http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/percent.mspx?mfr=true.


In a Windows batch file (with the so called "Command Extensions" on), %1 is the first argument, %2 is the second, etc. %* is all arguments concatenated.

My question: is there a way to get everything AFTER %2, for example?

I couldn't find such a thing, and it would be helpful for something I'm working on.

like image 456
noamtm Avatar asked Jun 01 '09 16:06

noamtm


People also ask

What does %1 do in batch?

When used in a command line, script, or batch file, %1 is used to represent a variable or matched string. For example, in a Microsoft batch file, %1 can print what is entered after the batch file name.

How many parameters can be passed to a batch file?

There is no practical limit to the number of parameters you can pass to a batch file, but you can only address parameter 0 (%0 - The batch file name) through parameter 9 (%9).

What does @echo off do in a batch file?

Example# @echo off prevents the prompt and contents of the batch file from being displayed, so that only the output is visible. The @ makes the output of the echo off command hidden as well.

What is %% g'in batch file?

%%parameter : A replaceable parameter: in a batch file use %%G (on the command line %G) FOR /F processing of a command consists of reading the output from the command one line at a time and then breaking the line up into individual items of data or 'tokens'.


1 Answers

There is a shorter solution (one-liner) utilizing the tokenization capabilities of for loops:

:: all_but_first.bat echo all: %* for /f "tokens=1,* delims= " %%a in ("%*") do set ALL_BUT_FIRST=%%b echo all but first: %ALL_BUT_FIRST% 

output:

> all_but_first.bat foo bar baz all: foo bar baz all but first: bar baz 

Footnote: Yes, this solution has issues. Same as pretty much anything written with batch files. It's 2021. Use Powershell or literally any other actual scripting language.

like image 83
Max Truxa Avatar answered Oct 03 '22 04:10

Max Truxa