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
Daily Task to delete *.mpg files created at 4:05am (Read 7796 times)
mkanet
Junior Member
**
Offline



Posts: 64
Daily Task to delete *.mpg files created at 4:05am
Jan 1st, 2011 at 8:39am
 
Hi Gert, you have always helped me create some extra-ordinary cool tasks pushing the limits of watchdirectory tasks...

This time, I have a very simple request..  Maybe it needs a new plugin (probably very simple plugin):

All I want to do is set up a task which will delete any *.mpg files created at 4:05am every morning.  The directory doesnt need to be monitored all day...  the task is very simple.. and only active after 4:06am... and inactive after it deletes these small test files (1 minute test TV recordings).

I realize this can probably be done with a vbscript that can be launched via Windows scheduler.  However, I dont know VBScript unfortunately.

I understand if you wont be able to help me.  However, you are the smartest guy I know when it comes to automated tasks.  Maybe you could provide some kind of solution... even if its the code to sample vbscript.

It would be great though if there is a watchdirectory plugin which is ONLY active at a certain time to perform a specific task such as this.

No worries if this is not something you want to support.

Have a happy New Year!
Michael
Back to top
 
 
IP Logged
 
Gert
YaBB Administrator
*****
Offline



Posts: 2225
The Netherlands
Re: Daily Task to delete *.mpg files created at 4:
Reply #1 - Jan 1st, 2011 at 9:15am
 
Best wishes to you as well!

Assuming the directory is C:\Dir write a deletefiles.bat:
Code:
DEL "C:\Dir\*.mpg"
 


or if the mpg files can also be in a sub-directory:
Code:
DEL /s "C:\Dir\*.mpg"
 



Now create a Windows Scheduled Task (somewhere on your Windows Control Panel) to start the bat file every day on 4:05am.
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
 
mkanet
Junior Member
**
Offline



Posts: 64
Re: Daily Task to delete *.mpg files created at 4:
Reply #2 - Jan 1st, 2011 at 9:21am
 
Thank you Gert.  Unfortunately, its not that simple; or, I would have already done that.  The problem is there are other mpg files in that folder which should not be deleted.   The only files that should be deleted are the ones created at 4:04am or later on the same day.  I would execute the batch file at 4:06am.
Back to top
 
 
IP Logged
 
Gert
YaBB Administrator
*****
Offline



Posts: 2225
The Netherlands
Re: Daily Task to delete *.mpg files created at 4:
Reply #3 - Jan 1st, 2011 at 9:37am
 
Ah, I see. That is more complex, I guess.

I am currently busy with a new beta release for WD (probably on the beta forum tomorrow or Monday).

