Batch script to run every x seconds

Atouray
Atouray used Ask the Experts™
on
I need a windows batch script to delete files from a specific folder for every x seconds.
Any ideas how to achieve this?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
Paste the script below into a text file with a .cmd extension.  Customize the value of the folder variable on line 4 with the location of the folder containing the files.  Customize the value of the mask variable on line 5 with the file type to delete.  Customize the value of the interval variable on line 6 with the number of seconds to wait.

Running the script will echo the delete commands to be run.  Once you have tested it successfully and are sure it will do what you intend, remove the word ECHO from line 9 to delete the files.


@echo off
setlocal
 
set folder=c:\FilesToDelete
set mask=*.*
set interval=5
 
:_repeat
ECHO del /f /q "%folder%\%mask%"
ping localhost -n %interval% > NUL 2>&1
goto :_repeat

Open in new window

Author

Commented:
How can I start the script to run automatically when the machine is started? Also how can I make it run in the background?

Author

Commented:
Also how can set path other than the C drive?
Announcing the Winners!

The results are in for the 15th Annual Expert Awards! Congratulations to the winners, and thank you to everyone who participated in the nominations. We are so grateful for the valuable contributions experts make on a daily basis. Click to read more about this year’s recipients!

Commented:
There are a few ways.  You could create a scheduled task and set it to run At System Startup.  You could also set the script to run under the group policy node Computer Configuration\Windows Settings\Scripts\Startup.

Change the path to the desired drive and directory by editing the folder variable on line 4.

Author

Commented:
Shift-3 something just come up. I know it's bit different but it's what is needed. I need to script to delete files older than 30 seconds only. The script should only delete files older than 30 seconds. Is there any way to do this?

Thanks.

Commented:
Yes, that would be possible with vbscript.

Author

Commented:
Can you please help me on that and I would prefer it with C# if possible? I need badly thus if can get it now would be grateful.

Thanks.

Commented:
I don't code in C#.  As your question has different requirements I'd recommend closing this one and using the "Ask a related question" link to post to pertinent zones.

Author

Commented:
Since you solved the question I asked before I will award you the points. I've already started the script and now it works. The only thing left is to make it a service and repeat the delete command every 5 seconds in C#.

Thanks.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial