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
How to create a variable with arguments starting at 2 positions ?
Moderator: DosItHelp
-
- Posts: 31
- Joined: 08 Sep 2017 06:10
Re: How to create a variable with arguments starting at 2 positions ?
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.
Set a flag to skip the first argument and then assign the remainder to the variable.
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.
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%
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%
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 ?
Remove the first argument from all of them:
Antonio
Code: Select all
@echo off
setlocal EnableDelayedExpansion
set "arguments=%*"
set "arguments=!arguments:*%1 =!"
echo arguments=%arguments%
Re: How to create a variable with arguments starting at 2 positions ?
DUH!!! I totally should have thought of that. Wildcard replacement is NON-GREEDY. That makes total sense!Aacini wrote: ↑01 Sep 2021 13:21Remove the first argument from all of them:
AntonioCode: Select all
@echo off setlocal EnableDelayedExpansion set "arguments=%*" set "arguments=!arguments:*%1 =!" echo arguments=%arguments%