LoginSignup
0
1

More than 5 years have passed since last update.

htaでローカルファイルをインクリメンタルサーチ改

Last updated at Posted at 2016-12-27

何故今更htaなのか

共有されたネットワークドライブのフォルダではファイル名検索が利かない(利かせようとするといろいろ大変な)ので、Electronあたりでなんとかしようとしていたところ、駆逐されたものと思っていたhtaがWindows 10でも健在だったので。

決して「作り捨てにするのが楽だから」とか思ってませんよ?

先行^H駆者発見

多謝。

が、そのままだとせっかく絞り込んでもそのファイルがあるフォルダが開くだけだったりするので、関連付けられているアプリでファイルを開くようにしてみました。
すると、今度は絞り込んだ後の期待したファイルと違うファイルが開かれてしまうトラップが…。

パッチ

--- IncrementalSearch.hta.orig  Tue Dec 27 22:58:53 2016
+++ IncrementalSearch.hta   Wed Dec 28 00:04:11 2016
@@ -6,14 +6,14 @@
     <meta http-equiv="MSThemeCompatible" content="yes">
     <style>
         ul { list-style-type:none; margin:1px; }
-        li { cursor:pointer; width:100%; border:3px double; margin:3px; font-size:12px }
+        li { cursor:pointer; width:100%; margin:3px; font-size:12px }
     </style>
     <script type="text/javascript" src="prototype.js"></script>
     <script type="text/javascript">
-    var targetDir = ".";
+    var targetDir = "."; //検索するパスはここで指定する
     var lastModDate;
     var localFiles;
-    var serchResultFiles;
+    var searchResultFiles;

     onLoad = function( link ) {
         resizeTo( 500, 800 );
@@ -28,10 +28,10 @@
         }
         $( "dir" ).innerHTML = targetDir;
         //ファイル一覧取得
-        searchFlvFiles();
+        searchFiles();
         // 検索
         var keyword = $F( "keyword" );
-        serchResultFiles = localFiles.findAll( function( file ) {
+        searchResultFiles = localFiles.findAll( function( file ) {
             return RegExp( keyword, "i" ).test( file.Name );
         } );
         // リストクリア
@@ -41,9 +41,9 @@
         } );

         // リスト追加
-        serchResultFiles.each( function( file, index ) {
+        searchResultFiles.each( function( file, index ) {
             var li = document.createElement( "li" );
-            li.setAttribute( "onclick", new Function( "openExploler( \'" + index + "\' );" ) );
+            li.setAttribute( "onclick", new Function( "openExplorer( \'" + index + "\' );" ) );
             file.Name.match( /(.*)$/ );
             var text = document.createTextNode( RegExp.$1 );
             li.appendChild( text );
@@ -52,7 +52,7 @@
         });
     }

-    searchFlvFiles = function() {
+    searchFiles = function() {
         var fs = new ActiveXObject( "Scripting.FileSystemObject" );
         var orgfiles = fs.GetFolder( targetDir ).Files;
         var modDate = fs.GetFolder( targetDir ).dateLastModified;
@@ -77,12 +77,12 @@

     execBuildupCommand = function( argument ) {
         var shell = new ActiveXObject( "WScript.Shell" );
-        shell.Run( "explorer.exe /e,/select," + argument , 1, true );
+        shell.Run( argument , 1, true );
         shell = null;
     }

-    openExploler = function( index ) {
-        execBuildupCommand( "\"" + localFiles[index].Path + "\"" );
+    openExplorer = function( index ) {
+        execBuildupCommand( "\"" + searchResultFiles[index].Path + "\"" );
     }

     argsArray = function (strArgs) {

補記

JScriptで再帰ファイル一覧の取得
http://kujirahand.com/blog/index.php?JScript%E3%81%A7%E5%86%8D%E5%B8%B0%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB%E4%B8%80%E8%A6%A7%E3%81%AE%E5%8F%96%E5%BE%97

を組み合わせて、指定フォルダ下から検索なんてのも簡単に実装できますね。
もっともネットワークドライブ上だと、ファイル数が多くなると処理が厳しくなるので事前に一覧を生成しておいて、それを読み込んで表示するとかの手法が必要かもしれません。

あと、shell.Runでパスに空白が含まれる起動するアプリを指定する場合は

"\"C:/Program Files (x86)/~\" "

のように書くのを忘れずに。

0
1
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
1