DOS Backup / Cleanup Script with Retention

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Barn01
Posts: 1
Joined: 27 Nov 2015 17:05

DOS Backup / Cleanup Script with Retention

#1 Post by Barn01 » 27 Nov 2015 17:09

Maybe someone can help me out.

I have a users XP computer that has a DB where some files are created and or updated daily and I want to make a script so that the DB is incrementally updated (not copied) for a period of 90days. All files/folders within DB older than 90days should be deleted. This way I can control the DB size to no more than 90days of last modified retention.

Here's is what I have so far and it's basically deleting everything every other day. I'll remind everyone that it's on an XP box and that the syntax for forfiles is different than that of Win7 (since I have to install forfiles on XP and that's the only ver I could find)

I'm a little green at DOS script and I haven't programmed for a while so please excuse the tidiness of the script.

Code: Select all

@ECHO
CLS
SETLOCAL ENABLEDELAYEDEXPANSION

SET unit_num=21

NET USE S: /delete
NET USE S: \\10.xxx.xxx.xx\xxx xxx_xxx /USER:xxx%unit_num%\xxx

::Setting Variables

SET max_days_db=90
SET max_days_log=10
SET today=%date:~10,4%-%date:~4,2%-%date:~7,2%
SET sourcedrive=d:\db\*.*
SET backupdrive=s:\unit%unit_num%\
SET logfile=%backupdrive%logs\%today%_backup_log.txt

ECHO %today% %time% Backup Starting>>%logfile%
ECHO Files being backed up from %sourcedrive% to %backupdrive%db ...>>%logfile%
ECHO.>>%logfile%

XCOPY "%sourcedrive%" "%backupdrive%db" /d /e /c /i /h /r /k /y >>%logfile%

IF ErrorLevel 0 @Echo Files Copied Without Error>>%logfile%
IF ErrorLevel 1 @Echo No Files Found to Copy>>%logfile%
IF ErrorLevel 2 @Echo User Pressed CTRL+C To Stop Backup>>%logfile%
IF ErrorLevel 4 @Echo Invalid Drive or Low Memory>>%logfile%
IF ErrorLevel 5 @Echo Disk Write Error Occured>>%logfile%

ECHO.>>%logfile%
ECHO %time% Backup to %backupdrive%db Complete>>%logfile%

::Start a log of all deleted files

ECHO.>>%logfile%
ECHO %time% Deleting folders older than %max_days_db% days Started>>%logfile%
ECHO Folders being deleted from %backupdrive%db ... >>%logfile%
ECHO.>>%logfile%

::Delete folders older than %max_days_db% days in %backupdrive%

FORFILES -p%backupdrive%db\ -m*.* -s -d-%max_days_db% -c"CMD /c IF @ISDIR==TRUE RD /s /q @PATH"2>NUL>>%logfile%

::Delete log files older than %max_days_log% Days in the Log File directory

ECHO.>>%logfile%
ECHO %time% Deleting log files older than %max_days_log% days Started>>%logfile%
ECHO Files being deleted from %backupdrive%logs ...>>%logfile%
ECHO.>>%logfile%

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: DOS Backup / Cleanup Script with Retention

#2 Post by foxidrive » 27 Nov 2015 22:03

robocopy from SysInternals (now microsoft) has a switch to copy files etc by age in days and it can be made to just report the files using the /L switch.

There is a version for XP days - you'll have to see which switches it supports.
In WIn 8.1 you can use this to acquire the names, and a for loop to delete them - it only echos the del command to the screen at present.

Note: change d:\folder to the source folder

Code: Select all

:: delete files older than 90 days

@echo off
for /f "tokens=*" %%a in ('robocopy "d:\misc" "%temp%" /XJ /NP /FP /NDL /NS /NC /NJH /NJS /minage:90 /L') do echo del "%%a"
pause

Post Reply