Solved

Start a .exe when a file appears in dir

Posted on 1999-01-24
8
149 Views
Last Modified: 2010-05-03
I need to write a little tsr that scans a certain directory.  When a file appears in that directory, it launches a program.  I know a little VB from my MS Access programming, but not of this nature.  Thanks.
0
Comment
Question by:qship
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 1

Accepted Solution

by:
sunj earned 70 total points
ID: 1470364
You can add a timer control to your form. Set the interval to be, say, 1000. Set the form to be invisible if you want. And add the following code:

Private Sub Timer1_Timer()
If Dir("c:\tmp\write.exe") <> "" Then
    Shell ("c:\tmp\write.exe")
    Unload Me
End If
End Sub

This will lauch the "write.exe" program if it appeared in a certain directory. After lauching the application, the program itself will terminate.
0
 
LVL 2

Expert Comment

by:dm_14
ID: 1470365
sunj's program will work but your CPU usage will be high. A better approach would be to you the  FindFirstChangeNotification API.

Code available if you want it.
0
 
LVL 4

Expert Comment

by:mcix
ID: 1470366
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 7

Expert Comment

by:Inteqam
ID: 1470367
?
0
 

Author Comment

by:qship
ID: 1470368
Sunj:  I assume then, if I want the TSR to remain running 24/7 I would just not use the 'Unload Me' ??

dm_14:  I looked at the site provided by mcix (thank you, btw) but as I am not to up on VB, it was a little overwhelming.  Maybe you could provide just the snippet of code that would do what I am looking to do???
0
 

Author Comment

by:qship
ID: 1470369
Sunj:  I assume then, if I want the TSR to remain running 24/7 I would just not use the 'Unload Me' ??

dm_14:  I looked at the site provided by mcix (thank you, btw) but as I am not to up on VB, it was a little overwhelming.  Maybe you could provide just the snippet of code that would do what I am looking to do???
0
 
LVL 1

Expert Comment

by:sunj
ID: 1470370
yes, qship. You can just take away the Unload Me statement if you want your program remain running.
0
 
LVL 2

Expert Comment

by:dm_14
ID: 1470371
Try this

(1) Start a new project
(2) in you .bas module include the following :

Public Const INFINITE = &HFFFF
Public Const FILE_NOTIFY_CHANGE_FILE_NAME As Long = &H1
Public Const FILE_NOTIFY_CHANGE_DIR_NAME As Long = &H2
Public Const FILE_NOTIFY_CHANGE_ATTRIBUTES As Long = &H4
Public Const FILE_NOTIFY_CHANGE_SIZE As Long = &H8
Public Const FILE_NOTIFY_CHANGE_LAST_WRITE As Long = &H10
Public Const FILE_NOTIFY_CHANGE_LAST_ACCESS As Long = &H20
Public Const FILE_NOTIFY_CHANGE_CREATION As Long = &H40
Public Const FILE_NOTIFY_CHANGE_SECURITY As Long = &H100
Public Const FILE_NOTIFY_FLAGS = FILE_NOTIFY_CHANGE_ATTRIBUTES Or _
                                 FILE_NOTIFY_CHANGE_FILE_NAME Or _
                                 FILE_NOTIFY_CHANGE_LAST_WRITE

Public Declare Function FindFirstChangeNotification Lib "kernel32" _
    Alias "FindFirstChangeNotificationA" _
   (ByVal lpPathName As String, _
    ByVal bWatchSubtree As Long, _
    ByVal dwNotifyFilter As Long) As Long

Public Declare Function FindCloseChangeNotification Lib "kernel32" _
   (ByVal hChangeHandle As Long) As Long

Public Declare Function FindNextChangeNotification Lib "kernel32" _
   (ByVal hChangeHandle As Long) As Long

Public Declare Function WaitForSingleObject Lib "kernel32" _
   (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long

Public Const WAIT_OBJECT_0 = &H0
Public Const WAIT_ABANDONED = &H80
Public Const WAIT_IO_COMPLETION = &HC0
Public Const WAIT_TIMEOUT = &H102
Public Const STATUS_PENDING = &H103

(3) in Form1 and a comman1 button and in its click event place the following code :

    Dim r As Long, sPath As String, lStatus As Long
    Dim lDir As Long, lFound1 As Long, lFound2 As Long
   
    sSourceDir = "C:\temp1\"
    sPath = Left$(sSourceDir, Len(sSourceDir) - 1)
    lDir = FindFirstChangeNotification(sPath, False, FILE_NOTIFY_FLAGS)
    Do
      lFound1 = WaitForSingleObject(lDir, 100)
      DoEvents: If bFlag = True Then Exit Do
    Loop While lFound1 <> 0
    Do
     lFound2 = FindNextChangeNotification(lDir)
     Do
      lFound2 = WaitForSingleObject(lDir, 100)
      DoEvents
     Loop While lFound2 <> 0
     
     If iFound2 = 0 Then
        DoEvents
        sApp = Dir(sSourceDir)
        If sApp<>"" then
            Shell (sSourceDir & sApp)
            sApp = ""
        End If
       
     End If
   Loop
   lFound2 = FindCloseChangeNotification(lDir)

Change sSourceDir to the directory you want to check and then run the project and click on the command button. Whenever a file is create in the directory it will execute that file.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
how to open Waze.com/livemap from address saved in DB? 26 211
Problem to skip loop 6 64
SQL VB connection works in one PC and doesn't in another 15 67
Advice in Xamarin 21 96
Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question