batチャレンジ! フォルダ内のファイル名から、文字列変換テキスト作成
ファイルをドラッグ&ドロップしたらリスト取得、のbatはよくあるんですが・・・
「フォルダ(ディレクトリ)」をドラッグ&ドロップして、求める結果が得られなかったので、今回自分が調べた内容を記録に残します。
<目的>
フォルダをドラッグ&ドロップ(以降D&D)して、そのフォルダ内のファイルをリストにしてテキストファイルに書き出す。
調べてすぐ出てくるのは、D&Dしたフォルダ・ファイルのカレントディレクトリのリストが取得されるっていうこと。
中身(下位層)のリストが取得できない!
↓
シンプルなこういうの
・・・
で作ったのがこれ。
使い方は、
以下コマンドをbatにして保存。
フォルダをD&Dすると、batと同じフォルダにtxtファイルが保存されます。
使い方は、
以下コマンドをbatにして保存。
フォルダをD&Dすると、batと同じフォルダにtxtファイルが保存されます。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@echo off | |
rem フォルダをbatにD&Dしてフォルダ内のリスト取得 | |
rem コマンドプロンプトに表示 | |
rem forfiles /p %~p1 /c "cmd /c echo @fname @ext" | |
rem テキストファイルに書き出し(batのある場所にtmp.txt一時作成) | |
forfiles /p %1 /c "cmd /c echo @fname@ext>> %~p0\tmp0.txt" | |
setlocal enabledelayedexpansion | |
for /f "delims=" %%a in (%~p0\tmp0.txt) do ( | |
set line0=%%a | |
set line1=!line0:""=.! | |
set line1=!line1:"=! | |
echo !line1!>>%~p0\tmp1.txt | |
) | |
rem type %~p0\tmp0.txt > %~p0\rawlist_%~n1.txt | |
type %~p0\tmp1.txt > %~p0\FILELIST01_%~n1.txt | |
del %~p0\tmp0.txt | |
del %~p0\tmp1.txt | |
endlocal |
@echo off
rem フォルダをbatにD&Dしてフォルダ内のリスト取得
rem コマンドプロンプトに表示
rem forfiles /p %~p1 /c "cmd /c echo @fname @ext"
rem テキストファイルに書き出し(batのある場所にtmp.txt一時作成)
forfiles /p %1 /c "cmd /c echo @fname@ext>> %~p0\tmp0.txt"
setlocal enabledelayedexpansion
for /f "delims=" %%a in (%~p0\tmp0.txt) do (
set line0=%%a
set line1=!line0:""=.!
set line1=!line1:"=!
echo !line1!>>%~p0\tmp1.txt
)
rem type %~p0\tmp0.txt > %~p0\rawlist_%~n1.txt
type %~p0\tmp1.txt > %~p0\FILELIST01_%~n1.txt
del %~p0\tmp0.txt
del %~p0\tmp1.txt
endlocal
---------------
以上。
D&Dするフォルダと同じドライブにbatを保存して使用してください!
<参考サイト>
↑
↓
--------------------------------------------
ちなみに、かなり特殊だけどカスタマイズしたのがこちら。
【テキスト1】ファイル名(拡張子なし)取得して、前後に文字列挿入&連番
【テキスト1】ファイルのフルパス名取得して、前後に文字列挿入&連番
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@echo off | |
rem フォルダをbatにD&Dしてフォルダ内のリスト取得 | |
rem フォルダ内リストをテキストで一時取得(拡張子なしのファイル名、フルパス)batのある場所に | |
forfiles /p %1 /c "cmd /c echo @path>> %~p0\tmp0.txt" | |
forfiles /p %1 /c "cmd /c echo @fname>> %~p0\tmp1.txt" | |
setlocal enabledelayedexpansion | |
rem ↑で取得した生データから、ダブルクォーテーションを削除 | |
for /f "delims=" %%a in (%~p0\tmp0.txt) do ( | |
set line0=%%a | |
set line1=!line0:""=.! | |
set line1=!line1:"=! | |
echo !line1!>>%~p0\tmp0_1.txt | |
) | |
for /f "delims=" %%a in (%~p0\tmp1.txt) do ( | |
set line0=%%a | |
set line1=!line0:""=.! | |
set line1=!line1:"=! | |
echo !line1!>>%~p0\tmp1_1.txt | |
) | |
type %~p0\tmp1_1.txt > %~p0\FILELIST01_%~n1.txt | |
rem 【テキスト1】リストの先頭と末尾に文字列追加したtxt作成 | |
set PRE0=name.eq( | |
set PRE1=).text(" | |
set SUF=") | |
set LINE = | |
set cnt0 =0 | |
for /f "delims=" %%a in (%~p0\tmp1_1.txt) do ( | |
set LINE=!PRE0!!cnt0!!PRE1!%%a!SUF! | |
echo !LINE! >> %~p0\tmp2.txt | |
set /a cnt0 = !cnt0!+1 | |
) | |
type %~p0\tmp2.txt > %~p0\FILELIST01_%~n1_1.txt | |
rem 【テキスト2】フルパスの加工txt作成 | |
set PRE0=tnl.eq( | |
set PRE1=).append('<img src=" | |
set SUF=">'); | |
set LINE = | |
set cnt1 =0 | |
for /f "delims=" %%a in (%~p0\tmp0_1.txt) do ( | |
set LINE=!PRE0!!cnt1!!PRE1!%%a!SUF! | |
echo !LINE! >> %~p0\tmp3.txt | |
set /a cnt1 = !cnt1!+1 | |
) | |
type %~p0\tmp3.txt > %~p0\FILELIST01_%~n1_2.txt | |
del %~p0\tmp0.txt | |
del %~p0\tmp0_1.txt | |
del %~p0\tmp1.txt | |
del %~p0\tmp1_1.txt | |
del %~p0\tmp2.txt | |
del %~p0\tmp3.txt | |
endlocal |
0 コメント:
コメントを投稿