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
>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" ?
> Can you accept and alternative of "Leave the 10 most recent files and
> delete the rest" ?
>
Lets see ;-)
"Jean Pierre Daviau" <on...@wasenough.ca> wrote in message
news:esUlx7nR...@TK2MSFTNGP05.phx.gbl...
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
This solution developed using XP
It may work for NT4/2K
----- batch begins -------
[1]@echo off
[2]rd "%temp%\testdir" 2>nul
[3]if exist "%temp%\testdir" echo\testdir exists!&goto :eof
[4]for /f "delims=" %%i in ( ' xcopy /l /d:09-15-09 * "%temp%\testdir\"
^|find ":" ' ) do ECHO del "%%i"
------ batch ends --------
Lines start [number] - any lines not starting [number] have been wrapped and
should be rejoined. The [number] that starts the line should be removed
The ECHO keyword needs to be removed to activate the delete It is there as
a safety measure to show what the process WOULD do until
you have verified that it will do what you require
The label :eof is defined in NT+ to be end-of-file but MUST be expressed as
:eof
The spaces surrounding the single-quotes are for emphasis only. The SPACES
are not required but the single-quotes ARE required.
Naturally, you could replace the '09-15-09' with "%1" to supply the date (in
mm-dd-yy format) as a parameter.
Also, you ask for YOUNGER files - files created on or after the date
provided. This is the precise opposite of the normal request - to delete
files OLDER than the date provided.
If you want to delete those OLDER then you can create a tempdir, move the
younger files to that tempdir, then clear the source dir and move the
contents of the tempdir back...
Why don't you try alt.msdos.batch.nt where there is a larger body of
solutions than in this group?
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.