Batch rename file extensions, including subdirecto

2019-04-19 08:45发布

I'm renaming empty file extensions with this command:

rename *. *.bla

However, I have a folder with hundreds of such subfolders, and this command requires me to manually navigate to each subfolder and run it.

Is there a command that I can run from just one upper level folder that will include all the files in the subfolders?

7条回答
戒情不戒烟
2楼-- · 2019-04-19 08:52
@echo off
for /f "tokens=* delims= " %%i in ('dir /b/s/A-d') DO (
  if "%%~xi" == "" rename "%%~fi" "%%~ni.bla"
)

Thanks @Wadih M. Find and rename files with no extension?

查看更多
小情绪 Triste *
3楼-- · 2019-04-19 08:53

this will allow you to enter dirs with spaces in the names. (note the double % is for batch files, use a single % for command lines.)

 for /f "tokens=* delims= " %%a in ('dir /b /ad /s') do rename "%%a\*." "*.bla"
查看更多
做个烂人
4楼-- · 2019-04-19 08:55

Using find and xargs and basename would make the expression easier to read

Prepare a rename shell script like this (corrected to handle multiple files)

#!/bin/sh
if [ $# -lt 3 ] 
then
    echo "usage: rename.sh sufold sufnew file1 (file2 file3 ...) "
    exit
fi

sufold=$1
sufnew=$2
shift
shift

for f in $*
do
  echo "to mv " $f `dirname $f`"/"`basename $f $sufold`$sufnew
  mv $f `dirname $f`"/"`basename $f $sufold`$sufnew
done

Then you could invoke that to each file matched your criteria, including recursive folder search by find command

find . -name "*." | xargs rename.sh "." ".bla"

A more common sample

find . -name "*.htm" | xargs rename.sh ".htm" ".html"
查看更多
一纸荒年 Trace。
5楼-- · 2019-04-19 08:58

Try this

for /R c:\temp %I in (*. ) DO Echo rename %I "%~nI.bla"
查看更多
男人必须洒脱
6楼-- · 2019-04-19 09:03
for /r c:\path\ %G in (*.) do ren "%G" *.bla
查看更多
Explosion°爆炸
7楼-- · 2019-04-19 09:05

You can use for to iterate over subdirectories:

for /d %x in (*) do pushd %x & ren *. *.bla & popd

When using it from a batch file you would need to double the % signs:

for /d %%x in (*) do pushd %%x & ren *. *.bla & popd
查看更多
登录 后发表回答