![]()
[<<]Message[>>] [<<]Author[>>] [<<]Subject[>>] [<<]Thread[>>]
Number : 4549 Date : 2003-05-07 Author : Garry Deane Subject : Re: Deleting folders based on folder date Size(KB) : 4
--- In xxcopy@yahoogroups.com, "daverunda" wrote: > MY OS is Windows 2000 SP3. The date format is mm-dd-yy > but that can be easily changed (and should be) with no > negative consequences. The deletion of all directories > created more than X (lets assume 5) days ago must be > automated, invoked from a batch file that is scheduled > to run every night at 2:00 AM. The only directories it > needs to check are the directories in the root of the > K:\ drive. For example, lets say there are three > directories located as follows: > k:\05-01-03\ > k:\05-05-03\ > k:\05-10-03\ > I want XXCOPY to simply check the creation date on > these three parent folders and then delete any which > are older than 5 days. Because you'll be running this every night, my previous suggestion of using /TM-5 to delete the directory which was created 5 days ago will work fine. Nevertheless, I'll give you a more general method which will delete all directories older than 5 days. Since you're using W2k, this is pretty easy. But first let me make a few comments on your existing batch file. Note that in the following I've used a date format of yyyy-mm-dd for the directory names and log files but it will work with any format you choose. @echo off setlocal net use k: /delete /y net use k: \\joanne\serverbu-joanne rem the following gets date and time into e-vars rem Requires c:\winnt\CURRENT.BAT ECHO.|DATE>c:\winnt\TEMPDTTM.BAT CALL c:\winnt\TEMPDTTM.BAT These round-about DOS methods for getting the date into an environment variable aren't needed. W2k/XP already has environment variables called %date% and %time% which are automatically set by the system (type "echo %date%" at a cmd prompt). The only drawback with these is that they probably (depending on locale) have characters like "/" and ":" within them which are no good for creating filenames. If you need the current date for filenames, you can use the following to parse the output from a "date/t" command (see "FOR /?" on a cmd line for an explanation of how this works). This assumes a default date format of mm-dd-yyyy. for /f "tokens=2-4 delims=./- " %%a in ('date/t') do ( set mm=%%a&set dd=%%b&set yy=%%c) set today=%yy%-%mm%-%dd% However if you use Xxcopy's date macros, you don't even need to do this. :: md "k:\%DATE%" This is not needed - let Xxcopy create the directory. :: xxcopy "c:\company data" "k:\% DATE%" /y/v/e/s/c/i/h/r/h/ze/nx/on"k:\%DATE%.txt" This is just personal taste but I much prefer to use /BU with a few overriding switches when necessary than to list all the /Y/V/S/H/R etc, etc switches. Also, make use of Xxcopy's date macro to specify date based directories and/or log files. Although it does the same as your command, I would write the previous line as: xxcopy "c:\company data\" k:\/$yyyy-mm-dd$\ /bu/nx/onk:\/$yyyy-mm- dd$.txt :: xxcopy k: /rs/s/h/r/pd0/ed/db#5/fc/y/on"k:\deleted-%DATE%.txt" With the additional batch commands below, you no longer need this unless you want to keep a log of what was deleted. Since I can never seem to get Xxcopy to log deleted files other than on screen, I would use the following to keep a record of which files are about to be deleted. xxcopy k:\ /rmdir/rsy/ed/db#5/fc/L >k:\deleted-%today%.txt So after all that, here's the batch code to delete all directories which were created more than 5 days ago. It uses Xxcopy to create a list of the directories which qualify then uses FOR /F to remove each of these directories. Note that the RD command is disabled by the use of "echo {test}" so you can check that it will do what you want. Remove the "echo {test}" to activate it. xxcopy k:\ /ltree/db#5/fc/dL2/fok:\old-dirs.txt>nul for /f %%a in (k:\old-dirs.txt) do ( if /i not "%%a"=="K:\" echo {test} rd /s/q %%a) del k:\old-dirs.txt net use k: /delete So here's what I would end up with (the RD command is enabled): @echo off setlocal net use k: /delete /y net use k: \\joanne\serverbu-joanne :: Get todays date in yyyy-mm-dd format for /f "tokens=2-4 delims=./- " %%a in ('date/t') do ( set mm=%%a&set dd=%%b&set yy=%%c) set today=%yy%-%mm%-%dd% :: Backup company data to date based directory xxcopy "c:\company data\" k:\/$yyyy-mm-dd$\ /bu/nx/onk:\/$yyyy-mm- dd$.txt :: Save a list of the files which will be deleted xxcopy k:\ /rmdir/rsy/ed/db#5/fc/L >k:\deleted-%today%.txt :: Create a list of top level directories created more :: than 5 days ago which will be deleted xxcopy k:\ /ltree/db#5/fc/dL2/fok:\old-dirs.txt>nul :: Delete all the directories in old-dirs.txt for /f %%a in (k:\old-dirs.txt) do ( if /i not "%%a"=="K:\" rd /s/q %%a) del k:\old-dirs.txt net use k: /delete Garry
This message if part of XXCOPY's message Archive. The archive contains all the messages posted at Yahoo!Groups: XXCOPY.