Solved

Rename CSV Files From MS Access 2010

Posted on 2015-01-22
7
189 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
  • 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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

808 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