Batch Rename

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
drgt
Posts: 158
Joined: 21 Sep 2010 02:22
Location: Greece

Batch Rename

#1 Post by drgt » 28 Jun 2012 16:32

Your lights please!

I need to rename lots of files in a Dir in this way: Delete the first 3 characters of the filename, keeping the rest and the extension intact.

I do not think there will be a duplicate name created, but let's keep it in mind so there will be no overwrites!

How can I do it in a batch?

Thanks

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Batch Rename

#2 Post by abc0502 » 28 Jun 2012 17:03

Hi, drgt

Here is a code but I don't know what will happen if there is a duplicates so take a copy of your files first :)

@echo off
cls
setlocal Enabledelayedexpansion
set "loc=C:\dir"

FOR /F %%i in ('dir /B /A:-D "%loc%\*.*"') DO (
set files=%%i
ren %loc%\!files! !files:~3!
)

and change the blue line to your files location "the folder"
Last edited by abc0502 on 28 Jun 2012 17:22, edited 1 time in total.

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Batch Rename

#3 Post by abc0502 » 28 Jun 2012 17:21

I tested in case there was duplicate files it didn't rename the duplicate files so you will have to rename them by yourself

And run the batch only once because every time u run it it will remove the first 3 letters

BUT JUST IN CASE TAKE A BACKUP OF YOUR FILES TILL YOU SEE THE RESULTS

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch Rename

#4 Post by foxidrive » 29 Jun 2012 02:20

Test this on copies of your files. Do not run it twice.

Ensure that your filenames are all longer than 4.3 characters. (EG abcd.txt)

This should rename duplicates with a number on the end - change the path to the location to test the copies of your files.

Code: Select all

@echo off
pushd "C:\dir\to\process" && (
FOR /F %%a in ('dir /B /A:-D *.* ') DO call :next "%%a"
)
popd
)
pause
goto :EOF
:next
set num=0
set "files=%~1"
:loop
set /a num=num+1
if exist "%files:~3%" if exist "%files:~3%.%num%" goto :loop
if %num% EQU 1 if not exist "%files:~3%" set num=
ren "%files%" "%files:~3%.%num%"

Post Reply