如何重命名子目录中的文件?

发布于 2024-07-08 17:51:11 字数 105 浏览 5 评论 0原文

有没有办法批量重命名子目录中的文件?

例如:

在包含目录和子目录的文件夹中将*.html重命名为*.htm

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

深陷 2024-07-15 17:51:11

Windows 命令提示符:(如果在批处理文件内,请将 %x 更改为 %%x)

for /r %x in (*.html) do ren "%x" *.htm

这也适用于重命名文件的中间部分

for /r %x in (website*.html) do ren "%x" site*.htm

Windows command prompt: (If inside a batch file, change %x to %%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
绝情姑娘 2024-07-15 17:51:11

如果你有 forfiles(我认为它是 Windows XP 和 2003 以及更新的东西附带的),你可以运行:

forfiles /S /M *.HTM /C "cmd /c ren @file *.HTML"

If you have forfiles (it comes with Windows XP and 2003 and newer stuff I think) you can run:

forfiles /S /M *.HTM /C "cmd /c ren @file *.HTML"
绳情 2024-07-15 17:51:11
find . -regex ".*html$" | while read line;
 do 
    A=`basename ${line} | sed 's/html$/htm/g'`;
    B=`dirname ${line}`;
    mv ${line} "${B}/${A}";
 done
find . -regex ".*html$" | while read line;
 do 
    A=`basename ${line} | sed 's/html$/htm/g'`;
    B=`dirname ${line}`;
    mv ${line} "${B}/${A}";
 done
黑白记忆 2024-07-15 17:51:11

在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)

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)
风向决定发型 2024-07-15 17:51:11

在 Bash 中,您可以执行以下操作:

for x in $(find . -name \*.html); do
  mv $x $(echo "$x" | sed 's/\.html$/.htm/')
done

In Bash, you could do the following:

for x in $(find . -name \*.html); do
  mv $x $(echo "$x" | sed 's/\.html$/.htm/')
done
灯角 2024-07-15 17:51:11
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!
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!
污味仙女 2024-07-15 17:51:11

我确信有一种更优雅的方式,但这是我脑海中浮现的第一件事:

for f in $(find . -type f -name '*.html'); do 
    mv $f $(echo "$f" | sed 's/html$/htm/')
done

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
装迷糊 2024-07-15 17:51:11

在Linux上,您可以使用'rename'命令批量重命名文件。

On Linux, you may use the 'rename' command to rename files in batch.

烟织青萝梦 2024-07-15 17:51:11

有一个非常强大的 forfiles 命令:

<代码> forfiles /? 向您提示该命令的功能。
在这种情况下,它可以像这样使用:

forfiles /S /M *.html /C "cmd /c rename @file @fname.htm"

there is pretty powerfull forfiles command:

forfiles /? gives u hint of what is possible with the command.
in this case it can be used like:

forfiles /S /M *.html /C "cmd /c rename @file @fname.htm"
墨落画卷 2024-07-15 17:51:11

Linux 上的 AWK。 对于第一个目录,这就是你的答案...通过在 dir_path 上递归调用 awk 进行推断,也许通过编写另一个 awk 在下面写入这个确切的 awk...等等。

ls dir_path/. | awk -F"." '{print "mv file_name/"$0" dir_path/"$1".new_extension"}' |csh

AWK on Linux. For the first directory this is your answer... Extrapolate by recursively calling awk on dir_path perhaps by writing another awk which writes this exact awk below... and so on.

ls dir_path/. | awk -F"." '{print "mv file_name/"$0" dir_path/"$1".new_extension"}' |csh
淡紫姑娘! 2024-07-15 17:51:11

在 Unix 上,您可以使用 rnm

rnm -rs '/\.html$/.htm/' -fo -dp -1 *

rnm -ns '/n/.htm' -ss '\.html

说明:

  1. -ns :名称字符串(新名字)。 /n/ 是扩展为不带扩展名的文件名的名称字符串规则。
  2. -ss :搜索字符串(正则表达式)。 搜索匹配的文件。
  3. -rs :替换 /search_regex/replace_part/modifier 形式的字符串
  4. -fo :仅文件模式
  5. -dp :目录深度(-1表示无限制)。
-fo -dp -1 *

说明:

  1. -ns :名称字符串(新名字)。 /n/ 是扩展为不带扩展名的文件名的名称字符串规则。
  2. -ss :搜索字符串(正则表达式)。 搜索匹配的文件。
  3. -rs :替换 /search_regex/replace_part/modifier 形式的字符串
  4. -fo :仅文件模式
  5. -dp :目录深度(-1表示无限制)。

On Unix, you can use rnm:

rnm -rs '/\.html$/.htm/' -fo -dp -1 *

Or

rnm -ns '/n/.htm' -ss '\.html

Explanation:

  1. -ns : name string (new name). /n/ is a name string rule that expands to the filename without the extension.
  2. -ss : search string (regex). Searches for files with match.
  3. -rs : replace string of the form /search_regex/replace_part/modifier
  4. -fo : file only mode
  5. -dp : depth of directory (-1 means unlimited).
-fo -dp -1 *

Explanation:

  1. -ns : name string (new name). /n/ is a name string rule that expands to the filename without the extension.
  2. -ss : search string (regex). Searches for files with match.
  3. -rs : replace string of the form /search_regex/replace_part/modifier
  4. -fo : file only mode
  5. -dp : depth of directory (-1 means unlimited).
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文