Home / Microsoft / Microsoft DOS / Needing a batch file to delete all files in a folder older than 5 days old
0 Members and 2 Guests are viewing this topic. « previous next »
Pages: 1 2 [All] - (Bottom) Print
Author Topic: Needing a batch file to delete all files in a folder older than 5 days old  (Read 20620 times)
WEW
Topic Starter
Rookie



Posts: 27


« on: August 05, 2008, 07:40:06 PM »

Hey I need a batch file to delete all .avi files in a folder older than 5 days old. I looked at the set, for, if commands, and for examples , but so far can't find any examples that help me figure out how to use the commands,I have read the if,for,set/? , but i guess i am to inexperienced to understand without some examples of their uses.... I bought a security camera and see that it will keep me very busy sorting the files out. I don't know if it's possible or not but would appreciate any help... I have no knowledge using the for, set, and if commands... did look through the forums, but didn't find anything that will help me yet, but will keep searching.... any help would be greatly appreciated...Thanks WEW. 


I'm running a dell dimension E510 with Processor Intel(R) Pentium(R) D CPU 3.00GHz
Processor Speed 2.92 GHz
Memory (RAM) 1024 MB
Operating System Microsoft Windows XP Professional
Operating System Version 5.1.2600
« Last Edit: August 05, 2008, 07:53:32 PM by WEW » IP logged
DJFLuFFY
Rookie



Posts: 11


« Reply #1 on: August 06, 2008, 03:45:46 AM »

I am looking for the same script so please answer this one


Many thanks in advance
IP logged
Carbon Dudeoxide
Global Moderator
Mastermind


Thanked: 146
Posts: 16,087

Certifications: List
Computer: Specs
Experience: Expert
OS: Mac OS


Carbon - The building block of life on Earth.

My Youtube Profile 1 1
« Reply #2 on: August 06, 2008, 03:48:35 AM »

Homework?   ::)
IP logged

Jacob
Hopeful



Posts: 338


« Reply #3 on: August 06, 2008, 03:54:50 AM »

Homework?   ::)
Quote
I'm running a dell dimension E510 with Processor Intel(R) Pentium(R) D CPU 3.00GHz
Processor Speed 2.92 GHz
Memory (RAM) 1024 MB
Operating System Microsoft Windows XP Professional
Operating System Version 5.1.2600
IP logged

DJFLuFFY
Rookie



Posts: 11


« Reply #4 on: August 06, 2008, 06:17:57 AM »

homework????

don't understand
IP logged
Jacob
Hopeful



Posts: 338


« Reply #5 on: August 06, 2008, 08:36:28 AM »

homework????

don't understand
He thinks this persons query is a school assignment.
IP logged

DJFLuFFY
Rookie



Posts: 11


« Reply #6 on: August 06, 2008, 08:38:30 AM »

LOL

i need it to delete my logfiles and .torrent files that i don't use
IP logged
Sidewinder
Guru



Thanked: 97
Posts: 4,342

Experience: Familiar
OS: Windows 7

« Reply #7 on: August 06, 2008, 08:41:17 AM »

homework????

don't understand

It's code for the paranoia factor.

Batch code does not handle date/time arithmetic very well. Dates can be formatted many different ways. The actual format can be extracted from the registry adding overhead to your batch file.

Aging files is simple arithmetic provided the current day of month is greater than age factor. When it's not, you may be dealing with multiple past months and their cumulative days.

Most scripting languages for Windows have functions to specifically handle dates. Any reason your solution must be batch code?

 8)


IP logged

If you don't know where you are going, any road will get you there

                                                                            -Lewis Carroll
Carbon Dudeoxide
Global Moderator
Mastermind


Thanked: 146
Posts: 16,087

Certifications: List
Computer: Specs
Experience: Expert
OS: Mac OS


Carbon - The building block of life on Earth.

My Youtube Profile 1 1
« Reply #8 on: August 06, 2008, 08:43:17 AM »

