Link to home
Start Free TrialLog in
Avatar of tzachari
tzachari

asked on

Deamon in VB

I am currently writing an Unix like Deamon using VB. The program continously checks for the presence of files in a particular folder
in my C drive. How do I write the code that would specify the particular folder in my C drive and if a file exists, I want to make a copy of the file.
Thanks,
Zack
Avatar of hatem72
hatem72

' this is the first part
'btw: it will only find the first one!
'just change the code to find all
'use as:

'MsgBox FindFile("c:\", vFile$) & vFile$

Public Function FileExist(Path$) As Integer
    Dim x

    x = FreeFile

    On Error Resume Next
    Open Path$ For Input As x
    If Err = 0 Then
        FileExist = True
    Else
        FileExist = False
    End If
    Close x

End Function

Public Function FindFile(ByVal Path As String, ByVal File As String) As String
  Dim DirName As String, LastDir As String
 
  If File = "" Then Exit Function
  If Right(Path, 1) <> "\" Then Path = Path & "\"
 
  DirName = Dir(Path & "*.*", vbDirectory)
  Do While Not FileExist(Path & File)
 
    If DirName = "" Then Exit Do
    DoEvents
    If DirName <> "." And DirName <> ".." Then
      If (GetAttr(Path & DirName) And vbDirectory) = vbDirectory Then
        LastDir = DirName
        DirName = FindFile(Path & DirName & "\", File)
        If DirName <> "" Then
          Path = DirName
          Exit Do
        End If
        DirName = Dir(Path, vbDirectory)
        Do Until DirName = LastDir Or DirName = ""
          DirName = Dir
        Loop
        If DirName = "" Then Exit Do
      End If
    End If
    DirName = Dir
  Loop
 
  If FileExist(Path & File) Then FindFile = Path

End Function

ASKER CERTIFIED SOLUTION
Avatar of hess
hess

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
if there are no files in dir("c:\the Dir Your Checking\*") then dir returns ""