Question

How could i rename a certain part of all files inside a directory.

Example

[HorribleSubs] File1 [720p].mkv
[HorribleSubs] File2 [1080p].mkv

Must be renamed into

File1.mkv
File2.mkv
Was it helpful?

Solution

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION 
SET "sourcedir=U:\sourcedir"
FOR /f "tokens=2delims=[]" %%a IN (
 'dir /b /a-d "%sourcedir%\[*]*[*].mkv" '
 ) DO (
 SET "newname=%%a"
 ECHO(REN "%sourcedir%\[*]%%a[*].mkv" "!newname:~1,-1!.mkv"
)

GOTO :EOF

This should fix your problem.

The required REN commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO(REN to REN to actually rename the files.

OTHER TIPS

This is a simple batch you can use to rename a single file manually.

@echo off
title Rename Bat
echo This bat must be in the folder that 
echo contains the files to be renamed.
echo Enter File Name
set /p old=
echo Enter New Name
set /p new=
ren "%old%" "%new%"
echo Done
pause

this has a loop to do multiple files manually

@echo off
title Rename Bat
echo This bat must be in the folder that 
echo contains the files to be renamed.
:begin
echo Enter File Name
set /p old=
echo Enter New Name
set /p new=
ren "%old%" "%new%"
echo Done
ping -n 3 127.0.0.1 >NUL
goto begin

its real simple hope it helps. fyi it works on windows XP

place file In directory you wanna rename

@echo off

setlocal enabledelayedexpansion

dir /b ¦ find /v /c "*">filescount.txt

for /f %%f in (filescount.txt) do (
    set /a "nof=%%f-2" 

    for /L %%n in (1 1 !nof!) do (
        for %%a in (*.mkv) do (
            rename "%%a" "file%%n%%~xa"
        )
    )
)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top