extract last subfolder\file of path in file

December 10, 2009 at 10:15:24
Specs: Batch
I have a file that contains a bunch of paths to various files. This is the first line of the file:

EXT\Baseline\01_system\HTC Scroll 2.0.1920.1130.00\files\HTCScroll2.dll\imageinfo.bin

What I would like to do is just truncate it so it is only including the last subfolder:


There are many (913) lines in this file. How to do this using batch? I only want from the first character to the right of the second to last \ to the end of the line to remain. Anyone?

See More: extract last subfolder\file of path in file

Report •

December 10, 2009 at 13:51:12
Give this a go and check the results in "newfile.txt".

setlocal disabledelayedexpansion
for /f "usebackq delims=" %%a in ("your file") do (
    set line=%%a
    setlocal enabledelayedexpansion
    call :rec
goto :eof

for /f "tokens=1-3 delims=\" %%b in ("!line!") do (
    if "%%d"=="" (
        >> "newfile.txt" echo !line!
    ) else (
        set line=!line:*\=!
        goto rec

Batch Variable how to

Report •

December 10, 2009 at 13:56:12
I prefer to have CMD do the work for me.
(for /f "usebackq delims=" %%a in ("a file") do @for %%b in ("%%a\..") do @echo %%~NXb\%%~NXa) > out

Report •

December 10, 2009 at 14:07:27

That's awesome, I didn't even think it would take that text as a path(ext\).

Batch Variable how to

Report •

Related Solutions

December 10, 2009 at 15:08:43
saw one similar to this and managed to find it:
judago wrote good code for accomplishing this, which i took liberty to modify and use here (apologies to Judago if this is wrong thing to do, i'm new to forums)
oops again, i was away from my computer and submitted before i reverted to the thread, so my post is redundant. i like the version razor sent, i can learn from it.

test data, contents of file test:

@ECHO OFF && setlocal enabledelayedexpansion
for /f "tokens=* delims=" %%a in (test) do (
set Endvar=%%~nxa
set half=%%~dpa
if "!half:~-1!"=="\" set half="!half:~0,-1!"
for /f %%b in ("!half!") do (
set startvar=%%~dpb
set nn=%%~nxb
:echo leading: !startvar!
echo trailing: !nn!\!endvar! >> xxx.bat

goto :EOF
trailing: test0\test1.txt
trailing: line2\two.txt
trailing: three.uu\th3
trailing: shortest\line.txt

Report •

December 10, 2009 at 15:27:15
nbrane: apologies to Judago if this is wrong thing to do
Not wrong per se, but when you quote the guy who already posted an answer in the thread, you can assume his expertise is already in play. Unless you just want to post your own version, in which case go nuts.

Report •

December 10, 2009 at 17:19:39
Razor, that is excellent! thank you!

Report •

December 10, 2009 at 22:17:47
Hi nbrane,

I don't mind at all, I know I will be stealing razors trick for my self, with credits if any asks. That's whats good about this place, you get to learn all the little tricks everyone has amassed.

Ask I mentioned above I didn't realize that it's possible to use a relative path of a relative path. Even then razor's code is much more clever. You will learn quickly Razor2.3 is probably the best programmer on this site.

Batch Variable how to

Report •

December 11, 2009 at 05:59:04
Judago: "I will be stealing razors trick"

Too late mate, I already have ;-)

In this thread.

Thanks Razor!

Report •

December 11, 2009 at 06:20:58
Razor, would it be possible to get a breakdown of what your command does? I love the output of it, but I also love to learn.. Thanks!

Report •

December 11, 2009 at 08:07:45
Sure? My only clever insight is that FOR tries to treat every text string as a relative path.
for /f "usebackq delims=" %%a in ("a file") do

Pretty standard FOR ?F statement. I disable tokening (delims=") because your example has spaces in the path, and usebackq was added because it allowed me to type "a file" instead of aFile.

for %%b in ("%%a\..") do

The important part here is the "%%a\..", which means the "parent" of whatever is parsed from the first FOR. In your example, the parent of imageinfo.bin is HTCScroll2.dll.

echo %%~NXb\%%~NXa

Print the "name" of the "parent," a back slash, then the "name" of the "file."

(for /f "usebackq delims=" %%a in ("a file") do 
@for %%b in ("%%a\..") do @echo %%~NXb\%%~NXa) > out

Add @ as needed to suppress unwanted echoing, parenthesis for grouping, and a greater than sign to dump everything into out. An artificial line break added here to make this post easier to read.

Report •

Ask Question