Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Batch: "%~1" works, but "%~*" is a syntax error. How do I find the equivalent command?

Tags:

batch-file

Batch: "%~1" works, but "%~*" is a syntax error.

How do I find the equivalent command?

like image 840
Mario Palumbo Avatar asked Sep 13 '25 22:09

Mario Palumbo


1 Answers

Since %* its batch parameter is a wildcard reference to all the arguments not including %0, you can't use ~ on it, but you can for loop on all arguments and %%~ them, example:

for %%x in (%*) do (
    echo %%~x
)

Also if you need to combine them into single argument you can use setlocal enabledelayedexpansion with this loop:

setlocal enabledelayedexpansion
set args=
for %%x in (%*) do (
  set args=!args! %%~x
)
echo %args:~1%

explain:

  1. !args! is another way to use variable when using setlocal enabledelayedexpansion
  2. %args:~1% remove first space.

And here is example without setlocal enabledelayedexpansion, which does not eat ! symbols from arguments:

set args=
for %%x in (%*) do call :SETARGS %%x
GOTO :END
:SETARGS
set args=%args% %~1
:END
echo %args:~1%
like image 190
BladeMight Avatar answered Sep 16 '25 14:09

BladeMight