WatchDirectory home page
WatchDirectory Startseite (Deutsche Version)
Site WatchDirectory (Français)
  Welcome, Guest. Please Login or Register
YaBB - Yet another Bulletin Board
   
  HomeHelpSearchLoginRegister  
 
Page Index Toggle Pages: 1
Run a task based on file content (Read 4028 times)
wesman
Junior Member
**
Offline



Posts: 75
Australia
Run a task based on file content
Jul 17th, 2012 at 3:15am
 
Hi Gert

I hope you are well.

I need to do the following using WatchDirectory, and I am not quite sure how to do it:

I would like to monitor all files with a .htm extension in a particular folder.

If an htm file is created or updated I need to check the contents of the file to see if it contains the string "blahblah" and if it does contain this string, the file needs to be renamed from filename.htm to filename-detected.htm

Please can you tell me how can I accomplish this using WatchDirectory?


Many thanks

Wes
Back to top
 
 
IP Logged
 
Gert
YaBB Administrator
*****
Offline



Posts: 2233
The Netherlands
Re: Run a task based on file content
Reply #1 - Jul 17th, 2012 at 7:41am
 
Hello Wes,

You will need to use the task "run a bat file" for that. The script should look something like this (not tested):
Code:
IF "%WD_REASON%" EQU "FILENEW" GOTO :CheckIt
IF "%WD_REASON%" EQU "FILECHNG" GOTO :CheckIt
rem ignore the rest
GOTO :EOF

:CheckIt
rem check for the string bla bla bla in the file

FINDSTR /I /C:"bla bla bla" "%WD_FILE%"
IF %ERRORLEVEL% NEQ 0 GOTO :EOF

rem rename the fil
MOVE "%WD_FILE%" "C:\Some Directory\%WD_FILE_B%-detected.htm"
 

Back to top
 

Gert Rijs - gert (at) gdpsoftware (dot) com
Blog: http://blog-en.gdpsoftware.com/
End Alzheimer's: http://www.alz.org&&...
WWW WWW GdPSoftware  
IP Logged
 
wesman
Junior Member
**
Offline



Posts: 75
Australia
Re: Run a task based on file content
Reply #2 - Jul 20th, 2012 at 6:43am
 
Thanks very much Gert. With your assistance I have been able to get this evening working perfectly.


Have a great day.

Thanks


Wes
Back to top
 
 
IP Logged
 
wesman
Junior Member
**
Offline



Posts: 75
Australia
Re: Run a task based on file content
Reply #3 - Mar 24th, 2014 at 4:44am
 
Hi Gert. I hope you are well.

Referring to the code in this thread that you provided me with in July 2012, I appear to have found a problem with the FINDSTR function - it seems that if the full Windows file and path length is greater than 205 characters then it seems as though the FINDSTR function is not able to run correctly, and the file gets marked for renaming. This happens even if it is a blank file with no content in it. The %ERRORLEVEL% seems to be set to 0 if the full path+file length is greater than 205 characters, regardless of whether the file contains the string being searched for or not.

Do you know of a workaround for this, or if not then please can you build a fix into the next version/build of WatchDirectory?

Thanks


Wes
Back to top
 
 
IP Logged
 
Gert
YaBB Administrator
*****
Offline



Posts: 2233
The Netherlands
Re: Run a task based on file content
Reply #4 - Mar 24th, 2014 at 8:04am
 
Perhaps you can use
%WD_FILE_SHORTPATH%
instead of
%WD_FILE%

If it is really related to the length of the path, applying this change to your script could work.
Back to top
 

Gert Rijs - gert (at) gdpsoftware (dot) com
Blog: http://blog-en.gdpsoftware.com/
End Alzheimer's: http://www.alz.org&&...
WWW WWW GdPSoftware  
IP Logged
 
wesman
Junior Member
**
Offline



Posts: 75
Australia
Re: Run a task based on file content
Reply #5 - Mar 27th, 2014 at 8:21am
 
Hi Gert. This worked perfectly. Thank you very very much indeed.

Have a great day.

Kind regards

Wes
Back to top
 
 
IP Logged
 
TonyF
YaBB Newbies
*
Offline



Posts: 37
Re: Run a task based on file content
Reply #6 - Feb 10th, 2016 at 12:13pm
 
Hi Gert,

I have an application that requires a task to be run based on a string in a text file. An instrument collects data (the data collection time could range from minutes to hours) and writes the data to a file and when it finishes it adds a text string e.g. 'Filename=X:\PicoLog\test7.PLW' to the end of the file. I have a prototype task that changes the security of the monitored file but I only want it to run when the application has finished writing to it (i.e. when it detects the text string). I assume the file change method would work if I add a variant of your code to my batch file. Please advise.

Many thanks, Tony
Back to top
 
 
IP Logged
 
Gert
YaBB Administrator
*****
Offline



Posts: 2233
The Netherlands
Re: Run a task based on file content
Reply #7 - Feb 11th, 2016 at 7:05am
 
Yes, that should work.
Back to top
 

Gert Rijs - gert (at) gdpsoftware (dot) com
Blog: http://blog-en.gdpsoftware.com/
End Alzheimer's: http://www.alz.org&&...
WWW WWW GdPSoftware  
IP Logged
 
Page Index Toggle Pages: 1