Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I have a batch file that creates a new directory named the current date using the mmddyyyy format. It then copies the contents of a directory to the newly created directory in the backup directory. I am trying to add to it so that it will automatically remove and delete without prompting a backup directory that is more than 7 days old. This is what the batch file already has in it.
@echo off
FOR /F "TOKENS=1* DELIMS= " %%A IN ('DATE/T') DO SET CDATE=%%B
FOR /F "TOKENS=1,2 eol=/ DELIMS=/ " %%A IN ('DATE/T') DO SET mm=%%B
FOR /F "TOKENS=1,2 DELIMS=/ eol=/" %%A IN ('echo %CDATE%') DO SET dd=%%B
FOR /F "TOKENS=2,3 DELIMS=/ " %%A IN ('echo %CDATE%') DO SET yyyy=%%B
SET date=%mm%%dd%%yyyy%
md C:\backup\%date%
xcopy /e C:\data\*.* C:\backup\%date%

A couple of comments.
[1] This forum has been around the block with "date math". Not easy. I think it was dtech10 who finally hammered out a "julian date" BAT.
[2] w2k has a built in var called DATE. Depending on your system date layout you can:
echo %date:~10,4%%date:~7,2%%date:~4,2%
[in my case]or
set myVAR=%date:~10,4%%date:~7,2%%date:~4,2%
and nevermind all those FORs.
[3] Resetting DATE in the BAT is probably a receipe for trouble.
[4] If it will suit your purposes, I can work up a BAT which will keep the most recent x number of backups.
If at first you don't succeed, you're about average.M2

I realized I was making it much harder than it had to be. Since I only needed to keep it for seven days I renamed the folders after the day of the week instead and rewrote the batch file as follows.
FOR /F "TOKENS=1* DELIMS= " %%A IN ('DATE/T') DO SET day=%%A
rmdir /s /q C:\backup\%day%
md C:\backup\%day%
xcopy /e C:\sms\*.* C:\backup\%day%I feel very silly sorry to waste anyone's time.

Great solution.
The simplest way is usually the best.
If at first you don't succeed, you're about average.M2

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |