Windows 7 Forums


Reply
Thread Tools

Again a batch-question

 
 
Helge Haensel
Guest
Posts: n/a
Thanked:
 
      06-02-2012
Hallo NG!

W7/64/HP
I have a folder containing more or less files with names YYYYMMDDtext.zip
All filenames differ by the date information only.
I want to keep the 4 youngest ones and delete the older if any - without
a yes/no prompt as a part of my backup strategy.
Consider the case that intermediate dates may be missing by any number.
Ideas? Thanks!

Vy 73! Helge
--
Helge, DJ1WM
 
Reply With Quote
 
 
 
 
Wolf K
Guest
Posts: n/a
Thanked:
 
      06-02-2012
On 02/06/2012 11:38 AM, Helge Haensel wrote:
> Hallo NG!
>
> W7/64/HP
> I have a folder containing more or less files with names YYYYMMDDtext.zip
> All filenames differ by the date information only.
> I want to keep the 4 youngest ones and delete the older if any - without
> a yes/no prompt as a part of my backup strategy.
> Consider the case that intermediate dates may be missing by any number.
> Ideas? Thanks!
>
> Vy 73! Helge


Firstly, I would rename them all SomeNameYYYMMDD.zip. Then count all
SomeName*.zip files, and if N >4, delete the N-4 oldest file. One way of
doing this: Sort the files, and delete the first or last N-4 files,
depending on sort order. [Reason for renaming: you may have occasion to
build AnotherNameYYYMMDD.zip files. Generically, it's best to name
sequential generic --> specific from left to right.]

Problem is, you'll need a batch language that includes Count functions
or IF-THEN-END so you can build Counts.

HTH & Good Luck,
Wolf K.
 
Reply With Quote
 
 
 
 
Paul
Guest
Posts: n/a
Thanked:
 
      06-02-2012
Helge Haensel wrote:
> Hallo NG!
>
> W7/64/HP
> I have a folder containing more or less files with names YYYYMMDDtext.zip
> All filenames differ by the date information only.
> I want to keep the 4 youngest ones and delete the older if any - without
> a yes/no prompt as a part of my backup strategy.
> Consider the case that intermediate dates may be missing by any number.
> Ideas? Thanks!
>
> Vy 73! Helge


You can try processing the file names, using some complicated scripting
language.

Or, you could keep a tracking file. For example, say that currently there
are four files that exist. Now, create a text file, call it "tracking.txt".
Copy the names of the four files into it.

21020523text.zip
21020525text.zip
21020526text.zip
21020529text.zip

Each time the backup script runs, it reads the first line of "tracking.txt"
and deletes that file. It also deletes the first line of the file. At
the end of the backup run, the latest file name is added to the end
of tracking.txt. After the backup today we might see...

21020525text.zip
21020526text.zip
21020529text.zip
21020602text.zip

So no actual date parsing is taking place. Just a simple FIFO queue
using a text file for tracking.

My scheme is not very clever. Doesn't take into account a situation
where at least four files exist yet. You can add more logic to the script
that processes "tracking.txt" to fix that if you want. But without
any logic to "build a FIFO", you can fake it pretty simply.

I would write the script in AWK, others would use PERL, and so on.
Many scripting languages - use the one you know. I don't know
many scripting languages.

Paul
 
Reply With Quote
 
Stan Brown
Guest
Posts: n/a
Thanked:
 
      06-02-2012
On Sat, 02 Jun 2012 17:38:19 +0200, Helge Haensel wrote:
>
> W7/64/HP
> I have a folder containing more or less files with names YYYYMMDDtext.zip
> All filenames differ by the date information only.
> I want to keep the 4 youngest ones and delete the older if any - without
> a yes/no prompt as a part of my backup strategy.
> Consider the case that intermediate dates may be missing by any number.


I don't know an easy way to do this in native Windows command line.
You could probably do it in PowerShell, but I know nothing of
PowerShell beyond the name.

The most straightforward way I know is to do something like
(untested):

dir /b /o:-dt >zonk.txt
gawk -f zonk.awk <zonk.txt >zonk.bat
zonk

where zonk.awk contains the following (untested):

BEGIN {print "@echo off"}
// { if (NR > 4) print "del " $0 }

The dir command gets the files into a file called zonk.txt, from
youngest to oldest. The gawk command ignores the first four and
prefixes a del command to the others. Then the batch file calls the
zonk.bat file just written.

gawk is free and open source; Win32 builds of it already exist in the
GNU project.

