Solved

Start a .exe when a file appears in dir

Posted on 1999-01-24
8
146 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
 
LVL 7

Expert Comment

by:Inteqam
ID: 1470367
?
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

746 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now