Felis
Felis

Reputation: 23

Script to batch rename to retain only some characters of original filename?

I need to rename thousands of rar files with original filenames of variable sizes. I must make them 10 characters long by keeping the first 3 and the last 4 characters of the original filename and adding in the middle 3 random characters [numbers].

Example:

input:

"John Doe - Jane Doe - 19073275.rar"

"XXXX - XYXY- 98705674.rar

output:

"Joh1273275.rar"

"XXX9795674.rar"

Next, the .bat should generate a .txt with the original name and the modified one underneath for each file!

I know it's possible but I'm completely stupid when it comes to writing it. Please help!

Upvotes: 2

Views: 2950

Answers (2)

Aacini
Aacini

Reputation: 67296

The Batch file below do what you want:

@echo off
setlocal EnableDelayedExpansion
for %%a in (*.rar) do (
   set name=%%~Na
   set num=00!random!
   set newName=!name:~0,3!!num:~-3!!name:~-4!
   ren "%%a" "!newName!%%~Xa"
   echo "%%a" modified to "!newName!%%~Xa" >> log.txt
)

Upvotes: 2

vonbrand
vonbrand

Reputation: 11831

I'd write a script to generate the names in any simple way (say first 6 + last 4), and then check for any duplicates to be cleaned up by hand (or a second pass shifting the middle, or ...). Unless this is a repetitive job (do it daily), it isn't worth fully automatizing.

Upvotes: 0

Related Questions