As mentionend in the Subject I need a BATCH-File which deletes all files older than one week (for a backup-routine). Does anybody
know how i can implement this routine within a BATCH-File?
Michael
remove the echo to make it functional
FORFILES v 1.1 can be found here:
ftp://ftp.microsoft.com/ResKit/y2kfix/x86/forfiles.exe
--
Regards,
Michael
On Wed, 26 Feb 2003 14:16:22 +0100, "Michael Haupt" <ha...@rmdata.at>
wrote:
remove the echo to make it functional
FORFILES v 1.1 can be found here:
ftp://ftp.microsoft.com/ResKit/y2kfix/x86/forfiles.exe
This is a followup to my own post which had COMMAND instead of %comspec%
in the line above, and strangely enough COMMAND would not function as
expected.
--
Regards,
Michael
On Wed, 26 Feb 2003 14:16:22 +0100, "Michael Haupt" <ha...@rmdata.at>
wrote:
>As mentionend in the Subject I need a BATCH-File which deletes all files older than one week
Windows 95/98/ME only: here are two example Batch files
that use no third-party utilities.
1) Using a Debug flick-back script to calculate date seven ago
This uses nothing outside a default Windows 95/98/ME installation
2) Using Microsoft WSH to calculate date seven days ago
Either technique handles month/year ends (and leap years, too).
In each case run the demo with the folder concerned as the
first parameter:
demo.bat C:\FOLDER
1) Using DEBUG
This technique uses Debug to flick the system date back 7 days
for an instant, creates a file with that date, returns time to now,
then uses the discriminant file to do the deletions.
====Begin cut-and-paste (omit this line)
@ECHO OFF
IF (GOTO:)==(%1) %1%2 (Subroutine handler)
IF ()==(%1) GOTO _INSTRUCTIONS
IF NOT EXIST %1\NUL GOTO _INSTRUCTIONS
:: Create a Discriminant file 7 days old
CALL %0 GOTO: _30DAY
:: Process the files in FolderSpec
FOR %%F IN (%1\*.*) DO CALL %0 GOTO: _FSCAN %%F
:: Tidy up workfile
DEL %TEMP%.\_D7
GOTO EOF (=Subroutine code follows=)
:_30DAY
:: Create a 7-day old file _D7
ECHO.e100 b4 2a cd 21 51 52 bb 7 0 fe ca 75 9 b2 1f fe ce>%TEMP%.\S
ECHO.e111 75 3 b6 c 49 b4 2b cd 21 3c ff 74 eb 4b 75 e8>>%TEMP%.\S
FOR %%C IN ("g=100 121" rcx 0 n%TEMP%.\_D7 w) DO ECHO.%%C>>%TEMP%.\S
ECHO.e100 5a 59 b4 2b cd 21>>%TEMP%.\S
FOR %%C IN ("g=100 106" q) DO ECHO.%%C>>%TEMP%.\S
debug<%TEMP%.\S>NUL
DEL %TEMP%.\S
GOTO EOF (=Subroutine section follows=)
:_FSCAN (Usage: CALL %0 GOTO: _DOLIST "ListItem)
:: Is file older than the discriminant file?
xcopy %3 %TEMP%.\_D7 /d /l /i /y /q | find " 0 ">NUL
:: DEL Command Inactivated - remove ECHO.{demo!} below to activate
IF NOT ERRORLEVEL 1 ECHO.{demo!}DEL %3
GOTO EOF
:_INSTRUCTIONS (for using the script)
ECHO. Syntax: %0 FolderSpec
ECHO. Eg: %0 C:\SomeFolder
ECHO. Files older than 7 days in FolderSpec deleted.
ECHO. Note: this demo version is inactivated, remove
ECHO. the "ECHO.{demo!}", where shown, to activate it.
:EOF (End-of-file)
====End cut-and-paste (omit this line)
Win9x GUI study/demo only. Cut-and-paste as Batch script (file with .BAT
extension). Lines that don't begin with 2 spaces have wrapped by mistake.
(2) Using Microsoft Windows Script Host (see Note 1)
====Begin cut-and-paste (omit this line)
@ECHO OFF
IF (GOTO:)==(%1) %1%2 {Subroutine-Handler}
IF ()==(%1) GOTO EOF
ECHO.
%COMSPEC%/c %0 GOTO: _VBS ECHO. d -7>%TEMP%.\_A.VBS
:: Run the process on all files in folder %1
FOR %%F IN (%1\*.*) DO CALL %0 GOTO: _FSCAN %%F
:: Delete the VBS workfile
DEL %TEMP%.\_A.VBS
GOTO EOF {=Subroutine-section-below=}
:_FSCAN
cscript//nologo %TEMP%.\_A.VBS %3|find "-">NUL
IF ERRORLEVEL 1 GOTO EOF
ECHO. %3 is older than 7 days
ECHO.{demo!}DEL %3
ECHO.
GOTO EOF {=Subroutine-section-below=}
:_VBS (Usage: %COMSPEC%/c %0 GOTO: _VBS ECHO. IntervalCode nn)
%3 Set fso=CreateObject("Scripting.FileSystemObject")
%3 fn=wscript.arguments(0)
%3 Set f=fso.GetFile(fn)
%3 mn=DateAdd("%4",%5,now)
%3 wscript.echo "SET AGE="& DateDiff("s",mn,f.DateLastModified)
:EOF {End-of-file}
====End cut-and-paste (omit this line)
Win9x GUI study/demo only. Cut-and-paste as Batch script (file with .BAT
extension). Lines that don't begin with 2 spaces have wrapped by mistake.
--
William and Linda Allen
Learn to write Batch Files on your Win95/98/ME PC. Free, interactive
Web Course. Syllabus and Index to Lessons: http://www.allenware.com/
Note 1
WSH (Windows Script Host) enables routine tasks to be handled in either
VBScript or JScript. It's available as a free downloadable add-on for
the tiny proportion of Windows machines (mostly older Windows 95 ones)
which don't already have it (in one version or another). Documentation
is included as an HTML help file, and includes cut-and-pastable syntax
examples in VBScript and JScript for all features.
Windows Script Host information/upgrade-to-current-version/downloads:
http://msdn.microsoft.com/scripting/
By default, WSH installs CSCRIPT.EXE which is a Batch file
interface allowing all WSH functionality to be run from a normal
DOS-style Batch file. This interface provides Windows machines
with a massive extension to the traditional Batch functionality.
With FORFILES, you need to include the .COM extension
when you use COMMAND.COM explicity, as in:
============Screen capture Windows 95
C:\WORK>ECHO.>UniqueFileName.EXT
C:\WORK>forfiles -pc:\ -s -mUniqueFileName.* -c"command.com /c ECHO.DEL @FILE"
DEL "UniqueFileName.EXT"
C:\WORK>
============End screen capture
Quoted from one of our previous posts:
Subject: Re: Microsoft Batch Tools freely available
Date: Sun, 25 Nov 2001
http://groups.google.com/groups?selm=9tqc0q$47kuq$1...@ID-55970.news.dfncis.de
===Quote
You can also use this sort of syntax:
forfiles -pc:\ -s -m*.XYZ -c"command.com /c ECHO. Found file @FILE"
to ECHO that message about all files matching the mask if COMMAND.COM
is in the path. But note that you _must_ add the .COM extension
for FORFILES to work.
===End quote
HUNT *.* b#-7 "del" u
(via sig line 3) will do that; omit u for test. It can also detect on
filename, scan subdirectories, etc. Here b#-7 means before the
beginning of the day 7 days ago; if you want exactly 168 hours, see my
NOWMINUS.
Avoid "solutions" that alter the system clock, however briefly,
especially if other programs are running at the same time (e.g.
Windows).
Be careful not to delete too much.
--
© John Stockton, Surrey, UK. j...@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demon.co.uk/> - FAQqish topics, acronyms & links.
PAS EXE TXT ZIP via <URL:http://www.merlyn.demon.co.uk/programs/00index.htm>.
Do not Mail News to me. Before a reply, quote with ">" or "> " (SoRFC1036)
>"Michael Marquart" wrote in message
>> @echo off
>> FORFILES -m*.* -d-8 -c"%comspec% /C echo del @FILE"
>>
>> remove the echo to make it functional
>>
>>
>> FORFILES v 1.1 can be found here:
>> ftp://ftp.microsoft.com/ResKit/y2kfix/x86/forfiles.exe
>>
>>
>> This is a followup to my own post which had COMMAND instead of %comspec%
>> in the line above, and strangely enough COMMAND would not function as
>> expected.
>
>With FORFILES, you need to include the .COM extension
>when you use COMMAND.COM explicity, as in:
In which environment does %COMSPEC% not contain the fully qualified
name of the shell program?
[snip>
> But note that you _must_ add the .COM extension
>for FORFILES to work.
Or use %COMSPEC%.