You can simply do a search of the computer for .log .txt and .torrent and then sort them by date.

Quote
It's code for the paranoia factor.
Heh, you will know it's bad when I just ask 'Why'  :P
IP logged

DJFLuFFY
Rookie



Posts: 11


« Reply #9 on: August 06, 2008, 08:45:45 AM »

if you have another script that does what i want it is welcome :)

i am a PHP coder so i know the basics for programming but making a Batch is really different

You can simply do a search of the computer for .log .txt and .torrent and then sort them by date.

Quote
It's code for the paranoia factor.
Heh, you will know it's bad when I just ask 'Why'  :P

this is how i work now, But it takes a lot of time to do.
IP logged
Sidewinder
Guru



Thanked: 97
Posts: 4,342

Experience: Familiar
OS: Windows 7

« Reply #10 on: August 06, 2008, 09:02:02 AM »

This little snippet is written in VBScript:

Code: [Select]
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder("c:\scripts")            'point to your directory
Set fc = f.Files

For Each objFile In fc
If fso.GetExtensionName(objFile) = "avi" then
If objFile.DateCreated < date - 5 Then
WScript.Echo objFile & " " & objFile.DateCreated
'fso.DeleteFile(objFile)  'commented out
End If
End If
Next

Save with a vbs extension and run from the command line as: cscript scriptname.vbs

The delete function is currently commented out. Script will currently list files to be deleted. When you're ready, uncomment the delete function and comment the wscript.echo line.

Good luck  8)

IP logged

If you don't know where you are going, any road will get you there

                                                                            -Lewis Carroll
DJFLuFFY
Rookie



Posts: 11


« Reply #11 on: August 07, 2008, 12:58:09 AM »

Thanks this works really great :)

only i have 2 new problems  :-[

because i forgot to tell you that it has to do the same with all the folders & subfolders

and now i can only select 1 extension by default it's avi. *.* works fine for me ;)

IP logged
DJFLuFFY
Rookie



Posts: 11


« Reply #12 on: August 07, 2008, 01:03:22 AM »

Thanks this works really great :)

only i have 2 new problems  :-[

because i forgot to tell you that it has to do the same with all the folders & subfolders

and now i can only select 1 extension by default it's avi. *.* works fine for me ;) < Yes i fixed this :)



But i still can't figure out how to include the subfolders
IP logged
Sidewinder
Guru



Thanked: 97
Posts: 4,342

Experience: Familiar
OS: Windows 7

« Reply #13 on: August 07, 2008, 04:02:34 AM »

The original code from the snippet closet had recursion built in. It took me days, hours, oh alright, a few minutes to rip it out. Should have gone with my gut feeling.;D

Again the delete function is commented. You can activate it when you're satisfied this works in your environment. Be careful, files deleted with this script do not make a pit stop in the recycle bin.

Code: [Select]
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder("c:\scripts")           'point to your directory
Set colSubFolders = f.SubFolders

For Each objFolder in colSubFolders
ShowFiles objFolder
Next

Sub ShowFiles(Fld)
Set k = fso.GetFolder(Fld)
Set s = k.SubFolders
Set kf = k.Files

For Each objFile In kf
If fso.GetExtensionName(objFile) = "avi" then
If objFile.DateCreated < date - 5 Then
WScript.Echo objFile & " " & objFile.DateCreated
                                'fso.Deletefile(objFile)    'commented out
End If
End if
Next

For Each SubFolder In s
ShowFiles SubFolder
Next
End Sub

 8)
IP logged

If you don't know where you are going, any road will get you there

                                                                            -Lewis Carroll
DJFLuFFY
Rookie



Posts: 11


« Reply #14 on: August 07, 2008, 05:40:58 AM »

Thanks again :)

but i get an error when i try to delete files. the echo part works great :)




ps. the recycle bin is for Pussies  :P

[recovering disk space -- attachment deleted by admin]
IP logged
Sidewinder
Guru



Thanked: 97
Posts: 4,342

Experience: Familiar
OS: Windows 7

