Getting substring of a token in for loop?
Of course that set x=%%g
and a substring extraction of x should work, but be aware that if the substring is taken inside a FOR loop, it must be done with ! instead of % (Delayed Expansion):
setlocal EnableDelayedExpansion
for /d %%g in (%windir%\Assembly\gac_msil\*policy*A.D*) do (
set x=%%g
echo !x:~29!
)
On the other hand, if you want to know "How to get the last part (name and extension) of a token in for loop", the answer is: use the ~Name and ~eXtension modifiers in %%g replaceable parameter:
for /d %%g in (%windir%\Assembly\gac_msil\*policy*A.D*) do (
echo %%~NXg
)
A simple
dir /B %windir%\Assembly\gac_msil\*policy*A.D*
should do the trick. If you want to loop over it:
for /f %%g in ('dir /B %windir%\Assembly\gac_msil\*policy*A.D*') do (
echo %%g
)
I recently had a similar question, and I wanted to compile here the 3 working solutions:
Use
EnableDelayedExpansion
in your batch file, as in the accepted answer@echo off setlocal EnableDelayedExpansion for /d %%g in (%windir%\Assembly\gac_msil\*) do ( set x=%%g echo !x:~29! )
Use
for /f
to process the output of adir
commandfor /f "tokens=*" %%g in ('dir /ad /b %windir%\Assembly\gac_msil\*') do @echo %%g
And finally, the optimal solution in this case, using enhanced variable substitution. See the end of the help at
for /?
for more information on this syntax.for /d %%g in (%windir%\Assembly\gac_msil\*) do @echo %%~nxg