Handling folders in a batch

H

Helge Haensel

Hallo NG!

Once per week I generate a (backup-)folder named YYYYMMDD.
When there are 5 or more folders the oldest 5+-folder(s)
should be deleted. How can I achieve that?
I checked with the help-command at cmd-prompt, but didnt
succeed with the dir-, for- and other commands.
Sorry, it is to tricky for me.
Thanks.

Vy 73! Helge
 
G

Guest

A backup script (.vbs) found in google search is at
http://www.michaelhorowitz.com/backupscript.html
I copy/pasted his .txt version to desktop and it looks like you might be able
to take parts of it for your own purposes. Too bad the mainframe OS concept
of generation data sets does not exist on P.C. There is more to find on
scripting
from Microsoft.
 
P

Pegasus \(MVP\)

Helge Haensel said:
Hallo NG!

Once per week I generate a (backup-)folder named YYYYMMDD.
When there are 5 or more folders the oldest 5+-folder(s)
should be deleted. How can I achieve that?
I checked with the help-command at cmd-prompt, but didnt
succeed with the dir-, for- and other commands.
Sorry, it is to tricky for me.
Thanks.

Vy 73! Helge

Here we go again . . .

Line1 @echo off
Line2 for /F "tokens=*" %%* in ('dir /ad /b /tc /o-d d:\Backup ^| more +5')
do echo rd /s /q "%%*"

The batch file will leave the 5 most recent folders in d:\Backup
and remove the rest.

Remove the word "echo" to activate the script.
rest.
 
P

Phil Robyn

Helge said:
Hallo NG!

Once per week I generate a (backup-)folder named YYYYMMDD.
When there are 5 or more folders the oldest 5+-folder(s)
should be deleted. How can I achieve that?
I checked with the help-command at cmd-prompt, but didnt
succeed with the dir-, for- and other commands.
Sorry, it is to tricky for me.
Thanks.

Vy 73! Helge

- - - - - - - begin screen capture WinXP MCE 2005 SP2 - - - - - - -
c:\cmd>dir /ad \junkdir
Volume in drive C has no label.
Volume Serial Number is 44E5-91F9

Directory of c:\junkdir

04/09/2006 03:13 PM <DIR> .
04/09/2006 03:13 PM <DIR> ..
04/09/2006 03:13 PM <DIR> 20060329
04/09/2006 03:13 PM <DIR> 20060330
04/09/2006 03:13 PM <DIR> 20060401
04/09/2006 03:13 PM <DIR> 20060402
04/09/2006 03:13 PM <DIR> 20060403
04/09/2006 03:13 PM <DIR> 20060404
04/09/2006 03:13 PM <DIR> 20060405
04/09/2006 03:13 PM <DIR> 20060406
04/09/2006 03:13 PM <DIR> 20060407
04/09/2006 03:13 PM <DIR> 20060408
04/09/2006 03:13 PM <DIR> 20060409
0 File(s) 0 bytes
13 Dir(s) 218,433,523,712 bytes free

c:\cmd>demo\SaveFiveDirs
rd /s /q 20060404
rd /s /q 20060405
rd /s /q 20060406
rd /s /q 20060407
rd /s /q 20060403
rd /s /q 20060329

C:\CMD>wyllist demo\SaveFiveDirs.cmd
==========begin file C:\CMD\DEMO\SaveFiveDirs.cmd ==========
01. @echo off
02. pushd \junkdir
03. for /f "skip=5" %%a in (
04. 'dir /b /ad /o-d'
05. ) do echo rd /s /q %%a
06. popd
==========end file C:\CMD\DEMO\SaveFiveDirs.cmd ==========
- - - - - - - end screen capture WinXP MCE 2005 SP2 - - - - - - -

If this appears to do what you intend, remove the word 'echo' from
line 5.
 
H

Helge Haensel

Hallo NG!

Once per week I generate a (backup-)folder named YYYYMMDD.
When there are 5 or more folders the oldest 5+-folder(s)
should be deleted. How can I achieve that?
I checked with the help-command at cmd-prompt, but didnt
succeed with the dir-, for- and other commands.
Sorry, it is to tricky for me.
Thanks.
Hallo, thanks to all you guys for suggestions. I will carefully
study and try them.
This surely will stabilize my backup-strategy.
Vy 73! Helge
 
H

Helge Haensel

Hallo, thanks to all you guys for suggestions. I will carefully
study and try them.
This surely will stabilize my backup-strategy.
Vy 73! Helge
Hallo!
I studied all 3 versions. Will's link is a little bit to complex.
I could have written that also using my VB system. That is what
I tried to avoid.
Phil's and Pegasus's versions are about identical and work perfectly.
Thanks!
Helge
 
P

Pegasus \(MVP\)

Helge Haensel said:
Hallo!
I studied all 3 versions. Will's link is a little bit to complex.
I could have written that also using my VB system. That is what
I tried to avoid.
Phil's and Pegasus's versions are about identical and work perfectly.
Thanks!
Helge
--

If you ever score a reply from Phil Robyn, use it! He must
be the undisputed master of batch files. I've learned a lot
from him.
 
P

Phil Robyn

Pegasus said:
If you ever score a reply from Phil Robyn, use it! He must
be the undisputed master of batch files. I've learned a lot
from him.

Gee, thanks, Pegasus, that means a *lot* coming from you!!! I have
learned a lot from you, too!

==========begin file C:\CMD\DEMO\SaveFiveDirs.cmd ==========
01. @echo off
02. pushd \junkdir
03. for /f "skip=5" %%a in (
04. 'dir /b /ad /o-n'
05. ) do echo rd /s /q %%a
06. popd
==========end file C:\CMD\DEMO\SaveFiveDirs.cmd ==========
 

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