Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Looping File Rename in Dir Error

Status
Not open for further replies.

jaycast

Programmer
Nov 28, 2001
42
US
I'm running the following function on a set of files within a specifc directory.

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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top