Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

Posted

For the first part, you could use regular expressions to check the filename and see if it meets your criteria. If it does, then you can rename it as you need to since you know that the ACTUAL filename matches the regular expression filename.

 

For your second question, I wouldn't have your exception fire off a call to the same function again. Try putting it into a loop. Here's the basics of what I mean:

 

 

Private Sub CheckLock()

       Dim i As Integer = 0
       Dim myFileStream As Stream
       Dim fi As FileInfo

       fi = New FileInfo(strPL1SAPPath & "\ZGPSIN0001_OUTBOUND_.txt")

       Dim openSuccessful As Boolean = False

       If fi.Exists() Then
           Do Until(i = 5 Or openSuccessful)
               Try
                   'try to open it to see if it was locked
                   myFileStream = New FileStream(strPL1SAPPath & "\ZGPSIN0001_OUTBOUND_.txt", FileMode.Open, FileAccess.Read)
                   openSuccessful = True
                   'if an exception occurs the file is locked
               Catch ex As Exception
                   i = i + 1       'try for 5 times i is global
                   If i = 5 Then
                       MailException("An error occurred when checking if file was locked." & Chr(13) & Chr(13) & ex.ToString())
                       Application.Exit()
                   End If
               Finally
                   If Not (myFileStream Is Nothing) Then myFileStream.Close()
               End Try
           Loop
       End If

   End Sub

 

Try the above code.

  • Leaders
Posted

You could use the following function of the DirectoryInfo class to get a list of files that match a string including standard wildcard characters (i.e. * and ?)

Public Function GetFiles(ByVal searchPattern As String) As System.IO.FileInfo()

'For example
Dim Folder As New DirectoryInfo("C:\\Temp\\")
Dim Files() As FileInfo = Folder.GetFiles("test*.txt")

[sIGPIC]e[/sIGPIC]
Posted

Can you please post an example of this regular expression and how it fits my needs I'd be very interested,

 

For your second example I cannot use a simple loop, my requirement is to have the app wait for five minutes before checking for a total of five checks.

 

Any more help is greatly appreciated ?

Posted
You could use the following function of the DirectoryInfo class to get a list of files that match a string including standard wildcard characters (i.e. * and ?)

Public Function GetFiles(ByVal searchPattern As String) As System.IO.FileInfo()

'For example
Dim Folder As New DirectoryInfo("C:\\Temp\\")
Dim Files() As FileInfo = Folder.GetFiles("test*.txt")

 

I cannot use that since I am not looking for multiple files, I am only looking at 3 distinct files. Are you saying to use the FileInfo object on three seperate instances which are ALL in the same folder?

Posted

The code given by marble_eater will return an array of the files that match "test*.txt". What you do with any one of them then is up to you.

 

As long as your application doesn't need to worry about not being seen to do anything for 5 minutes you could use:

 

System.Threading.Thread.Sleep(300000)

 

to wait 5 minutes.

 

Alternatively you could obtain the current time and loop until the time is 5 minutes later calling:

 

System.Windows.Forms.Application.DoEvents

 

To receive any events in the mean time.

 

:)

Please check the Knowledge Base before you post.

"Computers are useless. They can only give you answers." - Pablo Picasso

The Code Net

Posted
Can you please post an example of this regular expression and how it fits my needs I'd be very interested,

 

For your second example I cannot use a simple loop, my requirement is to have the app wait for five minutes before checking for a total of five checks.

 

Any more help is greatly appreciated ?

 

Within the loop, perform a 5 second wait in the Exception clause before continuing.

 

Check out this article for regular expressions:

http://www.devarticles.com/c/a/VB.Net/Regular-Expressions-in-.NET/

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...