0

I want to format a path in batch, for example this:

C:\key\aa\bb\cc\key\rem.txt

I want to obtain:

C:\key\aa\bb\cc\key

I have tried:

1. set path=C:\key\aa\bb\cc\key\rem.txt
2. set KEY=key
3. FOR /f "delims=" %%a in ("*\*\!KEY!") do SET delete=!path:%%a=!
4. FOR /f "delims=" %%a in ("\!KEY!!delete!") do SET pts=!path:%%a=!

It doesn't work if I let **!KEY! at line 3. The key before C:\ is my problem if I don't have this word then this works:

1. set path=C:\**dd**\aa\bb\cc\key\rem.txt
2. set KEY=key
3. FOR /f "delims=" %%a in ("*!KEY!") do SET delete=!path:%%a=!
4. FOR /f "delims=" %%a in ("\!KEY!!delete!") do SET pts=!path:%%a=!

How can I obtain the path I want ?

Thanks

1 Answer 1

2

Couldn't you simply do:

set "target=C:\key\aa\bb\cc\key\rem.txt"
set "format="
for /f "delims=" %%a in ("%target%") do (set format="%%~dpa")
Echo %format%

And that would retrieve the direcotry path.

Mona.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.