Solved

deleting files over a week old with batch files

Posted on 2011-03-25
10
407 Views
Last Modified: 2012-06-27
My company is running trendnet securview ip camera's for our security system that saves large amounts of files to C:\videos directory every couple of minutes. Naturally these directories grow very quickly.

I'm looking for a simple way of having those directories automatically remove video's older than a week every night so as to avoid running out of disk space for any of the camera's.

My knowledge of ms-dos and batch scripting is fairly limited so i want to accomplish this in as simple a manner as possible. This script needs to run on Vista, and windows server 2008 r2.
0
Comment
Question by:highhill2011
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
10 Comments
 
LVL 43

Accepted Solution

by:
Steve Knight earned 125 total points
ID: 35217453
Have got such a script here for you:

http://scripts.dragon-it.co.uk/links/batch-delete-files-older-than

Steve

In this case you'd want to use the line:

DeleteFiles "c:\videos",DateAdd("d", -7, Date)

Copy the rest of the code and save it in notepad as "RemoveOldFiles.vbs" or something.

You can run it from a shortcut or scheduled task as:

cscript //nologo removeoldfiles.vbs

Please test first of course on a copy of the dir perhaps to make sure you are happy with it!

Steve
0
 
LVL 56

Expert Comment

by:Bill Prew
ID: 35217477
If you are open to using a small free third party utility then here's a great one that should meet your needs called DELAGE32:

http://home.mnet-online.de/horst.muc/win/delage.htm
http://home.mnet-online.de/horst.muc/wbat32.htm

~bp
0
 

Author Comment

by:highhill2011
ID: 35217738
my boss would prefer it if we didn't use any third party utilities.

I did try that vbscript, but it didn't delete anything, when i ran it the prompt said it was deleting anything older than 1 month old, did i mess up the spacing or variables somewhere along the line?
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 56

Expert Comment

by:Bill Prew
ID: 35217847
If you go to a DOS command prompt, and run the following command, what exactly does it display:

ECHO %DATE%

~bp
0
 

Author Comment

by:highhill2011
ID: 35217872
fri 03/25/2011
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 35218010
Did you amend it to be a week as suggested, is there anything over a month old there anyway?  If this is a new test directory you have created with test files then they will all have been created now so will not trigger it.  You could amend to use modified date if you prefer instead, as below


' Call initial folder to delete and it will work down subdirs
' By running recursively through subdirs after doing files
' in each dir. Second parameter is date of last files to leave
' Anything older will go.
' Stephen Knight July 2009

Dim deletionDate
Dim fso
Dim oFile
Dim oFolder

Set fso = CreateObject("Scripting.FileSystemObject")

DeleteFiles "c:\videos",DateAdd("d", -7, Date)

msgbox "Deleting older than " & DateAdd("d", -1, Date)

Set oFolder = Nothing
Set oFile = Nothing
Set fso = Nothing

Function DeleteFiles(foldername,cutoffdate)
Set oFolder = fso.GetFolder(foldername)

For Each oFile in oFolder.Files
If ofile.DateLastModified < cutoffDate Then
  wscript.echo oFile.Name
  fso.DeleteFile oFile, True
End If
Next

For Each subFolder In oFolder.SubFolders
DeleteFiles subFolder.Path, cutoffdate
If (SubFolder.Files.Count = 0) and (SubFolder.SubFolders.Count=0) And (SubFolder.DateCreated < cutoffdate) Then
wscript.echo "deleting " & subfolder.name & " created on " & subfolder.DateCreated
call SubFolder.Delete()
End If
Next

End Function

0
 
LVL 56

Expert Comment

by:Bill Prew
ID: 35218012
Okay, I'm sure Steve will get the VBS approach to work for you, but here is a pure BAT solution if you want to go that way.  It's quite a bit more code than a VBS solution since BAT doesn't have the robust date manipulation commands in it, but it's not too bad.

As the script is provided, it will only display a line for each file that would be deleted to the console right now.  This will allow you to test and make sure it does what you want it to.  If it does then remove or REM out the ECHO line that displays the file name and age, and then remove the REM in front of the DEL line so that it will actually delete the files.

@echo off
setlocal EnableDelayedExpansion

REM Define base for folders, and days to keep old folders
Set BaseDir=c:\videos
set DaysToKeep=7

REM Get todays date (MM/DD/YYYY), convert to julian for age checks
call :jDate jToday %DATE:~-10%

REM Process all Files in the directory, delete if old
for %%A in ("%BaseDir%\*.*") do (
  call :jDate jFile %%~tA
  set /A FileAge = !jToday! - !jFile!
  if !FileAge! GTR %DaysToKeep% (
    ECHO File:[%%A] is [!FileAge!] days old and would be deleted
    REM del "%%~A"
  )
)

REM Done
exit /b

REM Subroutine to calculate julian date
:jDate return-variable date-string(MM/DD/YYYY) 
  set DateStr=%~2
  set yy=%DateStr:~6,4%
  set /A mm=1%DateStr:~0,2%-100
  set /A dd=1%DateStr:~3,2%-100
  set /a "yy=10000%yy% %%10000,mm=100%mm% %% 100,dd=100%dd% %% 100"
  set /a %~1=dd-32075+1461*(yy+4800+(mm-14)/12)/4+367*(mm-2-(mm-14)/12*12)/12-3*((yy+4900+(mm-14)/12)/100)/4
  exit /b

Open in new window

~bp
0
 

Author Comment

by:highhill2011
ID: 35218150
ah, i see now. i missed changing the "m" to "d" that makes sense now.
0
 

Author Closing Comment

by:highhill2011
ID: 35261787
this accomplished everything that i needed it to.
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 35261933
Good stuff, glad it helped.  Steve
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question