Trigger script by new files in sub folders

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
GaryC
Newbie
Posts: 5
Joined: Tue Sep 16, 2008 4:54 am

Trigger script by new files in sub folders

Post by GaryC » Thu Feb 05, 2009 9:49 am

Hi,

Would like to know if it is possible and how. For example I have a following folder structure, should trigger the script by adding "my-file.txt"

top-level-folder
- 1st-level-folder
- bak-folder
ignore-file.txt
- 2nd-level-folder
- nth-level-folder
my-file.txt

The problem is number of sub-folder is unknown and sub-folders are created dynamically so it is not possible predefine a folder to monitor. Furthermore, should not monitor the bak-folder.

Thanks

User avatar
JRL
Automation Wizard
Posts: 3505
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Thu Feb 05, 2009 2:07 pm

You could use the DOS "dir" command with a search subfolders switch (/s) to search through the second level folder and all of its subfolders, looking for the existence of the "my-file.txt". If its found, do something, if its not found, wait a bit and search again. Something like this:

Code: Select all

Let>dwell=2
Let>file1=my-file.txt
Let>file2=%temp_dir%\triggertest.txt
Let>dir1=2nd-level-folder
Label>Loop
  Let>RP_WAIT=1
  Let>RP_WINDOWMODE=0
  Run>cmd /c dir %dir1%%file1% /s /b>%file2%
  Readfile>%file2%,data
  Separate>data,%file1%,result
  If>result_count>1
     Goto>Process
  Else
     Wait>%dwell%
     Goto>Loop
  EndIf

Label>Process
MDL>%file1% found

GaryC
Newbie
Posts: 5
Joined: Tue Sep 16, 2008 4:54 am

Post by GaryC » Fri Feb 06, 2009 2:36 am

Thanks a lot for the suggestion, may be I did not make it clear enough,

1. there are unknown number of sub folders and new one can be created from time to time
2. name of new file also not fixed

Base on the idea from your code, can check the time stamp of each file, see if it was created after last time the script was triggered. However, since number of sub-folders (as well as number of files in there) keeps increasing, it will eventually becomes too big for the script to loop through the whole tree.

Is there any better way to detect if any new files in such circumstances?

User avatar
JRL
Automation Wizard
Posts: 3505
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Fri Feb 06, 2009 6:15 am

- Since file name is not fixed, change from named file to *.*
- Since the files are not being moved out of the search path (a wrong assumption on my part) set the archive attribute on searched files and use dir /aa to only find files ready for archiving.
However, since number of sub-folders (as well as number of files in there) keeps increasing, it will eventually becomes too big for the script to loop through the whole tree.
As far as I know, that's going to be an issue no matter what you do unless you have some way to remove searched files and folders to a neutral location. And it won't necessarily become too big to search, but searches will take longer and longer over time.

I use this method to search for files to archive on a 200 gig drive with over three hundred thousand files. The search takes a couple of minutes.

Code: Select all

Let>dwell=2
Let>file1=*.*
Let>file2=%temp_dir%\triggertest.txt
Let>dir1=2nd-level-folder
Label>Loop
  Let>RP_WAIT=1
  Let>RP_WINDOWMODE=0
  Run>cmd /c dir %dir1%%file1% /aa /s /b>%file2%
  Run>cmd /c attrib -a %dir1%%file1% /s
  Readfile>%file2%,data
  Separate>data,%file1%,result
  If>result_count>1
     Goto>Process
  Else
     Wait>%dwell%
     Goto>Loop
  EndIf

Label>Process
MDL>%file1% found

User avatar
JRL
Automation Wizard
Posts: 3505
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Fri Feb 06, 2009 1:25 pm

Sorry. I left the /s out of the attrib line in the posted code above when I posted last night. The /s lets the attrib function work through the entire directory structure rather than only the current or specified directory.

adroege
Automation Wizard
Posts: 438
Joined: Tue Dec 07, 2004 7:39 pm

Find additions and updates to folder structure fast with MD5

Post by adroege » Fri May 07, 2010 1:59 pm

Here is another method to try.

This works very quickly because it's using the MD5 hash
of a folder listing instead of checking every file for additions
or updates.


***EDIT*** Found that somehow pasting the text messed up
the code a little bit near the bottom, so fixed it.


[code]
//See if any new files have been added (or a file updated) to a directory structure
//
//This works because the OS will update the time/date stamp of a
//folder to match the newest file in that folder.

//So it saves time that we only have to look at folders and not
//all the files, to see if a new file has been added, or an
//existing file has been updated.


//Put Hashlib.dll in script directory
Let>Hashlib=%SCRIPT_DIR%\HashLib.dll

Let>newhash=0
Let>oldhash=0
Let>FirstTime=True
Let>RP_WAIT=1
Let>RP_WINDOWMODE=0
Let>TopFolder=c:\advan

Label>Loop


//Get recursive list of subdirectories starting at topfolder
//and don't print the summary line indicating bytes free
//since that can change based upon other file activity on the drive

Run>cmd /c dir %TopFolder% /AD /S |FIND /V " bytes free" >c:\mytemp.txt


//Get MD5 hash of the file
LibFunc>Hashlib,FileMD5,r,c:\mytemp.txt,buf1
Mid>r_2,1,r,newhash

If>FirstTime=True
Let>oldhash=newhash
Let>FirstTime=False
Endif

If>newhasholdhash
MessageModal>Files Changed!
Let>oldhash=newhash
//
// Do anything else you want here
//
Endif

Wait>30
Goto>Loop
[/code]


If you use this, please report back how it performs in your situation.

Thanks!

Post Reply
cron
Sign up to our newsletter for free automation tips, tricks & discounts