Solved

Rename CSV Files From MS Access 2010

Posted on 2015-01-22
7
192 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
Technology Partners: 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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

707 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