使用 vbs 尋找檔案並開啟位置資料夾

使用 vbs 尋找檔案並開啟位置資料夾

有沒有辦法在vbs(或其他)中製作一個腳本,在每個硬碟分割區中搜尋文件,然後在找到該文件時打開該文件的位置資料夾?因此,如果我想在 H:\stuff\texts\ 中找到位於 H: 磁碟機中的檔案“rand.txt”,程式碼會在 C:,D:,E:,F: 中查找,然後如果找到它在H 中:它繼續開啟資料夾「texts」。我嘗試使用 cmd 但它實際上對我不起作用...

答案1

這是一個 Windows 批次解決方案(可能比較耗時):

@ECHO OFF >NUL
@SETLOCAL enableextensions disabledelayedexpansion
for %%G in (a b c d e f g h i j k l m n o p q r s t u v w x y z) do (
   if exist %%G:\NUL (
       echo %%G: drive
       for /F "tokens=*" %%H in ('where /r %%G:\ rand.txt 2>NUL') do (

         echo %%H
         explorer.exe /e,/select,"%%~fH"

       )
     )
   )
@ENDLOCAL
@GOTO :eof

代替echo %%H你有檔名的完整路徑...

編輯:(where /r %%G:\ rand.txt 2>NUL重要:)如果起始目錄不存在,則2>NUL消除錯誤訊息,如下例所示(片段枚舉):ERROR: The system cannot find the file specifiedINFO: Could not find files for the given pattern(s)

d:\xxx>where /r C:\bat\ randx.txt
ERROR: The system cannot find the file specified.

d:\xxx>echo %errorlevel%
2

d:\xxx>where /r d:\bat\ randx.txt
INFO: Could not find files for the given pattern(s).

d:\xxx>echo %errorlevel%
1

d:\xxx>where /r d:\bat\ rand.txt
d:\bat\files\rand.txt

d:\xxx>echo %errorlevel%
0

d:\xxx>

答案2

這應該在 Powershell 中完成:

gwmi Win32_LogicalDisk | Select-Object -expand DeviceID | %{$drive = $_; $drive; ls "$drive\rand.txt" -recurse | %{ii (Split-Path $_)}}

相關內容