サブディレクトリ内のファイルの名前を変更するにはどうすればよいですか?
-
05-07-2019 - |
質問
サブディレクトリ内のファイルの名前をバッチで変更する方法はありますか?
例:
ディレクトリとサブディレクトリがあるフォルダ内の *。html
を *。htm
に名前変更します。
解決
Windowsコマンドプロンプト:(バッチファイル内の場合、%xを%% xに変更します)
for /r %x in (*.html) do ren "%x" *.htm
これは、ファイルの中央の名前を変更する場合にも機能します
for /r %x in (website*.html) do ren "%x" site*.htm
他のヒント
Windowsの場合、これは私が見つけた最良のツールです。
>何でもでき、キッチンシンクが付いています。
Linuxの場合、以前の回答のように、多数のスクリプト言語とシェルが用意されています。
find . -regex ".*html<*>quot; | while read line;
do
A=`basename ${line} | sed 's/html$/htm/g'`;
B=`dirname ${line}`;
mv ${line} "${B}/${A}";
done
Pythonで
import os
target_dir = "."
for path, dirs, files in os.walk(target_dir):
for file in files:
filename, ext = os.path.splitext(file)
new_file = filename + ".htm"
if ext == '.html':
old_filepath = os.path.join(path, file)
new_filepath = os.path.join(path, new_file)
os.rename(old_filepath, new_filepath)
Bashでは、次のことができます。
for x in $(find . -name \*.html); do
mv $x $(echo "$x" | sed 's/\.html$/.htm/')
done
もっとエレガントな方法があると確信していますが、ここに私の頭に浮かんだ最初のものがあります:
for f in $(find . -type f -name '*.html'); do
mv $f $(echo "$f" | sed 's/html$/htm/')
done
forfiles(Windows XPおよび2003以降に付属していると思います)がある場合は、次のコマンドを実行できます。
forfiles / S / M * .HTM / C&quot; cmd / c ren @file * .HTML&quot;
Linuxでは、「 rename 」コマンドを使用して、ファイルの名前を変更できます。バッチ。
In bash use command rename :)
rename 's/\.htm$/.html/' *.htm
# or
find . -name '*.txt' -print0 | xargs -0 rename 's/.txt$/.xml/'
#Obs1: Above I use regex \. --> literal '.' and $ --> end of line
#Obs2: Use find -maxdepht 'value' for determine how recursive is
#Obs3: Use -print0 to avoid 'names spaces asdfa' crash!
Total Commander を使用すると、&amp;そのディレクトリ内のすべてのファイルを選択&amp;サブディレクトリを使用すると、それらに対して任意の総司令官操作を実行できます。その1つは、選択したファイルの名前を複数変更します。
Linux上のAWK。最初のディレクトリの場合、これがあなたの答えです... dir_pathでawkを再帰的に呼び出すことで、おそらくこのawkを以下に記述する別のawkを書き込むことで外挿します...など。
ls dir_path/. | awk -F"." '{print "mv file_name/"<*>" dir_path/"$1".new_extension"}' |csh
Windowsの場合、正規表現ベースの名前変更とDrag&ampを使用して便利なリッテVBScriptソリューションを作成しました;ドロップのサポート。必要に応じて試してください-vbsファイルに入れて、エクスプローラーでフォルダーをドロップします。
Windowsでは、名前の変更はそれでかなり良い仕事。フリーウェアですが、オープンソースではありません。
Windowsでは、 httpsで、オープンソースのシンプルなC# bulk file renamer アプリケーションを見つけることができます。 ://filerenamer.codeplex.com は、単純な Excelファイルで機能します。 このアプリケーションへのソースと宛先の2つの列を含むExcelファイルを指定すると、完了です。
Unixでは、 rnm を使用できます。
rnm -rs '/\.html$/.htm/' -fo -dp -1 *
または
rnm -ns '/n/.htm' -ss '\.html
説明:
-
-ns
:名前文字列(新しい名前)。 / n /
は、拡張子なしのファイル名に展開される名前文字列ルールです。
-
-ss
:検索文字列(正規表現)。一致するファイルを検索します。
-
-rs
: / search_regex / replace_part / modifier
の形式の文字列を置き換えます
-
-fo
:ファイルのみのモード
-
-dp
:ディレクトリの深さ(-1は無制限を意味します)。
-fo -dp -1 *
説明:
-
-ns
:名前文字列(新しい名前)。/ n /
は、拡張子なしのファイル名に展開される名前文字列ルールです。 -
-ss
:検索文字列(正規表現)。一致するファイルを検索します。 -
-rs
:/ search_regex / replace_part / modifier
の形式の文字列を置き換えます
-
-fo
:ファイルのみのモード -
-dp
:ディレクトリの深さ(-1は無制限を意味します)。