Hi, I could do with a little help with this issue.
I'm currently developing a "Quick" file renaming system for some reports that are produced and currently renamed in numerical order.
I have set this up to read from the txt document for each file in the specified folder and get the report name. This text is then manipulated slightly to give a nicer looking report name.
The next stage was to copy the original file and paste it into a new file directory with the newly found and edited name.
Unfortunately I encounted Run-time error '52' after a few successful copies. The file name it crashed on is:
E:\Retail Implementation\Report output\_GL152A__00000044.txt
To me the filename seemed okay, so I did a test of just copying the file to the directory location that it would have been given as part of the full process (see sub: Test) and this worked perfectly. I
I have also tried to continue on error but only 700 or so files copied out of 5000 so I can't just omit the ones that don't work.
Some of the code is no doubt sloppy due to editing small parts for testing and debugging purposes.
I've tried lots of things but nothing seems to work.
Any ideas would be greatly appreciated.
I'm currently developing a "Quick" file renaming system for some reports that are produced and currently renamed in numerical order.
I have set this up to read from the txt document for each file in the specified folder and get the report name. This text is then manipulated slightly to give a nicer looking report name.
The next stage was to copy the original file and paste it into a new file directory with the newly found and edited name.
Unfortunately I encounted Run-time error '52' after a few successful copies. The file name it crashed on is:
E:\Retail Implementation\Report output\_GL152A__00000044.txt
To me the filename seemed okay, so I did a test of just copying the file to the directory location that it would have been given as part of the full process (see sub: Test) and this worked perfectly. I
I have also tried to continue on error but only 700 or so files copied out of 5000 so I can't just omit the ones that don't work.
Some of the code is no doubt sloppy due to editing small parts for testing and debugging purposes.
I've tried lots of things but nothing seems to work.
Any ideas would be greatly appreciated.
Code:
Option Explicit
Function GetReportName(FileName As String)
Dim fso As New FileSystemObject
Dim Report As TextStream
Set Report = fso.OpenTextFile(FileName)
Dim FirstLine As String
FirstLine = Report.Read(10)
Report.Close
FirstLine = Replace(FirstLine, "(", "_")
FirstLine = Replace(FirstLine, ")", "_")
FirstLine = Replace(FirstLine, ":", "_")
FirstLine = Trim(FirstLine)
FirstLine = Replace(FirstLine, " ", "_")
FirstLine = Replace(FirstLine, "__", "_")
DoEvents
GetReportName = UCase(FirstLine)
FirstLine = ""
End Function
Sub SearchFiles()
Dim directory As String
directory = "Y:\Reports"
Dim File As Variant, Folder, Files
Dim fso As New FileSystemObject
Set Folder = fso.GetFolder(directory)
Set Files = Folder.Files
For Each File In Files
Dim FileString As String, NewName As String, outputfile As String
FileString = File
NewName = GetReportName(FileString) & "_" & Mid(FileString, 12, 8)
outputfile = "E:\Retail Implementation\Report output" & NewName & ".txt"
DoEvents
fso.CopyFile File, outputfile, True
outputfile = 0
NewName = 0
FileString = 0
Next File
End Sub
Sub test()
Dim fso As New FileSystemObject
fso.CopyFile "Y:\Reports\00000044.txt", "E:\Retail Implementation\Report output\_GL152A__00000044.txt"
End Sub
Last edited by a moderator: