Jump to content

Question How to create a windows batch script with timer to delete files automatically?.

CrestfallenBot

For example let's say I have a folder named "X" on my desktop, I want that folder or the files in it to be deleted automatically after 2 hours have passed since I ran the script and I also need a way to cancel it if I don't want the files to be deleted anymore.

Thank you.

Link to comment
Share on other sites

Link to post
Share on other sites

How about this:

PING localhost -n 7200 >NUL
RMDIR /Q/S <foldername>

This will ping localhost 7200 times. Since the ping commands sends a ping every second, this will "sleep" for about 2 hours, before it deletes the directory. You can cancel the script with Ctrl+C at any time.

Remember to either quote or @mention others, so they are notified of your reply

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, Eigenvektor said:

How about this:


PING localhost -n 7200 >NUL
RMDIR /Q/S <foldername>

To test it I have changed 7200 to 10 

PING localhost -n 10 >NUL
RMDIR /Q/S X

 It worked :) but it deleted the entire folder, how about if just wanted the files or just some specific files inside the folder to be deleted without deleting the folder itself? and could you please breakdown the command for me so I understand how this works?.

 

I tried these commands but they didn't work 


C:\Users\crest>PING localhost -n 10 >NUL

C:\Users\crest>Del /Q/S C:\Users\crest\Desktop\X\New folder
Could Not Find C:\Users\crest\Desktop\X\New
Could Not Find C:\Users\crest\folder

C:\Users\crest>Del /Q/S C:\Users\crest\Desktop\X\11.mkv

Even though they exist according to dir: 


 Directory of C:\Users\crest\Desktop\X

01/01/2020  10:12 PM    <DIR>          .
01/01/2020  10:12 PM    <DIR>          ..
01/01/2020  10:09 PM                 0 11.mkv
01/01/2020  10:02 PM    <DIR>          New folder
               1 File(s)              0 bytes
               3 Dir(s)  615,379,709,952 bytes free

C:\Users\crest\Desktop\X>

 

Link to comment
Share on other sites

Link to post
Share on other sites

Look at the error message and the folder(s) it is trying to delete.

 

Your folder name has a space in it, so you need to use quotation marks. It good practice to always quote file and directory names in batch scripts.

 

Something like

del "C:\...\*.*"

Remember to either quote or @mention others, so they are notified of your reply

Link to comment
Share on other sites

Link to post
Share on other sites

21 minutes ago, Eigenvektor said:

Look at the error message and the folder(s) it is trying to delete.

 

Your folder name has a space in it, so you need to use quotation marks. It good practice to always quote file and directory names in batch scripts.

 

Something like

 


del "C:\...\*.*"
PING localhost -n 1 >NUL
del /q /s "C:\Users\crest\Desktop\X\testfolder"
del /q /s "C:\Users\crest\Desktop\X\lol.txt"

lol.txt gets deleted but testfolder doesn't. 


 Directory of C:\Users\crest\Desktop\X

01/01/2020  10:44 PM    <DIR>          .
01/01/2020  10:44 PM    <DIR>          ..
01/01/2020  10:44 PM                 0 lol.txt
01/01/2020  10:26 PM    <DIR>          testfolder
               1 File(s)              0 bytes
               3 Dir(s)  615,380,656,128 bytes free

Link to comment
Share on other sites

Link to post
Share on other sites

You have task scheduler  (at least that's what it's called in Windows 7)

 

You can create tasks that will run at a specific time interval , like every two hours, or every day at 1 pm, or every monday at some time etc etc

you can also create or control tasks from command line, where you have finer control over times and everything ... I'm copy pasting from the Help of task scheduler

Quote
  To create or manage a task on a remote computer by using the command line
  1. Open a command prompt. To open a command prompt, click Start, click All Programs, click Accessories, and then click Command Prompt.

  2. Use the Schtasks.exe tool to manage or create a task, and specify the name or IP address of the remote computer you want to connect to in the /S system argument, the user name used to connect to the remote computer in the /U username argument, and the password for the username in the /P password argument. For help using Schtasks.exe, type one of the following commands:

    • schtasks /Create /?
       
    • schtasks /Run /?
       
    • schtasks /End /?
       
    • schtasks /Delete /?
       
    • schtasks /Change /?

 

So you could have your batch file or whatever  run  schtasks /End /TN "Name of task"  to stop the task from being launched again, until you manually start it... or use /Delete to delete it completely.

 

You can also create tasks using batch and command line, ex this from schtasks /Create /?

Creates a scheduled task "doc" on the remote machine "ABC" which runs notepad.exe every hour under user "runasuser".

You can remove most of the parameters as the first few are about configuring the scheduled task on a remote pc, if you want task on current pc you don't need those

 

 SCHTASKS /Create /S ABC /U user /P password /RU runasuser /RP runaspassword /SC HOURLY /TN doc /TR notepad

 

Link to comment
Share on other sites

Link to post
Share on other sites

3 hours ago, Eigenvektor said:

Look at the error message and the folder(s) it is trying to delete.

 

Your folder name has a space in it, so you need to use quotation marks. It good practice to always quote file and directory names in batch scripts.

 

Something like

 


del "C:\...\*.*"

 

Nevermind I figured it out: TIMEOUT /T <timeinseconds> /NOBREAK >NUl del /Q/S <pathtofiles>

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×