Page 1 of 1
How to create a variable with arguments starting at 2 positions ?
Posted: 01 Sep 2021 04:58
by PiotrMP006
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
Re: How to create a variable with arguments starting at 2 positions ?
Posted: 01 Sep 2021 08:59
by Squashman
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%
Re: How to create a variable with arguments starting at 2 positions ?
Posted: 01 Sep 2021 13:21
by Aacini
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
Re: How to create a variable with arguments starting at 2 positions ?
Posted: 01 Sep 2021 14:33
by Squashman
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!