有没有批量重命名子目录中的文件的方法?
例如:
将具有目录和子目录的文件夹中的*.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
for /r %x in (website*.html) do ren "%x" site*.htm
- jonnybotfind . -regex ".*html$" | 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)
如果您有forfiles(它与Windows XP、2003和更新版本一起提供),则可以运行以下命令:
forfiles /S /M *.HTM /C "cmd /c ren @file *.HTML"
for x in $(find . -name \*.html); do
mv $x $(echo "$x" | 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!
我相信有更加优雅的方法,但这是我脑海中第一个想到的:
for f in $(find . -type f -name '*.html'); do
mv $f $(echo "$f" | sed 's/html$/htm/')
done
有一个相当强大的forfiles命令:
forfiles /?
可以让你了解该命令可能具备的功能。在这种情况下,它可以用作:
forfiles /S /M *.html /C "cmd /c rename @file @fname.htm"
在Linux上使用AWK。对于第一个目录,这就是你的答案...通过递归调用dir_path上的awk来推断,也许可以编写另一个awk,它会写下面这个确切的awk...以此类推。
ls dir_path/. | awk -F"." '{print "mv file_name/"$0" dir_path/"$1".new_extension"}' |csh