Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Rename CSV Files From MS Access 2010

Posted on 2015-01-22
7
Medium Priority
?
198 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 2000 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

604 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