« Reply #15 on: August 07, 2008, 06:01:10 AM »

Quote
but i get an error when i try to delete files

What error might that be? We're not psychic (at least not all of us)

You can try the delete method without the parens: fso.Deletefile objFile OR fso.Deletefile objFile, True

The true option will force read-only files to be deleted.

Quote
ps. the recycle bin is for Pussies

Can I quote you the next time some poster appears on the CH doorstep looking for their lost files? ;D
IP logged

If you don't know where you are going, any road will get you there

                                                                            -Lewis Carroll
DJFLuFFY
Rookie



Posts: 11


« Reply #16 on: August 07, 2008, 06:43:37 AM »

The error is posted as a print screen

I have another one in this message

Quote
but i get an error when i try to delete files

What error might that be? We're not psychic (at least not all of us)

You can try the delete method without the parens: fso.Deletefile objFile OR fso.Deletefile objFile, True

The true option will force read-only files to be deleted.

Quote
ps. the recycle bin is for Pussies

Can I quote you the next time some poster appears on the CH doorstep looking for their lost files? ;D Always  8)  ;D



[recovering disk space -- attachment deleted by admin]
IP logged
!~*:.Pink Floyd.:*~!
Guest
« Reply #17 on: August 07, 2008, 06:49:16 AM »

ps. the recycle bin is for Pussies  :P

I hate how there isnt a option to just delete the everything in recyle bin every few days.
Or turn it off.
IP logged
Sidewinder
Guru



Thanked: 97
Posts: 4,342

Experience: Familiar
OS: Windows 7

« Reply #18 on: August 07, 2008, 08:45:54 AM »

I can't duplicate your error, but I did find an oversight bug that bypasses the files in the top level directory.

Code: [Select]
Set fso = CreateObject("Scripting.FileSystemObject")
fs = "c:\temp\temp"                 'point to your directory
ShowFiles(fs)

Sub ShowFiles(Fld)
Set k = fso.GetFolder(Fld)
Set s = k.SubFolders
Set kf = k.Files

For Each objFile In kf
If fso.GetExtensionName(objFile) = "avi" then
If objFile.DateCreated < date - 5 Then
WScript.Echo objFile & " " & objFile.DateCreated
fso.DeleteFile objfile
End If
End if
Next

For Each SubFolder In s
ShowFiles SubFolder
Next
End Sub

Quote
and now i can only select 1 extension by default it's avi. *.* works fine for me Wink < Yes i fixed this

It might be better if you post the code your actually using. The delete method is correct as written.

I hate how there isnt a option to just delete the everything in recyle bin every few days.
Or turn it off.

This can be easily accomplished by writing a script to run from the scheduler or the startup  folder.

 8)

Quote
i am a PHP coder so i know the basics for programming

I was under the impression that the newer versions of PHP have a command line interface.
IP logged

If you don't know where you are going, any road will get you there

                                                                            -Lewis Carroll
DJFLuFFY
Rookie



Posts: 11


« Reply #19 on: August 07, 2008, 08:57:46 AM »

I just remove the IF statement where the script gets his extension.

like this
Code: [Select]
Set fso = CreateObject("Scripting.FileSystemObject")
fs = "c:\temp\temp"                 'point to your directory
ShowFiles(fs)

Sub ShowFiles(Fld)
Set k = fso.GetFolder(Fld)
Set s = k.SubFolders
Set kf = k.Files

For Each objFile In kf
If objFile.DateCreated < date - 5 Then
WScript.Echo objFile & " " & objFile.DateCreated
fso.DeleteFile objfile
End If
Next

For Each SubFolder In s
ShowFiles SubFolder
Next
End Sub


and the server where i run this script is a file server and there is no webserver installed, and that's why PHP doesn't work.
IP logged
Sidewinder
Guru



Thanked: 97
Posts: 4,342

Experience: Familiar
OS: Windows 7

« Reply #20 on: August 07, 2008, 09:23:01 AM »

