1

Here's some code from this great answer which doesn't solve my scenario.

See line 22 where I tried to address this.

@echo off
setlocal ENABLEDELAYEDEXPANSION
rem *** Take the cmd-line, remove all until the first parameter
rem *** Copy cmdcmdline without any modifications, as cmdcmdline has some strange behaviour
set "params=!cmdcmdline!"
set "params=!params:~0,-1!"
set "params=!params:*" =!"
set count=0
rem Split the parameters on spaces but respect the quotes
for %%G IN (!params!) do (
  set /a count+=1
  set "item_!count!=%%~G"
  rem echo !count! %%~G
  )
rem list the parameters
for /L %%n in (1,1,!count!) DO (
  rem Original line, works fine
  rem echo %%n #!item_%%n!#
  rem This works fine with !item_%%~n!
  echo !item_%%~n!
  rem This doesn't when I try to expand n to the path.
  echo !item_%%~pn!
)
pause
REM ** The exit is important, so the cmd.exe doesn't try to execute commands after ampersands
exit

I would ideally like to expand on the !item_%%n! variable to !item_%%~pn! to get the path for example but I cannot figure out how to modify that code to do this.

I thought the item variable held the path and the n variable then counted through them.


I would like to be able to expand to these others, like normal:

%%~A         &rem expands %%A removing any surrounding quotes (")
%%~dA        &rem expands %%A to a drive letter only
%%~fA        &rem expands %%A to a fully qualified path name
%%~pA        &rem expands %%A to a path only
%%~nA        &rem expands %%A to a file name only
%%~xA        &rem expands %%A to a file extension only
%%~sA        &rem expanded path contains short names only
%%~aA        &rem expands %%A to file attributes of file
%%~tA        &rem expands %%A to date/time of file
%%~zA        &rem expands %%A to size of file
%%~dpA       &rem expands %%A to a drive letter and path only
%%~nxA       &rem expands %%A to a file name and extension only
%%~fsA       &rem expands %%A to a full path name with short names only
%%~dp$dir:A  &rem searches the directories listed in the 'dir' environment variable and expands %%A to the fully qualified name of the first one found. If the environment variable name is not defined or the file is not found by the search, then this modifier expands to the empty string
%%~ftzaA     &rem expands %%A to a DIR like output line
Ste
  • 1,729
  • 1
  • 17
  • 27

1 Answers1

3

The expansion of !item_%%n! works in another way.

First the %%n will be expanded to a number.
Then the !item_<number>! will be expanded.

If you want to use the parameter syntax, you have to move the result into a parameter first.

echo !item_%%~n!
FOR /F "delims=" %%I in ("!item_%%n!") DO ECHO path: %%~dpI
jeb
  • 78,592
  • 17
  • 171
  • 225
  • 1
    Actually you used a FOR variable, not a parameter. Both have the same modifier syntax. Obviously a parameter could have been used if `call :routine "!item_%%n!!` was used instead of FOR /F. – dbenham Jan 23 '20 at 22:19
  • @dbenham Nitpicker, but obviously you are right :-) – jeb Jan 23 '20 at 22:26
  • You got my plus one anyway :-) – dbenham Jan 24 '20 at 02:01
  • There's an issue when filenames have `;` at the start. Is there a way to address this in this answer? – Ste Jan 27 '20 at 01:20
  • @Ste The problem here is the `drag&drop` itself. Files with any semicolon are not automatically quoted, therefore the semicolon is handeled as delimiter in the `for %%G IN (!params!)`. You could handle this by a full featured line parser or a little bit replace magic – jeb Jan 27 '20 at 05:35