windows 如何重命名子目录中的文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/245840/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I rename files in sub directories?
提问by Shoban
Is there any way of batch renaming files in sub directories?
有没有办法批量重命名子目录中的文件?
For example:
例如:
Rename *.html
to *.htm
in a folder which has directories and sub directories.
重命名*.html
为*.htm
具有目录和子目录的文件夹。
回答by Anonymous
Windows command prompt: (If inside a batch file, change %x to %%x)
Windows 命令提示符:(如果在批处理文件中,请将 %x 更改为 %%x)
for /r %x in (*.html) do ren "%x" *.htm
This also works for renaming the middle of the files
这也适用于重命名文件的中间
for /r %x in (website*.html) do ren "%x" site*.htm
回答by moogs
For windows, this is the best tool I've found:
对于 Windows,这是我发现的最好的工具:
It can do anything AND has the kitchen sink with it.
它可以做任何事情并且有厨房水槽。
For Linux, you have a plethora of scripting languages and shells to help you, like the previous answers.
对于 Linux,您有大量脚本语言和 shell 来帮助您,就像之前的答案一样。
回答by Aditya Mukherji
find . -regex ".*html$" | while read line;
do
A=`basename ${line} | sed 's/html$/htm/g'`;
B=`dirname ${line}`;
mv ${line} "${B}/${A}";
done
回答by monkut
In 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)
回答by Adam Rosenfield
In Bash, you could do the following:
在 Bash 中,您可以执行以下操作:
for x in $(find . -name \*.html); do
mv $x $(echo "$x" | sed 's/\.html$/.htm/')
done
回答by albertb
I'm sure there's a more elegant way, but here's the first thing that popped in my head:
我确信有一种更优雅的方式,但这是我脑海中浮现的第一件事:
for f in $(find . -type f -name '*.html'); do
mv $f $(echo "$f" | sed 's/html$/htm/')
done
回答by BBX
If you have forfiles (it comes with Windows XP and 2003 and newer stuff I think) you can run:
如果您有 forfiles(我认为它带有 Windows XP 和 2003 以及更新的东西),您可以运行:
forfiles /S /M *.HTM /C "cmd /c ren @file *.HTML"
forfiles /S /M *.HTM /C "cmd /c ren @file *.HTML"
回答by nightryde
Total Commanderwhich is a file manager app, lets you list & select all files within its dir & sub-dirs, then you can run any of the total commander operations on them. one of them being: multi-rename the selected files.
Total Commander是一个文件管理器应用程序,可让您列出和选择其目录和子目录中的所有文件,然后您可以对它们运行任何Total Commander操作。其中之一是:多次重命名所选文件。
回答by nightryde
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!