Again a batch-question

H

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
 
W

Wolf K

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.
 
P

Paul

Helge said:
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
 
S

Stan Brown

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.
 
N

Nil

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)
 
S

SC Tom

Helge Haensel said:
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
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.
 
F

Fokke Nauta

Helge Haensel said:
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
Take the free version of Take Command from JP Software, called TCC/LE. It is
on this site:
http://jpsoft.com/comparison-command-prompt-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
 
S

Stan Brown

Take the free version of Take Command from JP Software, called TCC/LE. It is
on this site:
http://jpsoft.com/comparison-command-prompt-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.
 
F

Fokke Nauta

Stan Brown said:
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
 
Z

Zaidy036

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
 
H

Helge Haensel

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

Fokke Nauta

Helge Haensel said:
Well friends, I'll try the TCC/LE of Take Command. Thanks.
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 %@lines[dir.dat]
set v="%@line[dir.dat,%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
 
F

Fokke Nauta

Fokke Nauta said:
Helge Haensel said:
Well friends, I'll try the TCC/LE of Take Command. Thanks.
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 %@lines[dir.dat]
set v="%@line[dir.dat,%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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top