Solved

Rename CSV Files From MS Access 2010

Posted on 2015-01-22
7
190 Views
Last Modified: 2015-01-22
I'm trying to rename all CSV files in a folder using MS Access 2010. The naming convention is as follows:
Grade Sheet Report for test 05_Science_MP1 for section 5th Grade Science-5th Grade Science-002-04(M-F)(Patricia Ashley).csv
I would like to rename the file(s) in the example above to 05_Science_MP1_Patricia_Ashley.csv and then loop through the rest of the files in the folder renaming in the same fashion. Please notice that the renaming starts at the first numeric character and continues through the two underscores and concludes with what's inside the last ( ). Thanks
0
Comment
Question by:shieldsco
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
7 Comments
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 40564703
do you already have the codes to loop for the csv files in the folder ?

here is the sample code to extract the new name for the files

Sub renameCSVFiles()
Dim csvFile As String, xFile As String, vArr() As String, j As Integer, xName As String

csvFile = "Grade Sheet Report for test 05_Science_MP1 for section 5th Grade Science-5th Grade Science-002-04(M-F)(Patricia Ashley).csv"
vArr = Split(csvFile, " ")
For j = 0 To UBound(vArr)
    If InStr(vArr(j), "Science_MP1") Then
        xFile = vArr(j)
        Exit For
    End If
Next
xName = Mid(csvFile, InStrRev(csvFile, "("))
xName = Replace(Replace(xName, "(", ""), ")", "")
Debug.Print xFile & "_" & xName
End Sub

Open in new window



try to use that in your codes..
if you have problem, then post all the necessary information
*folder path etc...
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 40564731
here is the codes with the loop

Sub renameCSVFiles()
Dim csvFile As String, xFile As String, csvFolder As String
Dim vArr() As String, j As Integer, xName As String

csvFolder = "C:\Folder\"  [b] '<<< CHANGE to CORRECT path
[/b]csvFile = Dir(csvFolder & "*.csv")
While csvFile <> ""

        vArr = Split(csvFile, " ")
        For j = 0 To UBound(vArr)
            If InStr(vArr(j), "Science_MP1") Then
                xFile = vArr(j)
                Exit For
            End If
        Next
        xName = Mid(csvFile, InStrRev(csvFile, "("))
        xName = Replace(Replace(xName, "(", ""), ")", "")
        Debug.Print xFile & "_" & xName
        
        Name csvFolder & csvFile As csvFolder & xFile & "_" & xName
        
        csvFile = Dir()
Wend
End Sub

Open in new window

0
 
LVL 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 500 total points
ID: 40564738
here is the code with the loop to the folder

Sub renameCSVFiles()
Dim csvFile As String, xFile As String, csvFolder As String
Dim vArr() As String, j As Integer, xName As String

csvFolder = "C:\Folder\" '<<< CHANGE to CORRECT path
csvFile = Dir(csvFolder & "*.csv")
While csvFile <> ""

        vArr = Split(csvFile, " ")
        For j = 0 To UBound(vArr)
            If InStr(vArr(j), "Science_MP1") Then
                xFile = vArr(j)
                Exit For
            End If
        Next
        xName = Mid(csvFile, InStrRev(csvFile, "("))
        xName = Replace(Replace(xName, "(", ""), ")", "")
        Debug.Print xFile & "_" & xName
       
        Name csvFolder & csvFile As csvFolder & xFile & "_" & xName
       
        csvFile = Dir()
Wend
End Sub
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:shieldsco
ID: 40564827
works great with one exception.. if the code does not find a file in the correct format an error occurs in the line below

xName = Mid(csvFile, InStrRev(csvFile, "("))

If the file is not in the proper format skip it
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 40564979
what is the error number?
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 40564985
try

Sub renameCSVFiles()
On Error Resume Next   'Add this line
0
 

Author Closing Comment

by:shieldsco
ID: 40565050
Very Good
0

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question