批處理從路徑裡擷取檔案路徑 檔名稱 字尾名

2021-09-26 19:53:27 字數 630 閱讀 4242

今天寫指令碼的時候遇到乙個問題就是從乙個完整的路徑裡擷取路徑、檔名和字尾名的需求,網上找了好久沒找到乙個好的方法,查詢了很多**找到的,我這裡統一去整理下,直接上例子

@echo off

set testpath="d:/demo/xyz/11111111.txt"

call :print %testpath%

:print

rem 獲取到檔案路徑

set file_path=%~dp1

echo %file_path%

rem 獲取到檔名稱

set file_name=%~n1

echo %file_name%

rem 獲取到檔案字尾

set suffix=%~x1

echo %suffix%

rem 擷取倒數第四個開始 擷取四個長度

set specstr=%file_name:~-4,4%

echo %specstr%

pause

執行後的結果:

d:\demo\xyz\

11111111

.txt

1111

press any key to continue . . .

擷取檔案路徑

開發介面時發現通過getpathname 函式得到的路徑是完全路徑,即d 測試 test.txt。如果只想得到檔名,可以用getfilename 函式來獲得,也可使用下列 inputfilepath dlg.getpathname int npos inputfilepath.reversefind...

shell之檔案路徑擷取

最近寫指令碼,需要對指令碼中函式傳遞的路徑引數進行擷取,發現了以下比較好用的方法,記錄下 file dir1 dir2 dir3 my.file.txt 我們可以用 分別替換獲得不同的值 拿掉第一條 及其左邊的字串 dir1 dir2 dir3 my.file.txt 拿掉最後一條 及其左邊的字串 ...

cmd獲取批處理檔案所在路徑

在批處理開頭加入cd d dp0 一行 就真真實實地做到 編寫一次,到處執行 0是批處理檔案本身的路徑,dp進行擴充套件,d向前擴充套件到驅動器,p往後擴充套件到路徑。例如,你的bat檔案在e mybat test.bat,則 0就是e mybat test.bat,dp0是e mybat 另外,i...