如果name包含批处理,则重命名文件

时间:2013-07-06 15:50:11

标签: windows file batch-file

我正在尝试根据名称的内容重命名一些文件。 我有4种不同的可能性:

  • 文件名包含“raw.footage”
  • 文件名包含“resized.1080”
  • 文件名包含“resized.720”
  • 文件名包含“converted.avi”

我正在尝试根据他们拥有的名称重命名这些文件。 raw.footage应该重命名为raw,调整大小.1080到1080等。

如果可能的话,我希望每个可能的名称只有一个.bat脚本/文件而不是四个。

这是我到目前为止所做的:

FOR /R %completepath% %%G IN (*.mp4) DO call :process "%%~nG"
GOTO :EOF

:process
SET %name%=%~1
SET chkname=%name:*raw.footage=?%
IF "%chkname:~0,1%"=="?" (
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
SET old=raw.footage
SET new=raw
for /f "tokens=*" %%f in ('dir /b *.mp4') do (
  SET newname=%%f
  SET newname=!newname:%old%=%new%!
  move "%%f" "!newname!"
)
) ELSE (
  GOTO 1080p
)

:1080p

FOR /R %completepath% %%G IN (*.mp4) DO call :process "%%~nG"
GOTO :EOF

:process
SET %name%=%~1
SET chkname=%name:*resized.1080=?%
IF "%chkname:~0,1%"=="?" (
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
SET old=resized.1080
SET new=1080
for /f "tokens=*" %%f in ('dir /b *.mp4') do (
  SET newname=%%f
  SET newname=!newname:%old%=%new%!
  move "%%f" "!newname!"
)
) ELSE (
  GOTO 720
)

虽然使用第一个块(仅包含raw.footage的重命名)可以正常工作,但添加seccond块只会导致命令行在块之间跳转,但文件没有任何反应。

3 个答案:

答案 0 :(得分:1)

@ECHO OFF
SETLOCAL
SET "completepath=."
SET "subfor="
FOR /R %completepath% %%G IN (*.mp4) DO FOR %%s IN (
     raw.footage raw resized.1080 1080 resized.720 720 converted.avi converted) DO (
 SET mp4path=%%~dpG
 IF DEFINED subfor (call :process "%%~nG" "%%s") ELSE (SET "subfor=%%s")
 )
)
GOTO :EOF
:process
SET name=%~1
CALL SET chkname=%%name:%subfor%=%~2%%
IF NOT "%name%"=="%chkname%" ECHO REN "%mp4path%%name%.mp4" "%chkname%.mp4"
SET "subfor="
GOTO :eof

转换字符串(from to)只需成对插入...完成。简单。


针对新要求进行了修订

@ECHO OFF
SETLOCAL
SET "completepath=."
SET "subfor="
FOR /R %completepath% %%G IN (*.mp4 *.wmv) DO FOR %%s IN (
     raw.footage raw resized.1080 1080 resized.720 720 converted.avi converted) DO (
 SET mp4path=%%~dpG
 IF DEFINED subfor (call :process "%%~nG" "%%s" "%%~xG") ELSE (SET "subfor=%%s")
 )
)
GOTO :EOF
:process
SET name=%~1
CALL SET chkname=%%name:%subfor%=%~2%%
IF NOT "%name%"=="%chkname%" ECHO REN "%mp4path%%name%%~3" "%chkname%%~3"
SET "subfor="
GOTO :eof

答案 1 :(得分:1)

我喜欢批它的循环之美:

    @echo off &SETLOCAL ENABLEDELAYEDEXPANSION
    CD /d %startfolder%
    FOR %%i IN (
    raw.footage=raw
    resized.1080=1080
    resized.720=720
    converted.avi=converted
        ) DO (
    SET "oldname=!newname!"
    SET "newname=%%i"
    IF DEFINED oldname (
        FOR /f "delims=" %%a IN ('dir /b/a-d "*!oldname!*"') DO (
            SET "oldfilename=%%~a"
            CALL SET "newfilename=%%oldfilename:!oldname!=!newname!%%"
            ECHO REN "!oldfilename!" "!newfilename!"
            )
        SET newname=&SET oldname=
        )
    )

请注意,这只是为了观看,不是进行投票。

答案 2 :(得分:1)

同一事物的另一个版本(用于比较目的):

@echo off
setlocal EnableDelayedExpansion
set "completepath=."
FOR /R "%completepath%" %%G IN (*.mp4) DO (
   set "name=%%~nxG"
   for %%s in ("raw.footage=raw" "resized.1080=1080" "resized.720=720" "converted.avi=converted") do (
      set "chkname=!name:%%~s!"
      if "!name!" neq "!chkname!" ECHO REN "%%G" "!chkname!"
   )
)