delete by date

  • Thread starter Thread starter Jean Pierre Daviau
  • Start date Start date
J

Jean Pierre Daviau

Hi again gang,

I would like to delete files by date (or even by hours). For example, delete
all files that are younger than 2009-10-06 07:30
 
Hi again gang,

I would like to delete files by date (or even by hours). For example, delete
all files that are younger than 2009-10-06 07:30

Can you accept and alternative of "Leave the 10 most recent files and
delete the rest" ?
 
Jean Pierre Daviau said:
Hi again gang,

I would like to delete files by date (or even by hours). For example,
delete all files that are younger than 2009-10-06 07:30



--
Jean Pierre Daviau

- - - -
Art: http://www.jeanpierredaviau.com

Doing this in a pure batch file is messy. Here is a hybrid VB Script/Batch
file solution. You need to do this:
1. Save the code in some batch file.
2. Adjust lines [02] to [06] to suit your environment as follows:
- Active=false/true: Runs the script in demo or working mode.
- Source: Specify your folder. Do NOT add double quotes!
- MaxAge: Files oder than MaxAge will be deleted.
- Unit: Must be d or h. If it is h then MaxAge is measured in hours,
otherwise in days.
- Recursive=true/false
3. Unwrap wrapped lines.
4. Remove the line numbers.
5. Save the file.
6. Run it.

