Computing.Net > Forums > Programming > Batch file for deletion of backups

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Batch file for deletion of backups

Reply to Message Icon

Name: nerdiak
Date: June 11, 2007 at 15:41:26 Pacific
OS: Winxp, 98, DOS
CPU/Ram: any
Product: any
Comment:

I would like a batch file that will delete sub-directories and the files contained in them, but it should keep the most recent 10 directories.
example- c:\test\ directory containing sub-directories named jan012004, jan022004 . . . jan102004.

In the event of a new dir being added to c:test\ ie - jan112004; jan012004 should be deleted by the batch program. However if no new sub directory is added ie jan112004 the oldest sub directory should not be deleted by the batch program ie jan012004.

I have been looking everywhere for something like this to help out and I would appreciate any assistance!

Thanks to all in advance.



Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: June 12, 2007 at 00:41:17 Pacific
Reply:

First decide if it needs to work in DOS.


=====================================
If at first you don't succeed, you're about average.

M2



0

Response Number 2
Name: nerdiak
Date: June 13, 2007 at 11:12:05 Pacific
Reply:

No, I need it for XP, but my belief was that dos and the XP "dos" are similar (though they have their few differences). I would need this program to work on XP.

Please advise.

-Thanks


0

Response Number 3
Name: Mechanix2Go
Date: June 13, 2007 at 17:40:24 Pacific
Reply:

@echo off
setLocal EnableDelayedExpansion

pushd c:\test
for /f "tokens=* skip=10" %%F in ('dir /b/o-d/ad') do echo rd /s /q %%F



=====================================
If at first you don't succeed, you're about average.

M2



0

Response Number 4
Name: nerdiak
Date: June 13, 2007 at 21:59:55 Pacific
Reply:

Wouldn't that command delete the most recent backups (if there are more than 10) I would like a batch to delete all the oldest backups, leaving the most recent 10.

Also, what does the pushd command do?

Below I have posted my code so far, please take a look and correct as necessary ...

This code runs, but doesn't delete anything for some reason ...

@echo off
:: Set backup folder path
set bup="C:\Data Backup"
cd %bup%
pause

:loop

dir %bup% /AD /B /ON>templist
echo set /p folder2del=<templist
pause
call :deleter %folder2del%
goto :eof

:deleter

echo for /f "tokens=* skip=10" %%T in (templist) do
echo ATTRIB %1\*.* -R -A -S -H
echo rd /s /q %1
pause
goto :loop

:eof
del templist
del #
pause

-------------
The next code works, but it doesn't progress to the :eof line to delete the temporary files ...

@echo on

:: Set backup folder path
@set bup="C:\Data Backup"
@cd %bup%

:loop

@dir %bup% /AD /B /ON>templist
@set /p folder2del=<templist
@call :deleter %folder2del%
@goto :eof

:deleter

dir %bup% /AD |find "Dir(s)">#
pause
for /f "tokens=1" %%T in (#) do if '%%T'=='12' goto :eof
@pause
ATTRIB %1\*.* -R -A -S -H
@pause
echo rd /s /q %1
@pause
rd /s /q %1
pause
goto :loop

:eof

del templist
del #
pause


----------
thanks!


0

Response Number 5
Name: Mechanix2Go
Date: June 13, 2007 at 22:11:47 Pacific
Reply:

"Wouldn't that command delete the most recent backups"

No; it deletes the oldest, in excess of 10. Did you try it?

pushd c:\test changes directory and 'remembers where it started'.



=====================================
If at first you don't succeed, you're about average.

M2



0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Batch file for deletion of backups

DOS batch file for deleting folders www.computing.net/answers/programming/dos-batch-file-for-deleting-folders/9005.html

Need batch file to delete old files www.computing.net/answers/programming/need-batch-file-to-delete-old-files/14092.html

Batch file to delete files in a dir www.computing.net/answers/programming/batch-file-to-delete-files-in-a-dir/14599.html