How to create a variable with arguments starting at 2 positions ?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
PiotrMP006
Posts: 31
Joined: 08 Sep 2017 06:10

How to create a variable with arguments starting at 2 positions ?

#1 Post by PiotrMP006 » 01 Sep 2021 04:58

Hi

How to create a variable with arguments starting at 2 positions ?

set arguments==%*

"D:Test\" "Folder1" "Folder2" "Folder3"


I need to create a variable from the 2nd argument

"Folder1" "Folder2" "Folder3"


Please help me

Squashman
Expert
Posts: 4487
Joined: 23 Dec 2011 13:59

Re: How to create a variable with arguments starting at 2 positions ?

#2 Post by Squashman » 01 Sep 2021 08:59

You have several different ways you could do this. Each of them do have some caveats.

Using the quote as a delimiter and assign the second token to the variable.

Code: Select all

@echo off
SET "arguments="
FOR /F TOKENS^=1^*^ delims^=^" %%G IN (%*) DO SET arguments=%%H"
echo arguments=%arguments%
Set a flag to skip the first argument and then assign the remainder to the variable.

Code: Select all

@echo off
setlocal enabledelayedexpansion
set "flag="
SET "arguments="
FOR %%G IN (%*) DO (
	IF DEFINED flag SET arguments=!arguments! %%G
	set "flag=1"
)
endlocal &set "arguments=%arguments%"
echo arguments=%arguments%
Use the shift command and check to see if there is still an argument assigned to %1 and then use a GOTO to loop through all the arguments.

Code: Select all

@echo off
SET "arguments="
:ARGS
SHIFT /1
IF NOT "%~1"=="" (
	SET "arguments=%arguments% %1"
	goto ARGS
)
echo arguments=%arguments%

Aacini
Expert
Posts: 1919
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: How to create a variable with arguments starting at 2 positions ?

#3 Post by Aacini » 01 Sep 2021 13:21

Remove the first argument from all of them:

Code: Select all

@echo off
setlocal EnableDelayedExpansion

set "arguments=%*"
set "arguments=!arguments:*%1 =!"
echo arguments=%arguments%
Antonio

Squashman
Expert
Posts: 4487
Joined: 23 Dec 2011 13:59

Re: How to create a variable with arguments starting at 2 positions ?

#4 Post by Squashman » 01 Sep 2021 14:33

Aacini wrote:
01 Sep 2021 13:21
Remove the first argument from all of them:

Code: Select all

@echo off
setlocal EnableDelayedExpansion

set "arguments=%*"
set "arguments=!arguments:*%1 =!"
echo arguments=%arguments%
Antonio
DUH!!! I totally should have thought of that. Wildcard replacement is NON-GREEDY. That makes total sense!

Post Reply