Solved

Del file extensions in a certain date range

Posted on 2011-09-27
21
712 Views
Last Modified: 2012-05-12
Im looking for a batch Script to del a certain type of file extension from a large database we have. The database contains dated folders ranging back over a decade. I need to clear these cap files out but it takes an incredibly long period of time to scan the whole database. Is there a batch script that would limit the scan to files only from the last year or preferably from the last week. So far this is what I have.
@echo off
del .\*.cap /s /q
I realize this looks through the whole directory but since the dated folders are created daily and each has a large number of subdirectories, Im uncertain of how to specify only the most recent files.
0
Comment
Question by:mstephenslnb
  • 10
  • 8
  • 2
  • +1
21 Comments
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36713075
I have got such a script here for you if you wish:

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

There are also utils people have written that will do it, delage32.exe I think it is.

With this VBScript you can amend as needed, think it is fairly clear but this below should do what you need.  Needless to say if the files are important please don't run across live data without a backup!

You amend the line

DeleteFiles "c:\temp",DateAdd("m", -1, Date)

to be the directory to delete within, and the DateAdd("m", -1, Date) bit to find the right date.  As it stands it takes 1 month of the current date, so if you, say, wanted 1 year old then you could choose DateAdd("yyyy", -1, Date)
The other (main) options for this aside from changing the -1 to any other number for 3,6 months etc. is :

yyyy - Year
q - Quarter
m - Month
d - Day



I have added a check that the filename ends in .cap too

You can run it with

