Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows command line string parsing: folder and filename in string

Is there a quick way to get the filename and last folder from a full file path (string) in Windows command line?

I would expect for input -> results:

"c:\test\1\2\test.txt" -> "2", "test.txt"  
"c:\test\1\2\3\a.txt" -> "3", "a.txt"  
"c:\test\0\b.txt" -> "0", "b.txt"  
"c:\c.txt" -> "", "c.txt"

I've been banging my head at this using FOR /F but since the full path can be any length, I can't figure it out.

like image 966
Vlad Avatar asked Jan 19 '23 07:01

Vlad


1 Answers

Try this:

for %I in (c:\test\1\2\3\a.txt) do set path=%~pI
for %I in (c:\test\1\2\3\a.txt) do set file=%~nxI
set pth2=%path:~0,-1%
for %I in (%pth2%) do set lastdir=%~nxI
echo %file% %lastdir%

The Windows Command Line Reference is your friend.

like image 146
deStrangis Avatar answered May 20 '23 18:05

deStrangis