string 从字符串路径获取文件名?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10393248/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get filename from string-path?
提问by ajax333221
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)
我真的试图在发布之前找到这个,但所有结果都被污染了,他们谈论从当前目录获取文件名(我必须只使用字符串)
回答by dbenham
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
以获取更多信息。
回答by stormofwar
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
回答by Marc
Assuming you need the the names of files under the "c:\temp" directory tree (including sub-directories):
假设您需要“c:\temp”目录树(包括子目录)下的文件名:
FOR /R c:\temp %i in (*.*) do echo %~nxi