Solved

Rename CSV Files From MS Access 2010

Posted on 2015-01-22
7
184 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 119

Expert Comment

by:Rey Obrero
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 119

Expert Comment

by:Rey Obrero
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 119

Accepted Solution

by:
Rey Obrero 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
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 

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 119

Expert Comment

by:Rey Obrero
ID: 40564979
what is the error number?
0
 
LVL 119

Expert Comment

by:Rey Obrero
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
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…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…
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…

762 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now