So do you still have a problem? I ran the code you posted on a test bed of files and had no problems.

Do you have permissions for the top and sub level directories? You can try adding the True option to the delete method (see earlier post), but I'm pretty sure that's for read-only files and will not override permissions.

This appears to be a local issue rather than a syntax problem with the script. Perhaps you could run the script as an administrator.

 8)
IP logged

If you don't know where you are going, any road will get you there

                                                                            -Lewis Carroll
DJFLuFFY
Rookie



Posts: 11


« Reply #21 on: August 07, 2008, 09:25:57 AM »

i didn't tried this code,

i shall do that First thing tomorrow.

many thanks and i will let you know what this script does :)
IP logged
DJFLuFFY
Rookie



Posts: 11


« Reply #22 on: August 08, 2008, 01:41:03 AM »

I tried it, but i get the same error :S
IP logged
WEW
Topic Starter
Rookie



Posts: 27


« Reply #23 on: August 08, 2008, 08:22:09 AM »

Thank you Sidewinder, I must be missing something,,, totally new to these vbs files. I copied and pasted your code into a notepad and saved it as avi.vbs on my C:\. Here is how i edited my path in.

Set fso = CreateObject("Scripting.FileSystemObject")
fs = "c:\dvr_data"                 
ShowFiles(fs)

Sub ShowFiles(Fld)
   Set k = fso.GetFolder(Fld)
   Set s = k.SubFolders
   Set kf = k.Files
   
   For Each objFile In kf
      If fso.GetExtensionName(objFile) = "avi" then
         If objFile.DateCreated < date - 3 Then
            WScript.Echo objFile & " " & objFile.DateCreated
            fso.DeleteFile objfile
         End If
      End if
   Next
   
   For Each SubFolder In s
      ShowFiles SubFolder
   Next
End Sub

I used this code because I too will have subfolders.The path to my files are  C:\DVR_DATA\VIDEO in this folder i have cam 1, cam2, cam3, cam4.


I opened a cmd prompt and typed   cscript avi.vbs and got the attached results.
Thank you for your time... Sorry i don't understand these vbs files at all. No experience.   THANKS AGAIN FOR YOUR TIME AND PATIENTS 


[recovering disk space -- attachment deleted by admin]
IP logged
Sidewinder
Guru



Thanked: 97
Posts: 4,342

Experience: Familiar
OS: Windows 7

« Reply #24 on: August 08, 2008, 09:27:13 AM »

The code seems to be correct. You can debug this:

Code: [Select]
Set fso = CreateObject("Scripting.FileSystemObject")
fs = "c:\dvr_data"                 
ShowFiles(fs)

Sub ShowFiles(Fld)
   Set k = fso.GetFolder(Fld)
   Set s = k.SubFolders
   Set kf = k.Files
   
   For Each objFile In kf
      'If fso.GetExtensionName(objFile) = "avi" then
         'If objFile.DateCreated < date - 3 Then
            WScript.Echo objFile & " " & objFile.DateCreated
            'fso.DeleteFile objfile
         'End If
      'End if
   Next
   
   For Each SubFolder In s
      ShowFiles SubFolder
   Next
End Sub

The posted code will unconditionally list the files. No checking extension or age. If no listing, there are probably no files in any of the directories. If there is a listing you can check for extension:

Code: [Select]
Set fso = CreateObject("Scripting.FileSystemObject")
fs = "c:\dvr_data"                 
ShowFiles(fs)

Sub ShowFiles(Fld)
   Set k = fso.GetFolder(Fld)
   Set s = k.SubFolders
   Set kf = k.Files
   
   For Each objFile In kf
      If fso.GetExtensionName(objFile) = "avi" then
         'If objFile.DateCreated < date - 3 Then
            WScript.Echo objFile & " " & objFile.DateCreated
            'fso.DeleteFile objfile
         'End If
      End if
   Next
   
   For Each SubFolder In s
      ShowFiles SubFolder
   Next
End Sub