cscript //nologo delolder.vbs, or just clicking from explorer
' 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")

 msgbox "Deleting older than " & DateAdd("m", -1, Date)
 DeleteFiles  "c:\temp",DateAdd("m", -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.DateCreated < cutoffDate and lcase(right(oFile.Name,4)) =".cap" Then
       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
	  msgbox "deleting " & subfolder.name & " created on " & subfolder.DateCreated
	  call SubFolder.Delete()
        End If
Next

End Function

Open in new window

0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36713084
Sorry meant to give a link to delage32.exe:

http://www.horstmuc.de/wbat32.htm
0
 
LVL 37

Expert Comment

by:Gerwin Jansen
ID: 36713088
Hi, can you share what your directory structure looks like? Do you have something like a year/month/day combination in your main directory and subdirectories beneath them? Where are the .cap files located?
0
 

Author Comment

by:mstephenslnb
ID: 36713150
Sure, thats the basic structure below. Right now I've got the .bat setting at x:\directory scanning all the dated folders included.
X:\directory\01-02-08
0
 

Author Comment

by:mstephenslnb
ID: 36713181
Sorry you wanted a directory of where the files were as well. Thats the universal structure for the file location in each dated folder. typically there are four locations per day with four .cap files.  
X:\directory\01-02-08\POD\locationA\xls.cap
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36713191
Well my vbscript or delage32 will do that for you OK or we can go by the file structure.  Can you give us a few more examples please.  Also which age you want to get rid of from, e.g. does it go:

Also, is 01-02-08 - 2nd January 2008, 1st Feb 2008, 8th Feb 2001 etc.?!
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36713206
Also do you want to keep the dir. structure and delete the CAP files or remove the whole 01-02-8 dir. and below .

If  so then frankly you could go into explorer, sort the directory into date order, or by directory if they are yy-mm-dd, highlight and delete!  We could also do that with a batch file then either looking for dirs that start anything other than 11 say, or are xx-xx-11 if that way around.
rd
0
 

Author Comment

by:mstephenslnb
ID: 36713220
Appologies. That is Jan 2nd 2008.
Im testing out delage32 now.
Ill close the case if that solution works, however I'd prefer to use a bat script if possible.
0
 

Author Comment

by:mstephenslnb
ID: 36713225
I need to keep the directories. the .cap file is only a small 8 byte portion of gigs worth of data daily.
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36713277
Then if, for instance you want to keep only those from 2010 and 2011:
@echo off
cd /d x:\directory
for /f %%a in ('dir /b /ad ??-??-0?') do echo del /q /s *.cap

That will echo the del commands it will use... effectively it is looking for xx-xx-0x as the dir names and then doing a del of all subdirs under that of .cap files.

remove the word echo to let it loose.

Steve
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:mstephenslnb
ID: 36713309
I actually need to remove all future .cap files from the whole of the directory (as I have ran a full scan today and deleted the files with my first script i posted.) I want to only scan the last 7 days worth of files added to avoid the time it takes to scan the whole directory for four tiny little cap files created today.
0
 
LVL 43

Accepted Solution

by:
Steve Knight earned 450 total points
ID: 36713575
Hmm, sorry I clearly misread originally because I thought you wanted all .Cap files over a year, or 7 days old removed but it seems it is the other way around then, i.e. just the ones from the last 7 days say?

The batch below will remove todays directory, perhaps to be scheduled at 23:50 each day.... you could soon change each "date" entry to "date-1" and it will run with yesterdays date if you run it after midnight.

Another option is to pickup from the directories, but sorted in date order.  What does this command produce for you:

dir /o-d /ad x:\directory\??-??-1?

If that lists the directories to clear up as the first so many then we can just take the first x entries from that and delete frm those dirs.
@echo off
REM Gets mm-dd-yy from vbscript and returns it to batch file as today
echo wscript.echo right(100 + month(date),2) ^& "-" ^& right(100+day(date),2) ^& "-" ^& right(year(date),2)  > "%temp%\dateparts.vbs"
for /f "tokens=1 delims=" %%a in ('cscript //nologo "%temp%\dateparts.vbs"') do set today=%%a

echo Removing files for today: %today%

REM remove ECHO below if happy with it
ECHO del "x:\directory\%today%*.cap" /s /q

pause

Open in new window

0
 
LVL 43

Assisted Solution

by:Steve Knight
Steve Knight earned 450 total points
ID: 36713613
Based on the dir listing suggestion above.  This will work down newest 7 dirs and del any cap files in subdirs:


@echo off
setlocal enabledelayedexpansion
REM Delete newest dir files
set count=0
for /f "tokens=*" %%a in ('dir /b /ad /o-d "x:\directory"') do (
  set /a count=!count!+1
  del "x:\directory\%%a\*.cap" /s /q
  if !count! GEQ 7 exit /b
)

Open in new window

C:\temp>\delnewer.cmd
Deleted file - c:\temp\18-09-11\new\test.cap
Deleted file - c:\temp\19-09-11\new\test.cap
Deleted file - c:\temp\26-09-11\new\test.cap
Deleted file - c:\temp\25-09-11\new\test.cap
Deleted file - c:\temp\24-09-11\new\test.cap
Deleted file - c:\temp\23-09-11\new\test.cap
Deleted file - c:\temp\20-09-11\new\test.cap

C:\temp>\delnewer.cmd
Could Not Find c:\temp\20-09-11\*.cap
Could Not Find c:\temp\26-09-11\*.cap
Could Not Find c:\temp\25-09-11\*.cap
Could Not Find c:\temp\18-09-11\*.cap
Could Not Find c:\temp\19-09-11\*.cap
Could Not Find c:\temp\24-09-11\*.cap
Could Not Find c:\temp\23-09-11\*.cap

Open in new window

0
 

Author Comment

by:mstephenslnb
ID: 36716741
Alright, gave it a shot. Set up a test directory with several days in it and one a year old. Each instant returns an error of operator not found. Any Ideas? Ive pasted below the results.

C:\>setlocal enabledelayedexpansion

C:\>REM Delete newest dir files

C:\>set count=0

C:\>for /F "tokens=*" %a in ('dir /b /ad /o-d "c:\Cap Test"') do (set /a count=!
count!+1 del "c:\Cap Test\%a\*.cap" /s /q if !count! GEQ 7 exit /b )

C:\>(set /a count=!count!+1 del "c:\Cap Test\09-22-10\*.cap" /s /q if !count! GE
Q 7 exit /b )
Missing operator.

C:\>(set /a count=!count!+1 del "c:\Cap Test\09-22-11\*.cap" /s /q if !count! GE
Q 7 exit /b )
Missing operator.

C:\>(set /a count=!count!+1 del "c:\Cap Test\09-26-11\*.cap" /s /q if !count! GE
Q 7 exit /b )
Missing operator.

C:\>(set /a count=!count!+1 del "c:\Cap Test\09-23-11\*.cap" /s /q if !count! GE
Q 7 exit /b )
Missing operator.

C:\>(set /a count=!count!+1 del "c:\Cap Test\09-28-11\*.cap" /s /q if !count! GE
Q 7 exit /b )
Missing operator.

C:\>(set /a count=!count!+1 del "c:\Cap Test\09-27-11\*.cap" /s /q if !count! GE
Q 7 exit /b )
Missing operator.

C:\>pause
Press any key to continue . . .

0
 
LVL 10

Assisted Solution

by:ReneGe
ReneGe earned 50 total points
ID: 36716998
Have you created a batch file with dragon-it's script?

If not, copy his script in notepad, then save it with a .bat extension (For example: deletefiles.bat)

Then, run that script.


@echo off
REM Delete newest dir files
setlocal enabledelayedexpansion
set TargetDir=c:\Cap Test
set count=0
for /f "tokens=*" %%a in ('dir /b /ad /o-d "%TargetDir%"') do (
  set /a count+=1
  del "%TargetDir%\%%a\*.cap" /s /q
  if !count! GEQ 7 exit /b
)

Open in new window

0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36717039
Thanks Rene... yes from your screen shot it does look like it has all bunched up on the same lines pasted directly into a command line so the count, del etc. are all mashed up on the same line.

So yes please try it as a .cmd file - Save As from notepad, change type to "all files", give it name of whateveryoulike.cmd
Steve
0
 
LVL 43

Assisted Solution

by:Steve Knight
Steve Knight earned 450 total points
ID: 36717053
Oh BTW your test will delete from all 6 ... it doesn't look a the dir name, it takes the seven most recently created directories out of a folder to delete from.

i.e. in the order show in explorer if you sort by date or by this command at cmd.exe prompt:

dir /ad /o-d

The first 7 shown will have delete command run against them, any more will not.

Steve
0
 

Author Comment

by:mstephenslnb
ID: 36717358
Worked like a dream! My bad on the bunching up there! Thanks so much! you guys are awesome!
0
 

Author Closing Comment

by:mstephenslnb
ID: 36717394
Excellent help. I do apologize on the effort it took, Im new to scripting and not the best at describing my exact needs!
Great Work!
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36717425
No problem, glad we got there in the end - sorry for misunderstanding to start with!

Steve
0
 
LVL 10

Expert Comment

by:ReneGe
ID: 36718019
Thanks for the points. Glad I could somehow help.
Cheers,
Rene
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now