批量重命名diff目录中的一堆文件

时间:2014-01-16 06:17:49

标签: batch-file

我有一个100个左右的子目录,每个子目录中有4或5个文件。例如,其中一个目录包含以下文件:

  • John Matthews ID.jpg
  • 许可证John Matthews.pdf
  • 证书的约翰-Matthews.jpg

如您所见,文件名不是任何统一格式。我想做的是将它们重命名为:

  • John_Matthews_ID.jpg
  • John_Matthews_License.pdf(注意:名称应该先是文档 输入结束)
  • John_Matthews_Certificate.jpg

那么,对于所有100个子目标来说,快速做这件事的最佳方法是什么?

非常感谢。

3 个答案:

答案 0 :(得分:0)

@echo off
setlocal EnableDelayedExpansion

set types=0
for %%t in (ID License Certificate) do (
   set /A types+=1
   set type[!types!]=%%t
)

for /D %%d in (*) do (
   cd "%%d"
   for /F "delims=" %%f in ('dir /A-D /B') do (
      set name=%%~Nf
      for /L %%i in (1,1,%types%) do for /F %%t in ("!type[%%i]!") do (
         if /I "!name:%%t=!" neq "!name!" (
            set "newName=!name:%%t=!"
            ECHO ren "!name!%%~Xf" "!newName: =_!_%%t%%~Xf"
         )
      )
   )
   cd ..
)

此批处理文件需要根据结果报告进行相应调整。 ECHO命令只显示将要执行的REN命令。将批处理文件放在包含100个子目录的文件夹中并运行它。

答案 1 :(得分:0)

编辑:已经过测试。

这是非破坏性的,只会在主文件夹中创建temprename.bat.txt文件,供您检查并查看它是否正在执行您所需的操作。

将其重命名为temprename.bat并运行它以提交更改。

在主文件夹中运行。

@echo off
del "temprename.bat.txt" 2>nul
setlocal enabledelayedexpansion
for /r %%a in (*.jpg *.pdf) do (
set "name=%%~na"
set "newname=!name:.=_!"
if "!newname:~-2!"=="ID" set "newname=!name:~0,-2!_ID"
if "!newname:~0,2!"=="ID" set "newname=!name:~2!_ID"
if not "!newname:License=!"=="!newname!" set "newname=!newname:License=!_License"
if not "!newname:Certificate=!"=="!newname!" set "newname=!newname:Certificate=!_Certificate"
set "newname=!newname: =_!"
set "newname=!newname:-=_!"
set "newname=!newname:__=_!"
set "newname=!newname:__=_!"
if  "!newname:~0,1!"=="_" set "newname=!newname:~1!"
if  "!newname:~0,1!"=="_" set "newname=!newname:~1!"

>>temprename.bat.txt echo ren "%%a" "!newname!%%~xa"
)

答案 2 :(得分:0)

@ECHO OFF
SETLOCAL
SET "sourcedir=c:\sourcedir"
FOR /f "tokens=1*delims=" %%a IN (
 'dir /s /b /a-d "%sourcedir%\*.*" '
 ) DO SET "name=%%~nxa"&SET "directory=%%~dpa"&CALL :process
)

GOTO :EOF
:: Process filename
:process
SET "namepart=%name:ID.jpg=%"
IF "%namepart%ID.jpg"=="%name%" CALL :CHANGE ID.jpg&GOTO :eof
SET "namepart=%name:License=%"
SET "namepart=%namepart:.pdf=%"
IF "License%namepart%.pdf"=="%name%" CALL :CHANGE License.jpg&GOTO :eof
SET "namepart=%name:Certificate=%"
SET "namepart=%namepart:.jpg=%"
IF "Certificate%namepart%.jpg"=="%name%" CALL :CHANGE Certificate.jpg&GOTO :eof
GOTO :eof

:CHANGE
:: Remove leading/trailing (space, dash)
IF "%namepart:~0,1%"==" " set "namepart=%namepart:~1%"
IF "%namepart:~-1%"==" "  set "namepart=%namepart:~0,-1%"
IF "%namepart:~0,1%"=="-" set "namepart=%namepart:~1%"
IF "%namepart:~-1%"=="-"  set "namepart=%namepart:~0,-1%"
:: Now change remaining (space,dash) to underscore
SET "namepart=%namepart: =_%
SET "namepart=%namepart:-=_%
ECHO REN "%directory%%name%" "%namepart% %1"
GOTO :eof

应该做的工作。当然,Meredith-Rebecca Cholmondley-Ffytch女士可能不赞成......

所需命令仅用于ECHO以用于测试目的。在您确认命令正确无误后,将ECHO REN更改为REN以实际重命名文件。


修订版提供了进一步的信息......

@ECHO OFF
SETLOCAL
SET "sourcedir=c:\sourcedir"
FOR /f "tokens=1*delims=" %%a IN (
 'dir /s /b /a-d "%sourcedir%\*" '
 ) DO SET "name=%%~nxa"&SET "directory=%%~dpa"&CALL :process
)

GOTO :EOF
:: Process filename
:process
FOR %%f IN (ID.jpg License.pdf Certificate.jpg) DO (CALL :extractname %%~nf %%~xf)
GOTO :eof

