Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Batch rename files with part of original file name 4

Status
Not open for further replies.

DrB0b

IS-IT--Management
May 19, 2011
1,431
US
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.
 
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 :
I've got nothing to hide, and I demand that you justify what right you have to ask.
 
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 = [blue]"C:\Test\"[/blue]
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
 
@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.
 
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
 
@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.
 
Give that a spin....

Code:
Option Explicit

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

On Error GoTo DrB0b

strFName = Dir(strPATH)

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

Exit Sub
DrB0b:

If Err.Number = 58 Then[green]
    'The file exists, add another piece from orginal file name[/green]
    strNewName = strPATH & Split(strFName, "_")(1) [blue]& "_" & Split(strFName, "_")(4) &[/blue] ".pdf"[blue]
    Resume[/blue]
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
 
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
 
@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.
 
@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.
 
I am glad you know (enough of) VBA to fix what I've missed.
[thumbsup2]

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

If you would rather have [tt]_(x+1)[/tt] appended instead, you may try: :)

Code:
Option Explicit

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

On Error GoTo DrB0b

strFName = Dir(strPATH)

Do While Len(strFName) > 0[blue]
    x = 0[/blue]
    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[green]
    'The file exists, add another piece to file name[/green][blue]
    x = x + 1[/blue]
    strNewName = strPATH & Split(strFName, "_")(1) & "_" & [blue]x[/blue] & ".pdf"
    Resume
End If

End Sub

---- Andy

"Hmm...they have the internet on computers now"--Homer Simpson
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top