If no listing, none of the files have avi extensions. If there is a listing you can check the age:

Code: [Select]
Set fso = CreateObject("Scripting.FileSystemObject")
fs = "c:\dvr_data"                 
ShowFiles(fs)

Sub ShowFiles(Fld)
   Set k = fso.GetFolder(Fld)
   Set s = k.SubFolders
   Set kf = k.Files
   
   For Each objFile In kf
      If fso.GetExtensionName(objFile) = "avi" then
         If objFile.DateCreated < date - 3 Then
            WScript.Echo objFile & " " & objFile.DateCreated
            'fso.DeleteFile objfile
         End If
      End if
   Next
   
   For Each SubFolder In s
      ShowFiles SubFolder
   Next
End Sub

If no listing, the files do not meet the age requirement. Debugging can be a chore especially with recursion where a subroutine continuously calls itself with changing values, but it will give you some insight how the script works. Do not uncomment the deletefile function until the listings are correct.

Good luck.  8)

IP logged

If you don't know where you are going, any road will get you there

                                                                            -Lewis Carroll
WEW
Topic Starter
Rookie



Posts: 27


« Reply #25 on: August 08, 2008, 10:52:43 AM »

 ;D  Hey Sidewinder It Works,,,, Thank you!!! Don't know how you Came up with that code... Got to say your the Man....I was messing it up because i copied the files to another folder as a precaution.Your tip to check the dates got me to poking around a little deeper..I found the Modified date was same as the created date in the original folder but i wasn't showing the creation date... when i looked at the creation date in the copied folder it showed all were created today.... So Sorry to have u write the last 2 codes... and thanks for taking the time and patients to lead me through this... Wife Hollering at me to go to town so i'll try the delete thing when i get home...Your help is greatly appreciated... I'll let u know how i come out...
IP logged
WEW
Topic Starter
Rookie



Posts: 27


« Reply #26 on: August 08, 2008, 09:08:07 PM »

Hey Sidewinder,,, It worked like a top... thank you for your experties...Did you go to College to learn how to do this?
IP logged
chachi
Greenhorn



Posts: 6


« Reply #27 on: December 12, 2008, 02:46:50 PM »

This script worked great even using a UNC path, I was just wondering if anyone could tell me how to set it to check multiple locations?  Like maybe point to a file with a list of locations vs. just the one location at the top?

Code: [Select]
fs = "c:\dvr_data"
IP logged
Sidewinder
Guru



Thanked: 97
Posts: 4,342

Experience: Familiar
OS: Windows 7

« Reply #28 on: December 12, 2008, 04:14:09 PM »

This script worked great even using a UNC path, I was just wondering if anyone could tell me how to set it to check multiple locations?  Like maybe point to a file with a list of locations vs. just the one location at the top?

Code: [Select]
fs = "c:\dvr_data"

Please start a new thread. Hijacking a 4 month old post is a near guarantee to have your post lost in the shuffle. A post with zero replies is much more likely to gather interest than one with 26 replies. I just happened by today, next time you might not get the service you deserve.

 8)

PS. Yes, it is possible to setup a file of locations, create a dynamic array from that file or even hardcode an array of locations in the script.
IP logged

If you don't know where you are going, any road will get you there

                                                                            -Lewis Carroll
chachi
Greenhorn



Posts: 6


« Reply #29 on: December 15, 2008, 07:46:34 AM »

Thanks and sorry about that.

New Post: http://www.computerhope.com/forum/index.php/topic,72266.msg471441.html#msg471441
IP logged
Pages: 1 2 [All] - (Top) Print 
Home / Microsoft / Microsoft DOS / Needing a batch file to delete all files in a folder older than 5 days old « previous next »
 


Login with username, password and session length

Old Forum Search | Forum Rules
Copyright © 2010 Computer Hope ® All rights reserved.
Powered by SMF 2.0 RC3 | SMF © 2006–2010, Simple Machines LLC
Page created in 0.167 seconds with 19 queries.