shieldsco
asked on
Rename CSV file for MS Access 2010
I'm using the code below to rename CSV files.
On Error Resume Next
Dim csvFile As String, xFile As String, csvFolder As String
Dim vArr() As String, j As Integer, xName As String
csvFolder = "C:\Students\" '<<< 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
The code works fine when the CSV file in the following format:
Grade Sheet Report for test 05_Science_MP1 for section 5th Grade Science-5th Grade Science-002-04(M-F)(Patric ia Ashley).csv and I get the following expected result:
05_Science_MP1_Patricia Ashley.csv
If change Science to Math I get an unexpected result:
_Patricia Ashley.csv
I need to adjust the code to select starting with the first number and include anything after the two underscores plus anything between the last ()
On Error Resume Next
Dim csvFile As String, xFile As String, csvFolder As String
Dim vArr() As String, j As Integer, xName As String
csvFolder = "C:\Students\" '<<< 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
The code works fine when the CSV file in the following format:
Grade Sheet Report for test 05_Science_MP1 for section 5th Grade Science-5th Grade Science-002-04(M-F)(Patric
05_Science_MP1_Patricia Ashley.csv
If change Science to Math I get an unexpected result:
_Patricia Ashley.csv
I need to adjust the code to select starting with the first number and include anything after the two underscores plus anything between the last ()
better post all the file names of the .csv files
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
revised codes
On Error Resume Next
Dim csvFile As String, xFile As String, csvFolder As String
Dim vArr() As String, j As Integer, xName As String
csvFolder = "C:\Students\" '<<< CHANGE to CORRECT path
csvFile = Dir(csvFolder & "*.csv")
While csvFile <> ""
For j = 0 To UBound(vArr)
If InStr(vArr(j), "Science_MP1") Then
xFile = vArr(j)
Exit For
elseif InStr(vArr(j), "Math_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
On Error Resume Next
Dim csvFile As String, xFile As String, csvFolder As String
Dim vArr() As String, j As Integer, xName As String
csvFolder = "C:\Students\" '<<< CHANGE to CORRECT path
csvFile = Dir(csvFolder & "*.csv")
While csvFile <> ""
For j = 0 To UBound(vArr)
If InStr(vArr(j), "Science_MP1") Then
xFile = vArr(j)
Exit For
elseif InStr(vArr(j), "Math_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
ASKER
Thanks
@shieldsco
did you try my last post?
did you try my last post?
ASKER
Rey - the name is variable
I ask you about it and you never replied.
this is 1. Science_MP1
what else?