Rename multiple csv files eliminating (-) hyphens from the label.

G

Guest

How do I rename multiple csv files eliminating (-) hyphens from the label.

example of a file name

07-24-07-075339-2308-0-sales.CSV

to

07240707533923080sales.CSV

Thanks in advance,

SimplyM
 
D

Dave O

This code will do it: copy it, and paste it as a macro into a
spreadsheet. Before you run it you'll need to change the line that
currently says
FilePath = "c:\CSV Files\"
to
FilePath = "c:\Your Exact Directory Tree Here\"

The code finds all the files in that directory with a CSV extension
and renames them without hyphens to an XYZ extension. Then it changes
the XYZ extensions to CSV. If the code changed the non-hyphenated form
to CSV right away the program would loop endlessly.


Sub CSV_No_Hyphen()
Dim OrigName As String
Dim FilePath As String
Dim NewName As String
Dim SheetName
Dim K As Byte

FilePath = "c:\CSV Files\"
OrigName = Dir(FilePath & "*.csv")
Do While OrigName <> ""
For K = 1 To Len(OrigName) - 4
If Mid(OrigName, K, 1) <> "-" Then
NewName = NewName & Mid(OrigName, K, 1)
End If
Next K

FileCopy FilePath & OrigName, FilePath & NewName & ".xyz"
NewName = ""
Kill FilePath & OrigName

OrigName = Dir
Loop

OrigName = Dir(FilePath & "*.xyz")
Do While OrigName <> ""
NewName = Mid(OrigName, 1, Len(OrigName) - 4)

FileCopy FilePath & OrigName, FilePath & NewName & ".csv"
NewName = ""
Kill FilePath & OrigName

OrigName = Dir
Loop

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top