"2", "te-6ren">
gpt4 book ai didi

Windows命令行字符串解析: folder and filename in string

转载 作者:可可西里 更新时间:2023-11-01 13:52:31 28 4
gpt4 key购买 nike

有没有一种快速的方法可以从 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"

我一直在用 FOR/F 解决这个问题,但由于完整路径可以是任意长度,我无法弄清楚。

最佳答案

试试这个:

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%

Windows Command Line Reference是你的 friend 。

关于Windows命令行字符串解析: folder and filename in string,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6595977/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com