0
0

VSCodeでファイル検索がもっと便利になるかもしれないツール

Last updated at Posted at 2024-05-25

VSCodeで「ctrl + F」でファイル検索するとき、
エクスプローラ上でフォルダ展開していないと、
検索したいファイルがどこにあるかわからなくて困った...

image.png

そんなときは、この「すごいツール!」(小並感)を用いてファイル検索をおこなうと、目的のファイルがどこにあるかを調べることができる。

これが「すごいツール!」のソースコード.

"""VSCodeでファイル検索を行う用の検索するモジュール"""
import glob
import os
import sys

from termcolor import colored


def search_file(path: str, target_file_name: str) -> tuple:
    """
    Get the path name of the file name you want to search.
    Args:
        path: Directory of the file you want to search.
        target_file_name: File name you want to search.
    return:
        the full path of the searched file
    """
    file = tuple(filter(lambda files: target_file_name in files,
                        glob.glob(path + '/**', recursive=True)))
    return file


if __name__ == '__main__':
    current_dir = os.getcwd()
    try:
        target_file = input(colored('検索したいファイル名を入力: ', 'light_green'))
        find_file = search_file(current_dir, target_file)
    except (FileNotFoundError, ValueError, TypeError, IndexError) as exc:
        print(colored(f'{exc} が発生.', 'light_red'))
    else:
        if any(find_file):
            print('検索結果:', colored(*find_file, 'light_yellow'))
        else:
            print(colored('要素が見つかりません。', 'light_yellow'))
    finally:
        sys.exit()

  1. 実行方法
    VSCodeを実行中に、「ctrl + @」でターミナルを起動し、可読性を向上に用いているライブラリをインストール。
python -m pip install termcolor
  1. 起動方法
    以下のコマンドを実行。
python searcher.py

image.png

「検索したいファイル名を入力:」に検索したいファイル名を入力し、力強くエンターキーをターンっ!!!
image.png

すると、検索結果が得られ、目的のファイルがどこに存在するかのパスを取得できる。
image.png

この「すごいツール!(小並感)」を使ってより効率的な開発を行うえるようになることでしょう。

おしまい。

0
0
2

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