For the "run a bat file" task, I can add a few environment variables ( http://www.watchdirectory.net/wdhelp/plugins/wdopAutoRunBatEnv.html ), for example
WD_FILEAGE_MINUTES_CR (how old in minutes based on creation timestamp)
WD_FILEAGE_MINUTES_LM (how old in minutes based on last modified timestamp)

So then you can create a task with the option "Fire FILENEW for all files at startup" that starts this script:
Code:
rem ignore files older than 10 minutes
IF %WD_FILEAGE_MINUTES_CR% GEQ 10 GOTO :EOF
DEL "%WD_FILE%"
 


and again use the Windows Scheduler to start the WD task at 4:05. Stop it using the scheduler at 4:10. See also this blogpost:
http://watchdirftp.blogspot.com/2008/09/automatically-start-watchdirectory.html

With the current release (you don't need the beta for that), you can also check the file size in the bat file. It sounds to me those test recordings are relatively small. Write something like
Code:
rem ignore files larger than 10 megabytes
IF %WD_FILE_SIZE_MB% GEQ 10 GOTO :EOF
DEL "%WD_FILE%"
 


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
 
mkanet
Junior Member
**
Offline



Posts: 64
Re: Daily Task to delete *.mpg files created at 4:
Reply #4 - Jan 1st, 2011 at 9:58am
 
Sorry Gert, I didnt mean to distract you from your new beta release.

Honestly, I feel that something like this should be done using only a batch file and Windows scheduler... without using Watch Directory.  Now that I think about it, it doesnt really make sense to create a Watchdirectory task just for this; especially since all logic can be done with native dos without the need of WD or 3rd party command-line tools.

It looks like the forfiles windows command might be able to do it in just a few lines.  However, I have no idea how to use it.

Anyway, thank you for your help Gert.  I dont think I should bug you with support about this since it doesnt involve WD.  You are the kindest person; but, I feel very bad for asking your help for something like this.  You have much bigger and more important tasks to work on!

Sorry again to bother you.

Sincerely,
Michael A.
Faithful WD User

Gert wrote on Jan 1st, 2011 at 9:37am:
Ah, I see. That is more complex, I guess.

I am currently busy with a new beta release for WD (probably on the beta forum tomorrow or Monday).

For the "run a bat file" task, I can add a few environment variables ( http://www.watchdirectory.net/wdhelp/plugins/wdopAutoRunBatEnv.html ), for example
WD_FILEAGE_MINUTES_CR (how old in minutes based on creation timestamp)
WD_FILEAGE_MINUTES_LM (how old in minutes based on last modified timestamp)

So then you can create a task with the option "Fire FILENEW for all files at startup" that starts this script:
Code:
rem ignore files older than 10 minutes
IF %WD_FILEAGE_MINUTES_CR% GEQ 10 GOTO :EOF
DEL "%WD_FILE%"
 


and again use the Windows Scheduler to start the WD task at 4:05. Stop it using the scheduler at 4:10. See also this blogpost:
http://watchdirftp.blogspot.com/2008/09/automatically-start-watchdirectory.html

With the current release (you don't need the beta for that), you can also check the file size in the bat file. It sounds to me those test recordings are relatively small. Write something like
Code:
rem ignore files larger than 10 megabytes
IF %WD_FILE_SIZE_MB% GEQ 10 GOTO :EOF
DEL "%WD_FILE%"
 



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



Posts: 2225
The Netherlands
Re: Daily Task to delete *.mpg files created at 4:
Reply #5 - Jan 1st, 2011 at 10:20am
 
Here is something to help you get started

script 1:
Code:
C:
CD \Directory
forfiles /D +0 /C "cmd /c call c:\temp\delold.bat @path @ftime"
 


this script calls delold.bat for all files created today. It passes the full path and creation time of those files.

script 2 (delold.bat):
Code:
SET FILE=%1
SET FTIME=%2

rem first 2 positions of the file time (4:58:16 or 12:10:10)
SET HOUR=%FTIME:~0,2%
IF "%HOUR%" NEQ "4:" GOTO :EOF

rem if MINUTE has a leading zero, remove it because otherwise
rem we can not use it with LEQ and GEQ (bat thinks it is an octal number)
SET MINUTE=%FTIME:~2,2%
IF %MINUTE:~0,1% EQU 0 SET MINUTE=%MINUTE:~1%

if %MINUTE% LEQ 40 GOTO :EOF

....
 

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
 
mkanet
Junior Member
**
Offline



Posts: 64
Re: Daily Task to delete *.mpg files created at 4:
Reply #6 - Jan 1st, 2011 at 10:51am
 
Gert, you are too kind.  I spent a good amount of time trying to figure out the logic in the two batch files.

I changed the location where the *.mpg files are at and also the location of the two batch files.  That wasn't to hard to do.

So, if I launch the first batch file at 4:06am (using Windows scheduler), will ALL files created after 4am the same day be deleted?

Do I need to change anything else for this to work?  What values would I change for files created after a specified time (for example 4:30am or 5:00am)?  Once I know this, I can have better control over it in the future.

Thanks a million!!!

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



Posts: 2225
The Netherlands
Re: Daily Task to delete *.mpg files created at 4:
Reply #7 - Jan 1st, 2011 at 11:08am
 
Perhaps this script helps a bit better (a new delold.bat)
Code:
SET FILE=%1
SET FTIME=%2

rem first 2 positions of the file time (4:58:16 or 12:10:10)
SET HOUR=%FTIME:~0,2%

rem if ftime doesn't start with "4:" (4am), ignore this file
IF "%HOUR%" NEQ "4:" GOTO :EOF

rem determine the minute part of FTIME.
SET MINUTE=%FTIME:~2,2%

rem if MINUTE has a leading zero, remove it because otherwise
rem we can not use it with LEQ and GEQ (bat thinks it is an octal number)
IF %MINUTE:~0,1% EQU 0 SET MINUTE=%MINUTE:~1%

rem we only want to delete files created after 04:30
rem we already checked for "4" above, so now we check the minutes
rem LEQ = less or equal
rem EQU = equals
rem GEQ = greater or eaqual
if %MINUTE% LEQ 30 GOTO :EOF

rem here you would write
rem DEL %FILE%
rem but it is probably best (for testing) to write

ECHO file %FILE% timestamp %FTIME% should be deleted >> C:\Temp\testdelete.txt

 



This one will write the filenames plus timestamps to a text file. I am a little bit worried about the format of your timestamps so it is better to test a bit first (not really deleting the files).
On my (Dutch) computer, timestamps are written like "4:30:59" - "23:59:59" (24 hour clock). I wonder if this is localized by microsoft so on other systems it may be like "4:39:59am" - "11:59:59pm" which would break the logic of this script. Should be easy enough to fix by also checking for the "am" and "pm" strings.
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
 
mkanet
Junior Member
**
Offline



Posts: 64
Re: Daily Task to delete *.mpg files created at 4:
Reply #8 - Jan 1st, 2011 at 11:50am
 
Gert, tried changing the time range from 2:00-2:30am.  Unfortunately, the script isn't working.  Also, what happens if there are files in the directory from another date that fall in that time range?  It would be preferable not to delete those since we're only concerned with the same day only.  Also, I didnt see a testdelete.txt get created either.

Attached, is the script for 2-2:30am

EDIT:  I also tried removing REM before "DEL %FILE%", but the files still didnt get deleted

Also, below is the commandline output; which is probably what will help you know what's going on:

Code:
C:\Program Files\Command line>del-405-temp-files.cmd

C:\Program Files\Command line>D:

D:\>CD \test

D:\test>forfiles /D +0 /C "cmd /c call C:\PROGRA~1\COMMAN~1\del-405-temp-files2.cmd @path @ftime"


D:\test>SET FILE="D:\test\New Text Document (2).mpg"

D:\test>SET FTIME=2:04:45

D:\test>rem first 2 positions of the file time (4:58:16 or 12:10:10)

D:\test>SET HOUR=2:

D:\test>rem if ftime doesn't start with "2:" (2am), ignore this file

D:\test>IF "2:" NEQ "2:" GOTO :EOF

D:\test>rem determine the minute part of FTIME.

D:\test>SET MINUTE=04

D:\test>rem if MINUTE has a leading zero, remove it because otherwise

D:\test>rem we can not use it with LEQ and GEQ (bat thinks it is an octal number)

D:\test>IF 0 EQU 0 SET MINUTE=4

D:\test>rem we only want to delete files created after 02:30

D:\test>rem we already checked for "2" above, so now we check the minutes

D:\test>rem LEQ = less or equal

D:\test>rem EQU = equals

D:\test>rem GEQ = greater or eaqual

D:\test>if 4 LEQ 30 GOTO :EOF

D:\test>SET FILE="D:\test\New Text Document (2).properties"

D:\test>SET FTIME=2:05:02

D:\test>rem first 2 positions of the file time (4:58:16 or 12:10:10)

D:\test>SET HOUR=2:

D:\test>rem if ftime doesn't start with "2:" (2am), ignore this file

D:\test>IF "2:" NEQ "2:" GOTO :EOF

D:\test>rem determine the minute part of FTIME.

D:\test>SET MINUTE=05

D:\test>rem if MINUTE has a leading zero, remove it because otherwise

D:\test>rem we can not use it with LEQ and GEQ (bat thinks it is an octal number)

D:\test>IF 0 EQU 0 SET MINUTE=5

D:\test>rem we only want to delete files created after 02:30

D:\test>rem we already checked for "2" above, so now we check the minutes

D:\test>rem LEQ = less or equal

D:\test>rem EQU = equals

D:\test>rem GEQ = greater or eaqual

D:\test>if 5 LEQ 30 GOTO :EOF

D:\test>SET FILE="D:\test\New Text Document.txt"

D:\test>SET FTIME=2:04:33

D:\test>rem first 2 positions of the file time (4:58:16 or 12:10:10)

D:\test>SET HOUR=2:

D:\test>rem if ftime doesn't start with "2:" (2am), ignore this file

D:\test>IF "2:" NEQ "2:" GOTO :EOF

D:\test>rem determine the minute part of FTIME.

D:\test>SET MINUTE=04

D:\test>rem if MINUTE has a leading zero, remove it because otherwise

D:\test>rem we can not use it with LEQ and GEQ (bat thinks it is an octal number)

D:\test>IF 0 EQU 0 SET MINUTE=4

D:\test>rem we only want to delete files created after 02:30

D:\test>rem we already checked for "2" above, so now we check the minutes

D:\test>rem LEQ = less or equal

D:\test>rem EQU = equals

D:\test>rem GEQ = greater or eaqual

D:\test>if 4 LEQ 30 GOTO :EOF
 

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



Posts: 2225
The Netherlands
Re: Daily Task to delete *.mpg files created at 4:
Reply #9 - Jan 1st, 2011 at 12:21pm
 
Quote:
Also, what happens if there are files in the directory from another date


forfiles /D +0

The "/D +0" makes sure it only calls the script for files made today.


It seems to me the script is working fine. It ignores files where the time is before 2:30.

Maybe it helps to "debug" the script by writing some info at the very start of delold:

ECHO =====Now testing File %1 Time %2 ===== >> C:\Temp\testdelete.txt

and everywhere you see GOTO :EOF replace with GOTO :IgnoreFile

at the very bottom of your script you write

Code:
GOTO :EOF
:IgnoreFile
ECHO =====File %1 is ignored because of timestamp ===== >> C:\Temp\testdelete.txt
 



(so the above GOTO :EOF is the only one left in the script)

All this scripting inspired me for this blogpost a few minutes ago:
http://watchdirftp.blogspot.com/2011/01/happy-new-year.html
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
 
mkanet
Junior Member
**
Offline



Posts: 64
Re: Daily Task to delete *.mpg files created at 4:
Reply #10 - Jan 2nd, 2011 at 9:33pm
 
Gert, sorry it took me so long to reply back.  I wanted to test this in real-life testing.  It works PERFECTLY and consistently.  You are a very generous and intelligent person.  Your software and knowledge has made me an incredibly happy user.  I can't thank you enough.  You helped me with something that has nothing to do with WD; even when you were busy with a beta update release!  I honestly wish more people in this world would follow your example of kindness in helping those people in need.  I honestly believe in Karma.  I have a strong feeling many good things will come your way this year.

Thank you very much, I will not forget your kindness.  I wish you great success with WD.  It's the BEST directory detection/automation software EVER MADE!  Believe me, I have tried them all.

Sincerely,
Michael
Back to top
 
 
IP Logged
 
Gert
YaBB Administrator
*****
Offline



Posts: 2225
The Netherlands
Re: Daily Task to delete *.mpg files created at 4:
Reply #11 - Jan 3rd, 2011 at 4:59am
 
Thanks for the nice words Michael.

In return, please consider giving an extra donation to your favorite charity, for example Blue Star Families:
http://watchdirftp.blogspot.com/2010/12/thank-you-america.html
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
 
mkanet
Junior Member
**
Offline



Posts: 64
Re: Daily Task to delete *.mpg files created at 4:
Reply #12 - Jan 3rd, 2011 at 5:30am
 
Done.  I made a generous donation Smiley  I would have gladly donated to you as well if I had that option.

...

Thanks again; and, keep up the great work!
-Michael

Gert wrote on Jan 3rd, 2011 at 4:59am:
Thanks for the nice words Michael.

In return, please consider giving an extra donation to your favorite charity, for example Blue Star Families:
http://watchdirftp.blogspot.com/2010/12/thank-you-america.html

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



Posts: 2225
The Netherlands
Re: Daily Task to delete *.mpg files created at 4:
Reply #13 - Jan 3rd, 2011 at 5:42am
 
Great!
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