How do I rename files in sub directories?

萝らか妹 提交于 2019-12-17 15:23:49

问题


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.


回答1:


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



回答2:


For windows, this is the best tool I've found:

http://www.1-4a.com/rename/

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.




回答3:


find . -regex ".*html$" | while read line;
 do 
    A=`basename ${line} | sed 's/html$/htm/g'`;
    B=`dirname ${line}`;
    mv ${line} "${B}/${A}";
 done



回答4:


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)



回答5:


In Bash, you could do the following:

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



回答6:


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



回答7:


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"




回答8:


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




回答9:


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!



回答10:


Total Commander which 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.




回答11:


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



回答12:


For Windows, I've made a convenient litte VBScript solution with regex-based renaming and Drag&Drop support. Give it a try if you like - put it in a vbs file and drop your folder on it in Explorer.




回答13:


On Windows, The Rename does a pretty good job at that. Freeware, but not open source.




回答14:


On Windows, you can find out opensource simple C# bulk file renamer application in https://filerenamer.codeplex.com works with a simple excel file. Give an excel file with two columns source and destination to this application and it's done.




回答15:


On Unix, you can use rnm:

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

Or

rnm -ns '/n/.htm' -ss '\.html$' -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).


来源:https://stackoverflow.com/questions/245840/how-do-i-rename-files-in-sub-directories

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!