
有沒有辦法找到文件(“example.txt”)的目錄,然後如果找到該文件,在cmd(或vb)上打開目錄(例如“c:\ example \ sub \”)?當然,查看所有分割區而不僅僅是“C:\”。
答案1
從每個磁碟機的根目錄執行以下命令。
for /f "delims=" %a in ('dir /s /b example.txt') do explorer %~dpa
上面的命令將找到所有名為“example.txt”的文件,然後在它們所在的目錄中運行資源管理器。
如果您想使用批次文件,則每個文件都%
需要替換為%%
:
for /f "delims=" %%a in ('dir /s /b example.txt') do explorer %%~dpa
若要取得磁碟機清單:
for /f "skip=1 delims=" %a in ('wmic logicaldisk get caption') do @echo %a
在批次檔中:
for /f "skip=1 delims=" %%a in ('wmic logicaldisk get caption') do @echo %%a
將它們全部放在一個批次檔中:
for /f "skip=1 delims=" %%a in ('wmic logicaldisk get caption') do (
cd %%a
cd \
for /f "delims=" %%b in ('dir /s /b example.txt') do explorer %%~dpb
)
在第一場比賽後停止:
for /f "skip=1 delims=" %%a in ('wmic logicaldisk get caption') do (
cd %%a
cd \
for /f "delims=" %%b in ('dir /s /b example.txt') do (
explorer %%~dpb
exit
)
)
答案2
這將在當前工作目錄和所有子目錄中找到具有給定副檔名的所有檔案:
dir *.cpp *.h *.java /b/s
這將對以“pyth”開頭的檔案執行此操作
dir pyth*
你可以擴展這個例子。
若要開啟檔案位置(即資料夾),您可以cd
在結果中輸入
explorer .
或者
start .
如果您不想使用,cd
則可以將檔案位置傳遞dir
給explorer
orstart
命令。
若要將其調整為每個已安裝磁碟機的 for 循環,請查看此 Stackoverflow 文章:
https://stackoverflow.com/questions/5709189/batch-script-to-find-drive-letter-of-a-mounted-device