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:HTCScroll2.dll\imageinfo.binThere 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

#1
December 10, 2009 at 13:51:12
 Give this a go and check the results in "newfile.txt".@ECHO OFF setlocal disabledelayedexpansion for /f "usebackq delims=" %%a in ("your file") do ( set line=%%a setlocal enabledelayedexpansion call :rec endlocal ) endlocal goto :eof :rec 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 •

#2
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 •

#3
December 10, 2009 at 14:07:27
 Razor2.3,That's awesome, I didn't even think it would take that text as a path(ext\).

Report •

Related Solutions

#4
December 10, 2009 at 15:08:43
 saw one similar to this and managed to find it:http://www.computing.net/answers/pr...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. @GOTO STRTtest data, contents of file test:c:\abc\def\hhj\test0\test1.txtc:\def\hhj\line2\two.txtc:\ggg\ppp\abc\def\hhj\three.uu\th3c:\shortest\line.txt:STRT@ECHO OFF && setlocal enabledelayedexpansionfor /f "tokens=* delims=" %%a in (test) do ( set Endvar=%%~nxa set half=%%~dpaif "!half:~-1!"=="\" set half="!half:~0,-1!"for /f %%b in ("!half!") do ( set startvar=%%~dpb set nn=%%~nxb):ex:echo leading: !startvar!echo trailing: !nn!\!endvar! >> xxx.batpause) :exxgoto :EOFtrailing: test0\test1.txt trailing: line2\two.txt trailing: three.uu\th3 trailing: shortest\line.txt

Report •

#5
December 10, 2009 at 15:27:15
 nbrane: apologies to Judago if this is wrong thing to doNot 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 •

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

Report •

#7
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.

Report •

#8
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 •

#9
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 •

#10
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") doPretty 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\%%~NXaPrint 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) > outAdd @ 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 •