:extractname
CALL SET "namepart=%%name:%1%2=%%"
IF "%namepart%%1%2"=="%name%" CALL :CHANGE %1%2&GOTO :eof
CALL SET "namepart=%%name:%1=%%"
CALL SET "namepart=%%namepart:%2=%%"
IF "%1%namepart%%2"=="%name%" CALL :CHANGE %1%2&GOTO :eof
CALL :length %1
SET /a $p1=%length%
CALL :length %2
CALL SET "namepart=%%name:~%$p1%,-%length%%%"
IF "%1%namepart%%2"=="%name%" CALL :CHANGE %1%2&GOTO :eof
GOTO :eof

:length
SET /a length=0
SET "$1=%1"
:lengthl
IF NOT DEFINED $1 GOTO :EOF
SET /a length+=1
SET "$1=%$1:~1%"
GOTO lengthl

:CHANGE
:: Remove leading/trailing (space, dash)
IF "%namepart:~0,1%"==" " set "namepart=%namepart:~1%"
IF "%namepart:~-1%"==" "  set "namepart=%namepart:~0,-1%"
IF "%namepart:~0,1%"=="-" set "namepart=%namepart:~1%"
IF "%namepart:~-1%"=="-"  set "namepart=%namepart:~0,-1%"
:: Now change remaining (space,dash) to underscore
SET "namepart=%namepart: =_%
SET "namepart=%namepart:-=_%
ECHO REN "%directory%%name%" "%namepart% %1"
GOTO :eof

重新编辑以适应foxidrive的朋友Midge和船员......

测试dirlist:

ABCDEFGH123.pdf
Certificate Ida Buttrose.jpg
Certificate Midge Ridley.jpg
Certificate Stupid Cupid.jpg
Certificate-Ida Buttrose.jpg
Certificate-John-Matthews.jpg
Certificate-Midge Ridley.jpg
ID Ida Buttrose.jpg
ID Midge Ridley.jpg
ID Stupid Cupid.jpg
Ida Buttrose Certificate.jpg
Ida Buttrose ID.jpg
Ida Buttrose License.pdf
John Matthews ID.jpg
License Ida Buttrose.pdf
License John Matthews.pdf
License Midge Ridley.pdf
License Stupid Cupid.pdf
Midge Ridley Certificate.jpg
Midge Ridley ID.jpg
Midge Ridley License.pdf
oldname.zip
Stupid Cupid Certificate.jpg
Stupid Cupid ID.jpg
Stupid Cupid License.pdf

测试结果:

REN "c:\sourcedir\Certificate Ida Buttrose.jpg" "Ida_Buttrose Certificate.jpg"
REN "c:\sourcedir\Certificate Midge Ridley.jpg" "Midge_Ridley Certificate.jpg"
REN "c:\sourcedir\Certificate Stupid Cupid.jpg" "Stupid_Cupid Certificate.jpg"
REN "c:\sourcedir\Certificate-Ida Buttrose.jpg" "Ida_Buttrose Certificate.jpg"
REN "c:\sourcedir\Certificate-John-Matthews.jpg" "John_Matthews Certificate.jpg"
REN "c:\sourcedir\Certificate-Midge Ridley.jpg" "Midge_Ridley Certificate.jpg"
REN "c:\sourcedir\ID Ida Buttrose.jpg" "Ida_Buttrose ID.jpg"
REN "c:\sourcedir\ID Midge Ridley.jpg" "Midge_Ridley ID.jpg"
REN "c:\sourcedir\ID Stupid Cupid.jpg" "Stupid_Cupid ID.jpg"
REN "c:\sourcedir\Ida Buttrose Certificate.jpg" "Ida_Buttrose Certificate.jpg"
REN "c:\sourcedir\Ida Buttrose ID.jpg" "Ida_Buttrose ID.jpg"
REN "c:\sourcedir\Ida Buttrose License.pdf" "Ida_Buttrose License.pdf"
REN "c:\sourcedir\John Matthews ID.jpg" "John_Matthews ID.jpg"
REN "c:\sourcedir\License Ida Buttrose.pdf" "Ida_Buttrose License.pdf"
REN "c:\sourcedir\License John Matthews.pdf" "John_Matthews License.pdf"
REN "c:\sourcedir\License Midge Ridley.pdf" "Midge_Ridley License.pdf"
REN "c:\sourcedir\License Stupid Cupid.pdf" "Stupid_Cupid License.pdf"
REN "c:\sourcedir\Midge Ridley Certificate.jpg" "Midge_Ridley Certificate.jpg"
REN "c:\sourcedir\Midge Ridley ID.jpg" "Midge_Ridley ID.jpg"
REN "c:\sourcedir\Midge Ridley License.pdf" "Midge_Ridley License.pdf"
REN "c:\sourcedir\Stupid Cupid Certificate.jpg" "Stupid_Cupid Certificate.jpg"
REN "c:\sourcedir\Stupid Cupid ID.jpg" "Stupid_Cupid ID.jpg"
REN "c:\sourcedir\Stupid Cupid License.pdf" "Stupid_Cupid License.pdf"