×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Batch rename files with part of original file name
4

Batch rename files with part of original file name

Batch rename files with part of original file name

(OP)
Hello All,
I don't know if there is a program out there that can do this. Not sure if it even can really be done without coding one myself. I have around 6k PDF files that fall in this naming convention: CompanyName_Cust#_State_Vendor#_3or4digitDate.pdf
I need it to be simply: Cust#.pdf. The Cust# is always a 6 digit number but obviously unique to each customer. Unfortunately the Vendor# is our Vendor# which is also 6 digits but is one static number.

Anyone have any great ideas before I tell Accounting they are up a creek? If you could delete everything prior and including the first _, then do a delete everything after the first _, that would work.......

Learning - A never ending quest for knowledge usually attained by being thrown in a situation and told to fix it NOW.

RE: Batch rename files with part of original file name

I'm sure coding would be possible, but the Bulk Rename Utility is free and can do the job.
You can find it at this link : https://www.bulkrenameutility.co.uk/

I've got nothing to hide, and I demand that you justify what right you have to ask.

RE: Batch rename files with part of original file name

Are those files all in one folder? Or spread all over some drive(s?) in different folders?

If you have them all in one place, let's say in C:\Test folder, you can simply rename all of them in Excel's VBA:

CODE

Option Explicit

Sub RenameTheFiles()
Const strPATH As String = "C:\Test\"
Dim strFName As String

strFName = Dir(strPATH)

Do While Len(strFName) > 0
    Debug.Print "Rename; " & strPATH & strFName & " As " & strPATH & Split(strFName, "_")(1) & ".pdf"
    Name strPATH & strFName As strPATH & Split(strFName, "_")(1) & ".pdf"
    strFName = Dir
Loop

End Sub 

Copy a few of your files to one folder and try it first before you go nuts with all of them.

---- Andy

"Hmm...they have the internet on computers now"--Homer Simpson

RE: Batch rename files with part of original file name

(OP)
@Andrzejek - Your script worked flawlessly in testing but I did realize another issue. There are some files that have the same Customer # but different 3or4digitDate in the name. This script would need to either append a "_(x+1)" or something for it not to error out. It does exactly as I described above as I didn't realize this was an issue, so thank you very much for coding as it does what it was designed for.

@pmonett - Bulk Rename Utility did as advertised. Thanks again.

Learning - A never ending quest for knowledge usually attained by being thrown in a situation and told to fix it NOW.

RE: Batch rename files with part of original file name

Do you want me to modify the code to include 3or4digitDate in the new name of the file if Cust# already exists?

---- Andy

"Hmm...they have the internet on computers now"--Homer Simpson

RE: Batch rename files with part of original file name

(OP)
@Andrzejek - That would be huge but please don't inconvenience yourself.

Learning - A never ending quest for knowledge usually attained by being thrown in a situation and told to fix it NOW.

RE: Batch rename files with part of original file name

3
Give that a spin....

CODE

Option Explicit

Sub RenameTheFiles()
Const strPATH As String = "C:\Test\"
Dim strFName As String
Dim strNewName As String

On Error GoTo DrB0b

strFName = Dir(strPATH)

Do While Len(strFName) > 0
    strNewName = strPATH & Split(strFName, "_")(1) & ".pdf"
    Debug.Print "Rename; " & strPATH & strFName & " As " & strNewName
    Name strPATH & strFName As strNewName
    strFName = Dir
Loop

Exit Sub
DrB0b:

If Err.Number = 58 Then
    'The file exists, add another piece from orginal file name
    strNewName = strPATH & Split(strFName, "_")(1) & "_" & Split(strFName, "_")(4) & ".pdf"
    Resume
End If

End Sub 

Hopefully, Cust# and 3or4digitDate will make the files unique...
Inconvenience? No. I lOVE problems like this pc1

---- Andy

"Hmm...they have the internet on computers now"--Homer Simpson

RE: Batch rename files with part of original file name

"It does exactly as I described above"
Unfortunately, (or fortunately...?) I work like a computer - you get exactly what you asked for. Which could be (in many cases) not what you really wanted or needed.
wiggle

---- Andy

"Hmm...they have the internet on computers now"--Homer Simpson

RE: Batch rename files with part of original file name

(OP)
@Andrzejek - 99.99% perfection. I did have to remove the last & ".pdf" from the last If statement as it was appending .pdf.pdf to files that matched that criteria. You are a king among men and I thank you for your time and effort. This is why I love this forum and have for years.

Learning - A never ending quest for knowledge usually attained by being thrown in a situation and told to fix it NOW.

RE: Batch rename files with part of original file name

(OP)
@Andrzejek - Also I love the function name, I finally got my name in code that works as intended. bigsmile

Learning - A never ending quest for knowledge usually attained by being thrown in a situation and told to fix it NOW.

RE: Batch rename files with part of original file name

I am glad you know (enough of) VBA to fix what I've missed.
thumbsup2

I see it now. the part Split(strFName, "_")(4) already 'grabs' the ".pdf" part of the file name.

If you would rather have _(x+1) appended instead, you may try: smile

CODE

Option Explicit

Sub RenameTheFiles()
Const strPATH As String = "C:\Test\"
Dim strFName As String
Dim strNewName As String
Dim x As Integer

On Error GoTo DrB0b

strFName = Dir(strPATH)

Do While Len(strFName) > 0
    x = 0
    strNewName = strPATH & Split(strFName, "_")(1) & ".pdf"
    Debug.Print "Rename; " & strPATH & strFName & " As " & strNewName
    Name strPATH & strFName As strNewName
    strFName = Dir
Loop

Exit Sub
DrB0b:

If Err.Number = 58 Then
    'The file exists, add another piece to file name
    x = x + 1
    strNewName = strPATH & Split(strFName, "_")(1) & "_" & x & ".pdf"
    Resume
End If

End Sub 

---- Andy

"Hmm...they have the internet on computers now"--Homer Simpson

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close