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
Batch Rename
Moderator: DosItHelp
Re: Batch Rename
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
and change the blue line to your files location "the folder"
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.
Re: Batch Rename
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
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
Re: Batch Rename
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.
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%"