[01] @echo off
[02] set Active=False
[03] set Source=d:\Tue
[04] set MaxAge=10
[05] set Unit=d
[06] set Recursive=true
[07]
[08] set Scr=c:\TempVBS.vbs
[09] set VB=echo^>^>%Scr%
[10] cd 1>nul 2>%Scr%
[11] %VB% Checked = 0
[12] %VB% Deleted = 0
[13] %VB% Set oFSO = CreateObject("Scripting.FileSystemObject")
[14] %VB% if %Active% then verb = "Deleting """ Else verb = "Old file: """
[15] %VB% CheckFolder oFSO.GetFolder("%Source%")
[16] %VB% WScript.echo
[17] %VB% if %Active% then verb = " file(s) deleted" Else verb = " file(s)
would be deleted"
[18] %VB% WScript.Echo Checked ^& " file(s) checked, " ^& Deleted ^& verb
[19] %VB% Sub CheckFolder (oFldr)
[20] %VB% For Each oFile In oFldr.Files
[21] %VB% Checked = Checked + 1
[22] %VB% If DateDiff("%Unit%", oFile.DateLastModified, Now()) ^>
%MaxAge% Then
[23] %VB% Deleted = Deleted + 1
[24] %VB% WScript.Echo verb ^& oFile.Path ^& """"
[25] %VB% If %Active% Then oFile.Delete
[26] %VB% End If
[27] %VB% Next
[28] %VB% if not %Recursive% then Exit Sub
[29] %VB% For Each oSubfolder In oFldr.Subfolders
[30] %VB% CheckFolder(oSubfolder)
[31] %VB% Next
[32] %VB% End Sub
[33] cscript //nologo %Scr%
[34] del %Scr%
 
Pegasus said:
Jean Pierre Daviau said:
Hi again gang,

I would like to delete files by date (or even by hours). For example,
delete all files that are younger than 2009-10-06 07:30



--
Jean Pierre Daviau

- - - -
Art: http://www.jeanpierredaviau.com

Doing this in a pure batch file is messy. Here is a hybrid VB Script/Batch
file solution. You need to do this:
1. Save the code in some batch file.
2. Adjust lines [02] to [06] to suit your environment as follows:
- Active=false/true: Runs the script in demo or working mode.
- Source: Specify your folder. Do NOT add double quotes!
- MaxAge: Files oder than MaxAge will be deleted.
- Unit: Must be d or h. If it is h then MaxAge is measured in hours,
otherwise in days.
- Recursive=true/false
3. Unwrap wrapped lines.
4. Remove the line numbers.
5. Save the file.
6. Run it.

[01] @echo off
[02] set Active=False
[03] set Source=d:\Tue
[04] set MaxAge=10
[05] set Unit=d
[06] set Recursive=true
[07]
[08] set Scr=c:\TempVBS.vbs
[09] set VB=echo^>^>%Scr%
[10] cd 1>nul 2>%Scr%
[11] %VB% Checked = 0
[12] %VB% Deleted = 0
[13] %VB% Set oFSO = CreateObject("Scripting.FileSystemObject")
[14] %VB% if %Active% then verb = "Deleting """ Else verb = "Old file: """
[15] %VB% CheckFolder oFSO.GetFolder("%Source%")
[16] %VB% WScript.echo
[17] %VB% if %Active% then verb = " file(s) deleted" Else verb = " file(s)
would be deleted"
[18] %VB% WScript.Echo Checked ^& " file(s) checked, " ^& Deleted ^& verb
[19] %VB% Sub CheckFolder (oFldr)
[20] %VB% For Each oFile In oFldr.Files
[21] %VB% Checked = Checked + 1
[22] %VB% If DateDiff("%Unit%", oFile.DateLastModified, Now()) ^>
%MaxAge% Then
[23] %VB% Deleted = Deleted + 1
[24] %VB% WScript.Echo verb ^& oFile.Path ^& """"
[25] %VB% If %Active% Then oFile.Delete
[26] %VB% End If
[27] %VB% Next
[28] %VB% if not %Recursive% then Exit Sub
[29] %VB% For Each oSubfolder In oFldr.Subfolders
[30] %VB% CheckFolder(oSubfolder)
[31] %VB% Next
[32] %VB% End Sub
[33] cscript //nologo %Scr%
[34] del %Scr%

Thanks for your script.
For example, delete all files that are younger than 2009-10-06 07:30

Can I modify your scripts for that goal?

JPD
 
Jean Pierre Daviau said:
Pegasus said:
Jean Pierre Daviau said:
Hi again gang,

I would like to delete files by date (or even by hours). For example,
delete all files that are younger than 2009-10-06 07:30



--
Jean Pierre Daviau

- - - -
Art: http://www.jeanpierredaviau.com

Doing this in a pure batch file is messy. Here is a hybrid VB
Script/Batch file solution. You need to do this:
1. Save the code in some batch file.
2. Adjust lines [02] to [06] to suit your environment as follows:
- Active=false/true: Runs the script in demo or working mode.
- Source: Specify your folder. Do NOT add double quotes!
- MaxAge: Files oder than MaxAge will be deleted.
- Unit: Must be d or h. If it is h then MaxAge is measured in hours,
otherwise in days.
- Recursive=true/false
3. Unwrap wrapped lines.
4. Remove the line numbers.
5. Save the file.
6. Run it.

[01] @echo off
[02] set Active=False
[03] set Source=d:\Tue
[04] set MaxAge=10
[05] set Unit=d
[06] set Recursive=true
[07]
[08] set Scr=c:\TempVBS.vbs
[09] set VB=echo^>^>%Scr%
[10] cd 1>nul 2>%Scr%
[11] %VB% Checked = 0
[12] %VB% Deleted = 0
[13] %VB% Set oFSO = CreateObject("Scripting.FileSystemObject")
[14] %VB% if %Active% then verb = "Deleting """ Else verb = "Old file:
"""
[15] %VB% CheckFolder oFSO.GetFolder("%Source%")
[16] %VB% WScript.echo
[17] %VB% if %Active% then verb = " file(s) deleted" Else verb = "
file(s) would be deleted"
[18] %VB% WScript.Echo Checked ^& " file(s) checked, " ^& Deleted ^& verb
[19] %VB% Sub CheckFolder (oFldr)
[20] %VB% For Each oFile In oFldr.Files
[21] %VB% Checked = Checked + 1
[22] %VB% If DateDiff("%Unit%", oFile.DateLastModified, Now()) ^>
%MaxAge% Then
[23] %VB% Deleted = Deleted + 1
[24] %VB% WScript.Echo verb ^& oFile.Path ^& """"
[25] %VB% If %Active% Then oFile.Delete
[26] %VB% End If
[27] %VB% Next
[28] %VB% if not %Recursive% then Exit Sub
[29] %VB% For Each oSubfolder In oFldr.Subfolders
[30] %VB% CheckFolder(oSubfolder)
[31] %VB% Next
[32] %VB% End Sub
[33] cscript //nologo %Scr%
[34] del %Scr%

Thanks for your script.
For example, delete all files that are younger than 2009-10-06 07:30

Can I modify your scripts for that goal?

JPD

Of course you can. You simply change this line:
%VB% If DateDiff("%Unit%", oFile.DateLastModified, Now()) ^> %MaxAge% Then
to this one:
%VB% If DateDiff("%Unit%", oFile.DateLastModified, Now()) ^< %MaxAge% Then

Remember to run this tool in demo mode until you're confident that it does
what you expect.
 
Back
Top