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 Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

loop a folder until a filename is found

Status
Not open for further replies.

magmo

Programmer
Joined
May 26, 2004
Messages
291
Location
SE
Hi


In VbScript I could use the following code....


Code:
    Dim FSO
    Set FSO = CreateObject("Scripting.FileSystemObject")

    ' Check to see if filename exists
    While FSO.FileExists(myFileName)
      Index = Index + 1
    Wend


Does anyone know a simular way to do this in vb.net?


Regards
 
Check out the FileSystemWatcherClass in the System.IO Namespace.


Sweep
...if it works dont mess with it
 
Yes - you can set up a Directory Watcher

--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
Hi

But I dont need to constantly watch a folder, I only need to loop through a folder when an event is triggered.....

Best Regards
 
imports system.io

if file.exists(filename) then
'do something
end if

 
try something like this:

Code:
    Public Sub FindFile(ByVal path As String, ByVal filename As String)
      Dim fname As String
      Dim dir As System.IO.Directory

      For Each fname In dir.GetFiles(path)
        If fname = filename Then
          'file found
          Exit For
        End If
      Next

    End Sub

-Rick


VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Code:
Dim dlist As New DirectoryInfo("c:\temp\)
Dim myFileInfo As FileInfo()
myFileInfo = dlist.GetFiles("*.txt")
dim iFileCount as Integer = myFileInfo.Length


Sweep
...if it works dont mess with it
 
You could just have a Do Loop:
Code:
Do Until System.IO.File.Exists(MyFile)

Loop

--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
or this too if you are only look to see if the file exists:
Code:
Public Sub FindFile(ByVal path As String, ByVal filename As String)
      Dim fil As System.IO.File

      If fil.Exists(path & "\" & filename) Then
        'found
      Else
        'not found
      End If

    End Sub

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Hi

Just perfect, Thanks a lot for the help!


Regards
 
I think we've given enough pointers...seeing as we all dived in at once. [wink]

Sweep
...if it works dont mess with it
 
5 alternative methods within 22 minutes of posting the question seems like enough to me as well!

--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
is tek-tips the place to get your problems answered or is tek-tips the place to get your problems pummelled! :-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top