vbsでファイルを見つけて、場所のフォルダを開く

vbsでファイルを見つけて、場所のフォルダを開く

VBS (または他のスクリプト) で、各ハード ドライブ パーティションでファイルを検索し、ファイルが見つかった場合はそのファイルの場所のフォルダーを開くスクリプトを作成する方法はありますか? つまり、H: ドライブの H:\stuff\texts\ にあるファイル「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 $_)}}

関連情報