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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With