サブディレクトリ内のファイルの名前をバッチで変更する方法はありますか?
例えば:
ディレクトリとサブディレクトリを持つフォルダーに名前を変更*.html
します。*.htm
サブディレクトリ内のファイルの名前をバッチで変更する方法はありますか?
例えば:
ディレクトリとサブディレクトリを持つフォルダーに名前を変更*.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
find . -regex ".*html$" | while read line;
do
A=`basename ${line} | sed 's/html$/htm/g'`;
B=`dirname ${line}`;
mv ${line} "${B}/${A}";
done
パイソンで
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
forfiles がある場合 (Windows XP および 2003 以降に付属していると思います)、次のコマンドを実行できます。
forfiles /S /M *.HTM /C "cmd /c ren @file *.HTML"
もっとエレガントな方法があると確信していますが、最初に頭に浮かんだのは次のとおりです。
for f in $(find . -type f -name '*.html'); do
mv $f $(echo "$f" | sed 's/html$/htm/')
done
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!
Linux では、' rename ' コマンドを使用して、ファイルの名前をバッチで変更できます。
Unix では、 rnmを使用できます。
rnm -rs '/\.html$/.htm/' -fo -dp -1 *
または
rnm -ns '/n/.htm' -ss '\.html$' -fo -dp -1 *
説明:
-ns
: 名前文字列 (新しい名前)。/n/
拡張子なしのファイル名に展開される名前文字列規則です。-ss
: 検索文字列 (正規表現)。一致するファイルを検索します。-rs
: フォームの文字列を置き換えます/search_regex/replace_part/modifier
-fo
: ファイルのみモード-dp
: ディレクトリの深さ (-1 は無制限を意味します)。Linux 上の AWK。最初のディレクトリについては、これがあなたの答えです... dir_path で awk を再帰的に呼び出すことによって推定します。おそらく、この正確な awk を以下に書き込む別の awk を書くことによって...などです。
ls dir_path/. | awk -F"." '{print "mv file_name/"$0" dir_path/"$1".new_extension"}' |csh