A Windows XP help forum. PCbanter

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » PCbanter forum » Microsoft Windows 7 » Windows 7 Forum
Site Map Home Register Authors List Search Today's Posts Mark Forums Read Web Partners

Again a batch-question



 
 
Thread Tools Rate Thread Display Modes
  #1  
Old June 2nd 12, 04:38 PM posted to alt.windows7.general
Helge Haensel
external usenet poster
 
Posts: 62
Default Again a batch-question

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
Ads
  #2  
Old June 2nd 12, 06:37 PM posted to alt.windows7.general
Wolf K
external usenet poster
 
Posts: 356
Default Again a batch-question

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.
  #3  
Old June 2nd 12, 07:00 PM posted to alt.windows7.general
Paul
external usenet poster
 
Posts: 18,275
Default Again a batch-question

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
  #4  
Old June 2nd 12, 07:13 PM posted to alt.windows7.general
Stan Brown
external usenet poster
 
Posts: 2,904
Default Again a batch-question

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...
  #5  
Old June 2nd 12, 08:16 PM posted to alt.windows7.general
Nil[_2_]
external usenet poster
 
Posts: 2,170
Default Again a batch-question

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)
  #6  
Old June 2nd 12, 09:17 PM posted to alt.windows7.general
SC Tom[_3_]
external usenet poster
 
Posts: 4,089
Default Again a batch-question


"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

  #7  
Old June 3rd 12, 11:05 AM posted to alt.windows7.general
Fokke Nauta[_2_]
external usenet poster
 
Posts: 295
Default Again a batch-question


"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


  #8  
Old June 3rd 12, 01:36 PM posted to alt.windows7.general
Stan Brown
external usenet poster
 
Posts: 2,904
Default Again a batch-question

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...
  #9  
Old June 3rd 12, 09:13 PM posted to alt.windows7.general
Fokke Nauta[_2_]
external usenet poster
 
Posts: 295
Default Again a batch-question

"Stan Brown" wrote in message
t...
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


  #10  
Old June 3rd 12, 09:55 PM posted to alt.windows7.general
Zaidy036[_5_]
external usenet poster
 
Posts: 427
Default Again a batch-question

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
  #11  
Old June 5th 12, 11:20 AM posted to alt.windows7.general
Helge Haensel
external usenet poster
 
Posts: 62
Default Again a batch-question

Well friends, I'll try the TCC/LE of Take Command. Thanks.
Helge

Am 02.06.2012, 17:38 Uhr, schrieb Helge Haensel :

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
  #12  
Old June 5th 12, 01:14 PM posted to alt.windows7.general
Nil[_2_]
external usenet poster
 
Posts: 2,170
Default Again a batch-question

On 05 Jun 2012, "Helge Haensel" wrote in
alt.windows7.general:

Well friends, I'll try the TCC/LE of Take Command. Thanks.
Helge


I'll repeat my earlier suggestion:

Robocopy. You already have it.
  #13  
Old June 6th 12, 09:40 AM posted to alt.windows7.general
Fokke Nauta[_2_]
external usenet poster
 
Posts: 295
Default Again a batch-question

"Helge Haensel" wrote in message
newsp.wffdnnzvsjedh2@w7-pc...
Well friends, I'll try the TCC/LE of Take Command. Thanks.
Helge

Am 02.06.2012, 17:38 Uhr, schrieb Helge Haensel :

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



Hi Helge,

This would do the trick (between the ------):

----------------------------------
@echo off
cls
;
rem Give the path name for the files
set p=d:\test
;
pushd %p
dir /b dir.dat
do t=4 to ]
set ,%t]"
del /q /y %v
enddo
popd
unset p, t, v
;
rem end of file
----------------------------------

The easiest way is to put the batch (BTM) file in the same dir as where
TCC/LE is.
Specify the path name to your files.
The dir command will automacillay sort the files in an numeric order.
The file dir.dat which will be created, will also be the last entry in the
file dir.dat, so will automatically be erased after use.

I tested it.

Succes!

Fokke





  #14  
Old June 6th 12, 09:45 AM posted to alt.windows7.general
Fokke Nauta[_2_]
external usenet poster
 
Posts: 295
Default Again a batch-question

"Fokke Nauta" wrote in message
...
"Helge Haensel" wrote in message
newsp.wffdnnzvsjedh2@w7-pc...
Well friends, I'll try the TCC/LE of Take Command. Thanks.
Helge

Am 02.06.2012, 17:38 Uhr, schrieb Helge Haensel :

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



Hi Helge,

This would do the trick (between the ------):

----------------------------------
@echo off
cls
;
rem Give the path name for the files
set p=d:\test
;
pushd %p
dir /b dir.dat
do t=4 to ]
set ,%t]"
del /q /y %v
enddo
popd
unset p, t, v
;
rem end of file
----------------------------------

The easiest way is to put the batch (BTM) file in the same dir as where
TCC/LE is.
Specify the path name to your files.
The dir command will automacillay sort the files in an numeric order.
The file dir.dat which will be created, will also be the last entry in the
file dir.dat, so will automatically be erased after use.

I tested it.

Succes!


I forgot to mention that a TCC/LE batch file is, as with DOS, a plain text
file but with the .btm extension instead of .bat.

Fokke


 




Thread Tools
Display Modes Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off






All times are GMT +1. The time now is 04:04 PM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 PCbanter.
The comments are property of their posters.