簡體   English   中英

從字符串路徑獲取文件名?

[英]Get filename from string-path?

我如何從這個字符串中獲取文件名?

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

輸出:

"test.txt"

我真的試圖在發布之前找到這個,但所有結果都被污染了,他們談論從當前目錄獲取文件名(我必須只使用字符串)

方法一

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

輸入HELP FOR了解更多信息。

方法二

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

:sub
echo %~nx1
exit /b

輸入HELP CALL了解更多信息。

如果您的路徑作為參數出現,只需使用:

%~nx1 (1 是第一個參數,我們假設它是第一個)

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