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?

ローカルフォルダを指定してUithub風テキストにしてくれるバッチファイル

Posted at

動機

  • ローカルフォルダでもUithubっぽいのを使いたい
  • バッチファイルをクリックするだけで手軽に使いたい

使い方

  • "LocalUit.bat"ファイルを作成してエディタでソースコードを張り付ける
    • extensions 変数を変更することで、対象ファイルの種類を自由に設定可能なので、各々の作業に合わせて調整する
  • "LocalUit.bat"をクリックして、指示に従ってフォルダを指定する
  • "output.txt"にUithub風テキスト化した結果が入っているので、生成AIへの質問等で活用する

ソースコード(バッチファイル)

@echo off
chcp 65001 >nul
setlocal enabledelayedexpansion

rem 現在のディレクトリを保存
set "currentDir=%CD%"

rem フォルダ名の入力を促す
set /p foldername=フォルダ名を入力してください:

rem output.txtを初期化
echo( > "%currentDir%\output.txt"
echo( >> "%currentDir%\output.txt"

rem フォルダの存在チェック
if not exist "%foldername%" (
    echo 指定されたフォルダは存在しません。>> "%currentDir%\output.txt"
    echo 指定されたフォルダは存在しません。
    pause
    exit /b
)

echo %foldername% >> "%currentDir%\output.txt"

rem 指定されたフォルダのツリー構造をoutput.txtに保存(上位3行をスキップ)
tree "%foldername%" /f /a | more +3 >> "%currentDir%\output.txt"

rem ツリー構造を表示
type "%currentDir%\output.txt"

rem 処理対象の拡張子を管理する変数
set "extensions=txt py java md json"

rem 拡張子ごとにファイルを処理
for %%e in (%extensions%) do (
    rem 拡張子ごとのファイルを再帰的に処理
    for /r "%foldername%" %%f in (*.%%e) do (
        rem 相対パスを取得
        set "fullpath=%%f"
        set "relativePath=!fullpath:%currentDir%\=!"

        rem 出力ファイルにヘッダー情報を追加
        >> "%currentDir%\output.txt" (
            echo -----------------------------------------------------
            echo !relativePath!
            echo -----------------------------------------------------
        )

        rem 表示用
        echo -----------------------------------------------------
        echo !relativePath!
        echo -----------------------------------------------------

        rem 行番号の初期化
        set "counter=1"
        set "separator= | "

        rem ファイルの内容の行番号付き表示
        for /f "usebackq delims=" %%a in ("%%f") do (
            set "line=%%a"
            >> "%currentDir%\output.txt" echo !counter!!separator!!line!
            echo !counter!!separator!!line!
            set /a counter+=1
        )

        >> "%currentDir%\output.txt" echo(
        >> "%currentDir%\output.txt" echo(
        echo(
        echo(
    )
)

rem 元のディレクトリに戻る
cd /d "%currentDir%"

pause

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?