I need a way to rename a file without knowing the EXACT name of the file..for instance I need to use wild card *.
For instance say I have c:\temp\test1242325365.txt
I would like to be able to rename this in my app
like:
fi = New FileInfo("c:\temp\test*.txt")
If fi.Exists() Then
fi.MoveTo(c:\temp2\zgpsout.txt")
End If
I want to be able to say test* rather than the actual name of the file because I will never know the exact name..all I will know is that it may start with "test" and end with ".txt"
That is my first question.
My second question is I need to check if a file is locked...if it is I need my code to wait for 5 minutes and try again..upto 5 times. I am getting no where with this...can someone show me how to "stop" my app for 5 minutes:
Private Sub CheckLock()
Dim myFileStream As Stream
Dim fi As FileInfo
fi = New FileInfo(strPL1SAPPath & "\ZGPSIN0001_OUTBOUND_.txt")
If fi.Exists() Then
Try
'try to open it to see if it was locked
myFileStream = New FileStream(strPL1SAPPath & "\ZGPSIN0001_OUTBOUND_.txt", FileMode.Open, FileAccess.Read)
'if an exception occurs the file is locked
Catch ex As Exception
i = i + 1 'try for 5 times i is [b]global[/b]
If i = 5 Then
MailException("An error occurred when checking if file was locked." & Chr(13) & Chr(13) & ex.ToString())
Application.Exit()
Else
CheckLock()
End If
Finally
If Not (myFileStream Is Nothing) Then myFileStream.Close()
End Try
End If
End Sub
Does that code make sense? Basically I try to open a file, if it is locked it hits that exception and i is incremented by 1...and recalls that function. But I need to sleep(5 minutes) before recallign that function...
Jon