从string-path获取文件名?

aja*_*221 28 string filenames batch-file

如何从此字符串中获取文件名?

"C:\Documents and Settings\Usuario\Escritorio\hello\test.txt"
Run Code Online (Sandbox Code Playgroud)

输出:

"test.txt"
Run Code Online (Sandbox Code Playgroud)

我真的试图在发布之前找到这个,但所有结果都被污染了,他们谈论从当前目录获取文件名(我必须只使用字符串)

dbe*_*ham 59

方法1

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

键入HELP FOR更多信息.

方法2

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

:sub
echo %~nx1
exit /b
Run Code Online (Sandbox Code Playgroud)

键入HELP CALL更多信息.

  • 谢谢,我在这里找到了更多详细信息http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/percent.mspx?mfr=true (3认同)

sto*_*war 11

如果你的路径是一个参数,只需使用:

%~nx1(1表示第一个参数,我们认为它是第一个参数)

echo%~nx1将直接返回test.txt


Mar*_*arc 6

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

FOR /R c:\temp %i in (*.*) do echo %~nxi
Run Code Online (Sandbox Code Playgroud)