| 1. |
Solve : Batch file to create backup for a week? |
|
Answer» I have a backup file which gets generated everyday with the same file name "contentstore.zip" and gets stored in a specific location C:\contentstore_backups. So each day, the previous copy of the file is overwritten. I want to write a BAT file which renames the file contentstore.zip file to contentstore_currentdate.For instance, if today's date is 04/14/2009, then i want it to renamed as contentstore_20090414.use %date% or %time%. search the forum for examples on how to get date VALUES into variables. Quote However , after 7 days, I want the oldest file to get deleted , so that at a point I have only 7 backups in the folder "contentstore_backups".you can set scheduled tasks every 7 days to execute your batch file. use dir (and some of its OPTIONS) to list files according to time stamp. check the dir /? help for more info. you want something like: Code: [Select]for /f "delims=/ tokens=1-3" %%A in ('date /t') do ( ren C:\contentstore_backups\contentstore.zip contentstore_%%A%%B%%C.zip) The deleting after a week is a little more tricky. You might want to look at VB for that, and to be fair if you're going to look at VB for that you may as well do the whole thing in VB... FBThanks for the inputs. It really helped me with the first part of my problem--renaming of the file with the date in it The second part was: Now for instance, for the next seven days every day we get a new backup file: contentstore_20090414 ----taken on 04/14/2009 contentstore_20090415 ----taken on 04/15/2009 contentstore_20090416 ----taken on 04/16/2009 contentstore_20090417 ----taken on 04/17/2009 contentstore_20090418 ----taken on 04/18/2009 contentstore_20090419 ----taken on 04/19/2009 contentstore_20090420 ----taken on 04/20/2009 On the 8Th day, when we get a new file "contentstore_20090421" on 04/21/2009, I want the oldest backup "contentstore_20090414" taken on 04/14/2009 to get deleted. This ensures that I have only 7 backups at any given time. Since the files are named in this format, can we not sort the filenames and delete the file with the SMALLEST name? Quote from: em on April 14, 2009, 11:10:19 AM Thanks for the inputs. It really helped me with the first part of my problem--renaming of the file with the date in itHmmm... It looks possible with FOR...@OP, set a scheduled task to execute every 7 days. in your batch, you just specify dir ( with a for loop) to get the oldest file. check the dir/? for more info, or search the forum. (been posted many times). Quote from: gh0std0g74 on April 14, 2009, 07:40:42 PM @OP, set a scheduled task to execute every 7 days. in your batch, you just specify dir ( with a for loop) to get the oldest file. check the dir/? for more info, or search the forum. (been posted many times).Ok, set a sheduled task for this: Code: [Select]@echo off dir /b /o:d C:\contentstore_backups > directory set /p delzip=<directory del C:\contentstore_backups\%delzip% del directoryThat should do it.Code: [Select]@echo off pushd C:\contentstore_backups || goto:eof dir/b contentstore.zip || goto:eof >$$$.vbs echo wsh.quit year(date)*10000+month(date)*100+day(date) cscript//nologo $$$.vbs ren contentstore.zip contentstore_%errorlevel%.zip for /f "skip=7" %%a in ('dir/b/on contentstore_*.zip') do del %%a del $$$.vbs popd untestedQuote from: Reno on April 15, 2009, 01:44:05 AM Code: [Select]@echo offMine works for getting rid of the oldest one...as long as there isn't anything in the folder other than the .zips. The Code: [Select]@echo off dir /b /o:d C:\contentstore_backups > directory set /p delzip=<directory del C:\contentstore_backups\%delzip% del directory Echo Oldest file deleted. for /f "tokens=1-3 delims=/ " %%a in ("%date%") do ( set y=%%c set m=%%b set d=%%a echo %%a echo %%b echo %%c ) ren C:\contentstore_backup\contentstore.zip contentstore_%y%%m%%d%.zip echo Zip File has be renamed! You may now close. pauseA combo of my previous code (for deleting the oldest file) and my new one (which does the renaming).Batch Features:Quote from: Helpmeh on April 16, 2009, 05:58:14 AM Mine works for getting rid of the oldest one...as long as there isn't anything in the folder other than the .zips.Answer: Quote from: em on April 14, 2009, 08:49:36 AM However , after 7 days, I want the oldest file to get deleted , so that at a point I have only 7 backups in the folder "contentstore_backups". bug fix: for /f "skip=7" %%a in ('dir/b/o-n contentstore_*.zip') do del %%a looks like the OP didn't come BACK hereQuote from: Reno on April 17, 2009, 12:29:07 AM Batch Features:Answer:Wait...my code works fine, it deletes the oldest one, and then renames the undated zip to what the OP requested.helpmeh, it's logic error. imagine the batch first running, and initially there is only one file contentstore.zip dir /b /o:d C:\contentstore_backups > directory set /p delzip=<directory del C:\contentstore_backups\%delzip% bang, there it goes, today content_store.zip which should be renamed goes into dos black hole. ok, how about if the directory already contain 2,3, or even 4 contentstore_yyyymmdd.zip everytime the batch is run, the numbers of backup will never increase. thus the requierment of 7 backups at any time is not fulfil. the only condition where your batch will works is the folder only contain 8 *.zip file(s) -including the contentstore.zip to be renamed, not LESS, not MORE. and only contentstore*.zip, or else the batch code is buggy. and date retrieving problem, .......Quote from: Reno on April 18, 2009, 10:13:37 PM helpmeh, it's logic error.Oh...now I see. It could be fixed with a FOR loop (now that I know the basic functions of FOR, I love it)...I think. for /f "delims=" %%a in ("directory") do ( set /a loop+=1 ) That should count how many lines are in directory, and... if %loop%==8 del C:\contentstore_backups\%delzip% So the full (hopefully) working code is: Code: [Select]@echo off dir /b /o:d C:\contentstore_backups > directory set /p delzip=<directory for /f "delims=" %%a in ("directory") do ( set /a loop+=1 ) if %loop%==8 del C:\contentstore_backups\%delzip% del directory Echo Oldest file deleted. for /f "tokens=1-3 delims=/ " %%a in ("%date%") do ( set y=%%c set m=%%b set d=%%a ) ren C:\contentstore_backup\contentstore.zip contentstore_%y%%m%%d%.zip echo Zip File has be renamed! You may now close. pauseThat should work...as long as FOR counts the lines... |
|