I need help with the following problem:
I want to split a filepath into its components inside the batch-file with CMD-commands.
First of all I determine the path where the batch file is located:
set home=%~dp0 (e.g. C:\SomeFolder\OtherFolder\)
What I need is to extract this string into:
and re-concatenate these components to:
This helps me loop thru the elements of the file-path
set List=!home!
:ProcessListSource
FOR /f "tokens=1* delims=\" %%a IN ("!List!") DO (
if "%%a" NEQ "" (
echo %%a
)
if "%%b" NEQ "" (
set List=%%b
goto :ProcessListSource
)
)
the loop works fine, the components of the file-path echoed correctly. I thought it will be easy to change the echo statement by simple string concatenation
if "%%a" NEQ "" (
set foo=%foo%%%a
set foo=%foo%\\
)
the result is simply sobering. Only the backslashes will be added to the variable. Where is my mistake? Echoing %%a works fine, but in the concatenate-statement seems to be an error. I played around with quotes and '!' but nothing works.
Any help on that is highly appreciated
If you only need to double the backslashes, it's simpler to use a replace.
set "home=%~dp0"
set "foo=%home:\=\\%"
echo %foo%
Your code could also work:
But the key word is here EnableDelayedExpansion
(like every day).
That's the cause why %foo%
doesn't expand as expected.
Add this line after your @echo off
setlocal EnableDelayedExpansion
and change your concatenation block to
if "%%a" NEQ "" (
set foo=!foo!%%a
set foo=!foo!\\
)