looking for meathods for automatic deletio of 3 day old files

MrAl

Joined Jun 17, 2014
11,559
Hello there,

The way I do it is I use the Windows API to perform almost all the functions needed. This provides for a lot of options.

I create a directory "DeleteHourly" and when I delete a file I actually don't delete it right away, I move it into that directory. The program then does a directory read (which reads the name of all files and date and time and size) and if the date and time are greater than one hour the file gets deleted permanently.
This requires a background process to scan the directory every so often. It does not have to do it too often, even once every ten minutes is good enough because that 'hour' timing is not critical. You can also make it longer, two hours, three, etc., or shorter depending on your needs.
If I remember right these functions are found in the kernel.dll file so you have to link to that .dll file, but possibly some others too.
Calculating the elapsed time is not as easy as it sounds because it involves seconds, hours, minutes, days, months, and years, but you can do an approximation that is simpler when it's only one hour or so.
I have to recommend reading about these functions and they are all available on the MS developer's site.
 

Thread Starter

Wendy

Joined Mar 24, 2008
23,457
I changed the file name to 3.bat and got this result:
240118 SS #2.png
I think I need to take a second look at the 3.bat file.
 

Thread Starter

Wendy

Joined Mar 24, 2008
23,457
Here's my .bat file as a xt file.
/s /d -3 /c "cmd /c del @*.wmv"
The path to the *.wmv files is:
cd/users/Wendy/doc*/web*/*.wmv
The path is defined by task?
Do the rest of the commands look right?
 
Last edited:

Reloadron

Joined Jan 15, 2015
7,543
This is my file which works fine.
Code:
ForFiles /p "C:\Temp" /s /d -3 /c "cmd /c del @file"
The only change is the directory path for your system.

Again, you can't run the .bat file from a command prompt. You can either double click the .bat file or have another program run it. My last post I gave you a link explaining why you can't run the .bat file as it is from a command prompt. Your path I thought looked like this:

Code:
ForFiles /p " C:/users/wendy/documents/videos " /s /d -3 /c "cmd /c del @file"
Yours should look like the above based on my read.

Ron
 

djsfantasi

Joined Apr 11, 2010
9,180
This is my file which works fine.
Code:
ForFiles /p "C:\Temp" /s /d -3 /c "cmd /c del @file"
The only change is the directory path for your system.

Again, you can't run the .bat file from a command prompt. You can either double click the .bat file or have another program run it. My last post I gave you a link explaining why you can't run the .bat file as it is from a command prompt. Your path I thought looked like this:

Code:
ForFiles /p " C:/users/wendy/documents/videos " /s /d -3 /c "cmd /c del @file"
Yours should look like the above based on my read.

Ron
Yes, Wendy. I was about to say I haven’t seen the “Forfiles /p”
 

Reloadron

Joined Jan 15, 2015
7,543
This is what works for me.

ForFiles /p "C:\Temp" /s /d -3 /c "cmd /c del @file"


The only thing you should need to change is the file path. I can only guess the .wmv is to target a specific file type. The version I used will delete all files in the target folder older than 3 days old.

Ron
 

Thread Starter

Wendy

Joined Mar 24, 2008
23,457
Something more like this perhaps bat files sits in my C: directory and I have named the bat file 3.bat?
forfiles /p /s /d -3 /c "cd/bat/3 del @*.wmv"
 

Reloadron

Joined Jan 15, 2015
7,543
Why do you insist on running or trying to run a .bat file extension from a command prompt?

Let's try something. Save the .bat file to your desktop. Now while stuff is in the folder to be removed just double click the .bat file you have saved to the desktop. Has the folder cleaned up?

That's all there is to this. You want the folder emptied of anything 3 days old or older. I also have no idea why you have wmv in there?

Ron
 

panic mode

Joined Oct 10, 2011
2,793
problem i see is that path is not valid and depending how the code is executed and - successful or not, the command window may close before one has a chance to see what happened or what the problem is.

the usual solution to window closing (and covering any trace of success or failure) is to add PAUSE in a new line after whatever is in the script. this will prevent window from closing.

so my suggestion is to try ready made file, like the one attached. i added the FORFILES line twice, first one is just displaying all the files that are about to be deleted, then there is a PAUSE so you still have a way to decide to proceed or abort.

the other part is to not type the path - there cannot be any asterisks in it. it need to be the FULL PATH as is and WITHOUT last backslash, like this:
1705808483785.png

1705808610650.png
1705808911891.png
1705808998598.png
1705809059657.png
 

Attachments

panic mode

Joined Oct 10, 2011
2,793
to see help for any command, just type it followed by /?

DIR /?
FORFILES /?
etc.

note that some commands may be more tolerant when it comes to mixing up / and \ but this is not expected or guaranteed to work for all of them. this is windows and folder separator is a backslash. on Unix type OS it is the forward slash.

for example you changed path using CD/Bob which happened to work with CD but ... not with FORFILES

also some like to lump the switches in different order. i like to follow format as shown by command help.
/P should be followed by path
/S means recursive subdirectories
/D means days and /D -3 means last three days
/C means command, should be followed by suitable string. and it doe
1705811007374.png
 

Thread Starter

Wendy

Joined Mar 24, 2008
23,457
forfiles /p /s /d -3 /c "cd/user*/Doc*/web* del @*.wmv"
What command did I leave out?
*.wmv is what my security recording software uses.
 

panic mode

Joined Oct 10, 2011
2,793
that does not work as explained in posts 35 and 36.
this should really look more like:

FORFILES /M *.wmv /P "C:\cd\userBobM\Documents and Settings\web" /S /D -3 /C "CMD /C del @file"

breakdown:
instruction: FORFILES
search mask : /M *.wmv
path : /P "C:\cd\userBobM\Documents and Settings\web"
subdirectories too : /S
file age: /D -3
external command: /C "CMD /C del @file"
 
Last edited:
Top