Link to home
Create AccountLog in
Avatar of auraorange
auraorangeFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Code to delete files or folders over 7 days old in batch file does not work

Previously on here I was provided code for a batch file for daily backups.

Here is the code template I use:

forfiles /p c:\backup /d -7 /c "cmd /c del @path"

C:\WINDOWS\system32\ntbackup.exe backup C:\Source Folder /j "%date:/=-%-Daily-%computer name%" /f "C:\Destination Folder\%date:/=-%-Daily-%computer name%.bkf"

The backup works fine and has done so for a number of weeks, however, part of the code that stipulates to delete files older than 7 days does not work.

The directory used to store the backup files is actually c:\backup as above

Can someone help please?
SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of auraorange

ASKER

When running the forfiles line in command prompt i'm getting an "invalid arguement / option" error.

And it states "forfiles /?"
Avatar of Bill Prew
Bill Prew

It doesn't produce a syntax error for me here.  I think there are different versions of FORFILES, and the parms were slightly different.  Make sure you have the latest version.  Windows 7 includes version 6.1.7600.16385.  I also have a copy of an earlier version from a resource kit versioned at 5.2.3790.0, and it processed that command line fine.

What is displayed when you do FORFILES /?

And what version shows in the properties for the FORFILES.EXE?

~bp

BP,

I never realised coding of this nature might be operating system specific and rely on a certain version of the forfiles.exe.

I have run "dir C:\backup and the contents of the backup folder displayed as it should showing correct files.

The below information may help find the root of the problem:

1) FORFILES.EXE file version number is 5.2.3790.0

2) Specs of the application server our website is run on:

Dell Single Dual Core Xeon
Windows 2003 Server Standard Edition 32 Bit Edition
2 GB RAM
2 x 250 GB RAID 1 SATA Disks
Managed Firewall: http://www.memset.com/packet-patrol-firewall.php
Metered 20 Mbps Bandwidth with 100 Gigabytes of transfer per month Included.
Managed Support: http://www.memset.com/support/self-managed.php
Hmmm, not sure what you are having trouble with.  I ran a test here with the same version of forfiles you are using and it worked as it should.  I created a c:\backup directory, put 4 files in it, and stamped 2 of them as "old".  I then ran the forfiles command and it removed the old ones.  See below.

[c:\]dir c:\backup
 Directory of c:\backup
01/04/2011  10:07 AM    <DIR>          .
01/04/2011  10:07 AM    <DIR>          ..
01/04/2011  10:05 AM                 2 file1.txt
01/04/2011  10:05 AM                 2 file2.txt
12/01/2010  08:00 AM                 2 file3.txt
12/15/2010  09:00 AM                 2 file4.txt
               4 File(s)              8 bytes

[c:\]c:\_util\forfiles /p c:\backup /d -7 /c "cmd /c del @path"

[c:\]dir c:\backup
 Directory of c:\backup
01/04/2011  10:08 AM    <DIR>          .
01/04/2011  10:08 AM    <DIR>          ..
01/04/2011  10:05 AM                 2 file1.txt
01/04/2011  10:05 AM                 2 file2.txt
               2 File(s)              4 bytes

~bp
This is the exact code I am running in a .BAT file
"
forfiles /p c:\backup /d -7 /c "cmd /c del @path"

C:\WINDOWS\system32\ntbackup.exe backup C:\wwwroot /j "%date:/=-%-Daily-%appserver%" /f "C:\backup\%date:/=-%-Daily-%appserver%.bkf"
"

The forfiles does not work

However, the actual daily backup works perfectly
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
I'm not sure what version of Windows it came in, but you could also try the following at a command line to see where it is pulling forfiles.exe from:

where forfiles

~bp
Thank you for the feedback I will try this tomorrow

Regards
I would like to close this question.

I have not been able to resolve the problem however, I am confident some of you have actually provided a correct solution it is now a little beyond me to start messing around with a server which is not mine, on an operating system I am not familar with and where consequences of myself messing around could take down our corporate website.

For now I will continue to delete files manually as my manager has been advised and he doesnt have the time to look into this for several weeks and it may be that we decide to get our server management providers to give us a full backup solution depending on cost vs value to the business.
ASKER CERTIFIED SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Thank you very much for the feedback and your patience
Welcome.

~bp