简体   繁体   English

从字符串路径获取文件名?

[英]Get filename from string-path?

How do I get the filename from this string?我如何从这个字符串中获取文件名?

"C:\Documents and Settings\Usuario\Escritorio\hello\test.txt"

output:输出:

"test.txt"

I really tried to find this one before posting, but all the results were contaminated, they talk about getting filenames from current dir (I must work with strings only)我真的试图在发布之前找到这个,但所有结果都被污染了,他们谈论从当前目录获取文件名(我必须只使用字符串)

Method 1方法一

for %%F in ("C:\Documents and Settings\Usuario\Escritorio\hello\test.txt") do echo %%~nxF

Type HELP FOR for more info.输入HELP FOR了解更多信息。

Method 2方法二

call :sub "C:\Documents and Settings\Usuario\Escritorio\hello\test.txt"
exit /b

:sub
echo %~nx1
exit /b

Type HELP CALL for more info.输入HELP CALL了解更多信息。

if your path comes as a parameter, just use:如果您的路径作为参数出现,只需使用:

%~nx1 (1 is for the first param and we suppose it's the first one) %~nx1 (1 是第一个参数,我们假设它是第一个)

echo %~nx1 would return directly test.txt echo %~nx1会直接返回 test.txt

假设您需要“c:\\temp”目录树(包括子目录)下的文件名:

FOR /R c:\temp %i in (*.*) do echo %~nxi

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM