Link to home
Start Free TrialLog in
Avatar of SStroz
SStrozFlag for United States of America

asked on

Batch file for deleting files

Gurus,

I need to learn how to write a batch file that will automatically delete files from a directory older than "X" days.

For example, delete all files in C:\Backup older than 10 days ago.

Can anyone help?

Thanks in advance

Steve
Avatar of ltickett
ltickett

Are you against using a thirs party command line util?

A bit of googling found the following; http://www.chebucto.ns.ca/~ak621/DOS/Bat-Adv2.html

XXCOPY C:\BACKUP\*.* /DB#10 /RSY /PD0 /ED

XXCOPY available from; http://www.xxcopy.com/

Good Luck

Lee
ASKER CERTIFIED SOLUTION
Avatar of SteveGTR
SteveGTR
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
or if you dont mind Powershell this is a one-liner. Remove the whatif to actually do it.

get-ChildItem c:\backup | where-Object{$_.LastWriteTime -gt (([datetime]::Now).adddays(-10))} | remove-Item -force -whatif

You could easily make it a script as well
Name = remove-oldfiles.ps1
#### cut/past ####
Param($path,[int]$days)
get-ChildItem $path | where-Object{$_.LastWriteTime -gt (([datetime]::Now).adddays(-$days))} | remove-Item -force
################
Then from the PS Prompt just do
PS> C:\PathToScript\remove-oldfiles.ps1 C:\backup 10
or if you want to be precise
PS> C:\PathToScript\remove-oldfiles.ps1 -path C:\backup -days 10
I just used the solution from here today and it was extremly easy and very small:
This may be the quickest/easiest solution for you:
http://www.michna.com/software.htm#DelOld
Specify the file types you want to delete along with the age...
 DelOld.exe C:\TEMP\*.* 7
Referenced from:
https://www.experts-exchange.com/questions/20988259/delete-files-older-than-7-days-batch-file.html
Avatar of SStroz

ASKER

I appreciate the freeware options but I'm trying to avoid that.  I really need a solution that will only rely on Windows and the scheduler to kick it off.

BSonPosh,
I've never heard of PowerShell.  Is that available on all Windows Operating systems?  I would need this to work on servers and workstations from Win 2000 - Win 2003.

SteveGTR,
I'll be trying your code asap.  I had no idea it would be as complex.  As I try it the only two variables I would need to change in production would be the Path and duration (7 days, 30 days, etc).  Can you let me know which elements in your code would need to be changed to accomodate that?

Thanks

Steve

Change workdir and the 10 that is on the call :SUBTRACTDAYS line:

@echo off

setlocal

REM ** REM this line to enable processing
set debug=echo

set workDir=C:\Backup

call :GETDATEPARTS "%date%"
call :SUBTRACTDAYS 10

You could use this also:


@echo off

setlocal

REM ** REM this line to enable processing
set debug=echo

REM ** Configure batch file variables here
set workDir=C:\Backup
set dayCnt=10

call :GETDATEPARTS "%date%"
call :SUBTRACTDAYS %dayCnt%
Avatar of SStroz

ASKER

Thanks for your help!!!