Syntax
Syntax

Reputation: 25

Rename Files if name contains with batch

I'm currently trying to rename some Files depending on the contents of their name. I have 4 differen Possibilities:

I'm trying to rename these Files depending on which Name they have. raw.footage should be renamed to raw, resized.1080 to 1080, etc.

If possible i would like to have just one .bat Script/File instead of four for each possible name.

Here's what i've done so far:

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
)

While using the first block, which contains only the Renaming of the raw.footage works fine on it's own, adding the seccond block simply causes the commandline to jump between the blocks but nothing happens to the files.

Upvotes: 1

Views: 2998

Answers (3)

Magoo
Magoo

Reputation: 79982

@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

The conversion-strings (from to) are simply inserted in pairs...done. Simple.


Revised for new requirement

@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

Upvotes: 1

Aacini
Aacini

Reputation: 67206

Another version of the same thing (for comparison purposes):

@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!"
   )
)

Upvotes: 1

Endoro
Endoro

Reputation: 37569

I love batch for the beauty of its loops:

    @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=
        )
    )

Please note, this is just for watching, not for voting.

Upvotes: 1

Related Questions