Aside from a possible typo in the above, I'm confident it will work
because it's the sort of thing I do fairly often, though I don't have
your specific requirement.

--
Stan Brown, Oak Road Systems, Tompkins County, New York, USA
http://OakRoadSystems.com
Shikata ga nai...
 
Reply With Quote
 
Nil
Guest
Posts: n/a
Thanked:
 
      06-02-2012
On 02 Jun 2012, "Helge Haensel" <> wrote in
alt.windows7.general:

> W7/64/HP
> I have a folder containing more or less files with names
> YYYYMMDDtext.zip All filenames differ by the date information
> only. I want to keep the 4 youngest ones and delete the older if
> any - without a yes/no prompt as a part of my backup strategy.
> Consider the case that intermediate dates may be missing by any
> number. Ideas? Thanks!


Maybe Robocopy (included with Windows 7) would do it for you. It has a
date option. Also, maybe it would work better in your other batch file
than does XCOPY (I'm not able to test it right now.)


/MAXAGE:n : MAXimum file AGE - exclude files older than n days/date.
/MINAGE:n : MINimum file AGE - exclude files newer than n days/date.

(If n < 1900 then n = no of days, else n = YYYYMMDD date)
 
Reply With Quote
 
SC Tom
Guest
Posts: n/a
Thanked:
 
      06-02-2012

"Helge Haensel" <> wrote in message newsp.we98d5dpsjedh2@w7-pc...
> Hallo NG!
>
> W7/64/HP
> I have a folder containing more or less files with names YYYYMMDDtext.zip
> All filenames differ by the date information only.
> I want to keep the 4 youngest ones and delete the older if any - without
> a yes/no prompt as a part of my backup strategy.
> Consider the case that intermediate dates may be missing by any number.
> Ideas? Thanks!
>
> Vy 73! Helge
> --
> Helge, DJ1WM


Here's a VBS script I use to delete old savegames:
___________________________________________________________
On Error Resume Next

Set fso = CreateObject("Scripting.FileSystemObject")

olddate = DateAdd("d", -11, date) 'change -11 to however old you want to delete from

WScript.StdOut.WriteLine("Today is " & Date & vbCrLf)
WScript.StdOut.WriteLine("Deleting files unaccessed since " & olddate)
WScript.StdOut.WriteLine(" ")

WScript.stdout.writeline("Connecting to FileShare ")
Set folder = fso.GetFolder("C:\folder\where\the files are") ' Get the folder
WScript.StdOut.Writeline("Getting a List of the Files")
Set fc = folder.Files
For Each f1 in fc
If f1.DateLastModified < olddate Then
WScript.StdOut.WriteLine("Removing: " & f1.DateLastModified & vbtab & f1.name)
fso.deletefile(f1)
End If
Next
_____________________________________________________________

I then created a DelOldFiles.bat to run it, and put it in Task Scheduler.
--
SC Tom

 
Reply With Quote
 
Fokke Nauta
Guest
Posts: n/a
Thanked:
 
      06-03-2012

"Helge Haensel" <> wrote in message
newsp.we98d5dpsjedh2@w7-pc...
> Hallo NG!
>
> W7/64/HP
> I have a folder containing more or less files with names YYYYMMDDtext.zip
> All filenames differ by the date information only.
> I want to keep the 4 youngest ones and delete the older if any - without
> a yes/no prompt as a part of my backup strategy.
> Consider the case that intermediate dates may be missing by any number.
> Ideas? Thanks!
>
> Vy 73! Helge
> --
> Helge, DJ1WM
>


Take the free version of Take Command from JP Software, called TCC/LE. It is
on this site:
http://jpsoft.com/comparison-command...-commands.html

With this command processor you are able to create complex, unix style like
bach files. I always use this to automate complex tasks on my PC. It has an
excellent help feature. Using this it is a piece of cake to solve your
problem.

Succes,

Fokke


 
Reply With Quote
 
Stan Brown
Guest
Posts: n/a
Thanked:
 
      06-03-2012
On Sun, 3 Jun 2012 12:05:29 +0200, Fokke Nauta wrote:
>
> "Helge Haensel" <> wrote in message
> newsp.we98d5dpsjedh2@w7-pc...
> > W7/64/HP
> > I have a folder containing more or less files with names YYYYMMDDtext.zip
> > All filenames differ by the date information only.
> > I want to keep the 4 youngest ones and delete the older if any - without
> > a yes/no prompt as a part of my backup strategy.
> > Consider the case that intermediate dates may be missing by any number.
> > Ideas? Thanks!
> >

> Take the free version of Take Command from JP Software, called TCC/LE. It is
> on this site:
> http://jpsoft.com/comparison-command...-commands.html
>
> With this command processor you are able to create complex, unix style like
> bach files. I always use this to automate complex tasks on my PC. It has an
> excellent help feature. Using this it is a piece of cake to solve your
> problem.


As it happens, I do use JPsoft's TCCLE ("tickle"?) as my regular
command processor. (I had the paid version, 4NT, on my Win XP
system.)

Solving this particular problem is not quite straightforward with
TCCLE, because the /o option on the FOR command isn't available in
the free version. But it's still possible to write the file names to
a file and then process the file without relying on an external
program like AWK.

I concur in your recommendation of TCCLE for anyone who spends much
time with the command line.

--
Stan Brown, Oak Road Systems, Tompkins County, New York, USA
http://OakRoadSystems.com
Shikata ga nai...
 
Reply With Quote
 
Fokke Nauta
Guest
Posts: n/a
Thanked:
 
      06-03-2012
"Stan Brown" <> wrote in message
news:...
> On Sun, 3 Jun 2012 12:05:29 +0200, Fokke Nauta wrote:
>>
>> "Helge Haensel" <> wrote in message
>> newsp.we98d5dpsjedh2@w7-pc...
>> > W7/64/HP
>> > I have a folder containing more or less files with names
>> > YYYYMMDDtext.zip
>> > All filenames differ by the date information only.
>> > I want to keep the 4 youngest ones and delete the older if any -
>> > without
>> > a yes/no prompt as a part of my backup strategy.
>> > Consider the case that intermediate dates may be missing by any number.
>> > Ideas? Thanks!
>> >

>> Take the free version of Take Command from JP Software, called TCC/LE. It
>> is
>> on this site:
>> http://jpsoft.com/comparison-command...-commands.html
>>
>> With this command processor you are able to create complex, unix style
>> like
>> bach files. I always use this to automate complex tasks on my PC. It has
>> an
>> excellent help feature. Using this it is a piece of cake to solve your
>> problem.

>
> As it happens, I do use JPsoft's TCCLE ("tickle"?) as my regular
> command processor. (I had the paid version, 4NT, on my Win XP
> system.)
>
> Solving this particular problem is not quite straightforward with
> TCCLE, because the /o option on the FOR command isn't available in
> the free version. But it's still possible to write the file names to
> a file and then process the file without relying on an external
> program like AWK.
>
> I concur in your recommendation of TCCLE for anyone who spends much
> time with the command line.
>
> --


What I would do in this case (and in many cases of mine) is use the DIR
command and write the name of the files into a bare text file.
And proceed from there, reading the lines of the text file and process them
in a BTM file.
It always works!

And thanks for your recommendation.
I think it's a great application. Have used 4DOS for a long time.
This is its descendant me thinks.

Fokke


 
Reply With Quote
 
Zaidy036
Guest
Posts: n/a
Thanked:
 
      06-03-2012
On 6/2/2012 11:38 AM, Helge Haensel wrote:
> Hallo NG!
>
> W7/64/HP
> I have a folder containing more or less files with names YYYYMMDDtext.zip
> All filenames differ by the date information only.
> I want to keep the 4 youngest ones and delete the older if any - without
> a yes/no prompt as a part of my backup strategy.
> Consider the case that intermediate dates may be missing by any number.
> Ideas? Thanks!
>
> Vy 73! Helge


DIR /B /ON [directory path] > D:\Files.txt
then count the lines in D:\Files.txt
if count GE 5 DO (
delete the first file using first line in D:\Files.txt
remove first line in D:\Files.txt
SET Count=Count-1
)
DEL D:\Files.txt
--
Zaidy036
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
System service exception and freeze again and again and again ceyilmaz Crashes, BSODs and Debugging 8 11-18-2011 04:54 AM
windows 7 not booting, showing setup repair again and again greathtg Windows 7 Support 3 10-24-2011 04:51 AM
Firefox updates again! catilley1092 Off-Topic Discussion 16 01-11-2010 07:49 PM
Allowed to go from 64 to 32 again? al91206 Installation, Setup and Updates 2 11-03-2009 04:04 PM
7 home premium full retail £45 again SIW2 General Discussion 1 09-14-2009 08:41 AM


All times are GMT +1. The time now is 02:31 AM.
W7Forums is an independent website and is not affiliated with Microsoft Corporation.