I'm running the following function on a set of files within a specifc directory.
For some reason, when there is more than one file that fulfills the criteria, I get the following error:
I always seem to get this error in the Rename function, but never after the first pass of the Rename function. I've tried stepping through the process, but have been unable to find out what's going on.
Any ideas?
Thanks
Code:
Private Function RenameFile(ByVal strSoldTo As String, ByVal strDirectory As String, ByVal strBackupDirectory As String, ByVal strSearchString As String, ByVal strTrans As String)
Dim objStreamReader As System.IO.StreamReader
Dim strFileEntries As String, strCurrentLine As String, strFileName As String, strDir As String
For Each strFileEntries In System.IO.Directory.GetFiles(strDirectory)
objStreamReader = System.IO.File.OpenText(strFileEntries)
Do While objStreamReader.Peek <> -1
strCurrentLine = objStreamReader.ReadLine
If InStr(strCurrentLine, strSearchString) > 0 Then
strFileName = strSoldTo & "_" & strTrans & "_" & Format(Now, "yyyyMMddHmm") & ".txt"
Else
strFileName = Nothing
End If
Loop
objStreamReader.Close()
If Len(strFileName) <> 0 Then
Rename(strFileEntries, strDirectory & strFileName)
System.IO.File.Copy(strDirectory & strFileName, strBackupDirectory & strFileName)
End If
strFileName = Nothing
Next
End Function
For some reason, when there is more than one file that fulfills the criteria, I get the following error:
An unhandled exception of type 'System.ArgumentException' occurred in microsoft.visualbasic.dll
Additional information: Procedure call or argument is not valid.
I always seem to get this error in the Rename function, but never after the first pass of the Rename function. I've tried stepping through the process, but have been unable to find out what's going on.
Any ideas?
Thanks