0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ファイルのフルパスを素早く検索する【Windows】

Last updated at Posted at 2025-01-28

作成の経緯

元々は以下を作成した際に一意に絞れないものが出てきたことが作成の経緯です。

怪我の功名で、「普通のExplorerからの検索より早くね?」となり、体裁を整えていった次第です。通常のExplorerの場合、ファイル内部の文字列まで検索しているので、かなり時間がかかるようで、環境にもよるかとは思いますが、自分が試した環境では5倍以上の速度で検索ができました。

検索スクリプト

PowerShellを用いた検索です。
検索を始めるパスを対話式にすると若干の手間があるので、デフォルト値を入れて必要に応じてデフォルト値を書き換える形で作成します。
コピペで実行できると思いますが、デフォルト部分は書き換え推奨です。

#検索を始めるディレクトリのデフォルト値の指定(書き換え可能)
$StartPath = "c:/"

#デフォルトの書き換えの有無チェック
$Check = Read-Host "「${StartPath}」配下を再帰的に検索します`n y or n"

#書き換える場合
if($Check -match "n"){
$StartPath = Read-Host "検索を始めるディレクトリのフルパスを記載"
}

#検索する文字列を対話式で入力
$SearchSTR = Read-Host "検索を始めるディレクトリのフルパスを記載"

#検索した結果を変数に格納
$Result = Get-ChildItem -Path ${StartPath} -Filter "*${SearchSTR}*" -Recurse 2>$null `
|Convert-Path

#変数にヘッダをつけて表示
echo $Result |% -b{$count = 1} -p{"[0] [1]" -f $count++,$_}

#複数パターンあるので、検索コマンドをfunction化する
function PSFileSearch{
Start-Process -FilePath "c:/windows/Explorer.exe" -ArgumentList "${Result}"
}

#検索結果による返答
switch($Result.Length){
0 {
 echo 検索はヒットしませんでした。
 pause
 exit
 }
1 {
 PSFileSearch
 ;exit
 }
}
$i = Read-Host "何行目を検索しますか"
$i = $i -1
$Result = $Result[$i]
PSFileSearch

検索時の-Filterの後ろは前後に*をつけることで完全一致ではなく部分一致での検索が可能になります。
$iの数字を引いているのは最初の行が0行目扱いであるためです。

おわりに

今回はすぐに対象を選んでエクスプローラーで開く処理にしましたが、テキストに出力してゆっくり精査するような形にしても良いかと思います。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?