Windows命令行字符串解析:字符串中的文件夹和文件名

Vla*_*lad 4 windows cmd batch-file

有没有一种快速的方法从Windows命令行中的完整文件路径(字符串)获取文件名和最后一个文件夹?

我希望输入 - >结果:

"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"
Run Code Online (Sandbox Code Playgroud)

我一直在使用FOR/F敲打我的头,但由于完整路径可以是任何长度,我无法弄明白.

deS*_*gis 5

试试这个:

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%
Run Code Online (Sandbox Code Playgroud)

Windows命令行参考是你的朋友.