Replace Characters

D Willett
SilverLounger
Posts: 1728
Joined: 25 Jan 2010, 08:34
Location: Stoke on Trent - Staffordshire - England

Replace Characters

Post by D Willett »

Hi.
I have a series of folders with a vast amount of documents in various subfolders.
I need to loop through each folder and re name all of the files.
As examples, the current naming of the files resembles the following:

aaa_-_bbb ccc.pdf
aaa b-c-d_-eee.pdf

The two file names above as you can see are quite mismatched with all kinds of variations.
I would like to loop through them and rename them in some kind of normal format, so they would (after renaming) resemble the following instead:

aaa-bbb-ccc.pdf
aaa-b-c-d-eee.pdf

Does anyone have any code that can handle this and remove spaces, underscores etc?
Cheers ...

Dave.

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: Replace Characters

Post by Rudi »

Hi Dave,

It will be MUCH easier to simply use a renaming app to do this than reinvent the wheel in creating code to do this. There are some really powerful (and free) renaming apps to do this sort of thing. One that I can absolutely recommend is ReNamer.
BTW: Have a look at this screenshot in the above link to illustrate stripping symbols and hyphens and cleaning up names...

ReNamer also provides the ability to rename the contents of multiple folders and even set conditions or explicits on which files to rename or leave alone. I'm sure you'll be able to get the job done quite fast.

Cheers
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.

D Willett
SilverLounger
Posts: 1728
Joined: 25 Jan 2010, 08:34
Location: Stoke on Trent - Staffordshire - England

Re: Replace Characters

Post by D Willett »

What a fantastic find :-)
Thanks Rudi..
Cheers ...

Dave.

D Willett
SilverLounger
Posts: 1728
Joined: 25 Jan 2010, 08:34
Location: Stoke on Trent - Staffordshire - England

Re: Replace Characters

Post by D Willett »

Hi guys.
The utility above has enabled me to change the name to approx 2000 files in a matter of minutes, absolutely brilliant.
Going forward, I need to prevent my users adding files that don't conform to the naming convention.
So if a user enters a name like;

A Panel Off This Car.pdf

Then I want it to be intercepted within the routine and change the spaces to:

A-Panel-Off-This-Car.pdf

Here's my routine:

Code: Select all

Private Sub Command1_Click()

If Me.List1 = "" Then
MsgBox "No Manufacturer Selected", vbInformation, ""
Exit Sub
End If

If Me.List2 = "" Then
MsgBox "No Model Selected", vbInformation, ""
Exit Sub
End If

Dim FN As String
FN = InputBox("Document Name")
FileCopy Me.txtMethDesc.Text, "L:\mmpdf\QuickMethod\" & List1 & "\" & List2 & "\" & FN & ".pdf"
MsgBox "File Transferred", vbInformation, ""
Unload Me
End Sub
I understand the "Replace" function will take care of this but I'm not sure where to include it within my code.

Kind Regards
Cheers ...

Dave.

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: Replace Characters

Post by Rudi »

Try something like this...

Code: Select all

FileCopy Me.txtMethDesc.Text, "L:\mmpdf\QuickMethod\" & List1 & "\" & List2 & "\" & REPLACE(FN," ","-") & ".pdf"
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.

User avatar
HansV
Administrator
Posts: 78545
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Replace Characters

Post by HansV »

Below the InputBox line:

Code: Select all

FN = InputBox("Document Name")
FN = Replace(FN, "_-, "-")
FN = Replace(FN, "-_", "-")
FN = Replace(FN, "_", "-")
FN = Replace(FN, " -, "-")
FN = Replace(FN, "- ", "-")
FN = Replace(FN, " ", "-")
Best wishes,
Hans

D Willett
SilverLounger
Posts: 1728
Joined: 25 Jan 2010, 08:34
Location: Stoke on Trent - Staffordshire - England

Re: Replace Characters

Post by D Willett »

Thanks guys.. I love this site :-)
Cheers ...

Dave.

User avatar
Jan Karel Pieterse
Microsoft MVP
Posts: 656
Joined: 24 Jan 2010, 17:51
Status: Microsoft MVP
Location: Weert, The Netherlands

Re: Replace Characters

Post by Jan Karel Pieterse »

In Hans last post two quotes were missing. Here is the corrected version:

Code: Select all

FN = InputBox("Document Name")
FN = Replace(FN, "_-", "-")
FN = Replace(FN, "-_", "-")
FN = Replace(FN, "_", "-")
FN = Replace(FN, " -", "-")
FN = Replace(FN, "- ", "-")
FN = Replace(FN, " ", "-")
Regards,

Jan Karel Pieterse
Excel MVP jkp-ads.com

User avatar
HansV
Administrator
Posts: 78545
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Replace Characters

Post by HansV »

Thank you, Jan Karel!
Best wishes,
Hans