Delete old files

Microsoft Windows
Post Reply
User avatar
MigrationUser
Posts: 336
Joined: 2021-Jul-12, 1:37 pm
Contact:

Delete old files

Post by MigrationUser »

24 Jul 2009 17:10
blockie

I backup to a second HDD, D: drive. There will be a lot of backups on that drive. They take the name according to the date they were made. ie; Fri 07.24.2009, Thu 07.23.2009,etc. Each day my Backup program adds one more. Eventually I will run out of disk space and have to delete the oldest files to make room. The logic in laymen terms would be "If used space D; drive is more than 250 GB delete the oldest two files". Can anyone tell me how to do this with a batch file?

----------------------------

#2 25 Jul 2009 17:32
bluesxman


It'll be much easier to query the free space than the amount of space used.

Code: Select all

@echo off

REM define the parameters to use later on

REM minimum amount of free space before trying to delete backups (megabytes)
set min.mb=2000

REM number of backups to delete, if required
set delete=2

REM location where the backups are kept
set backups=d:\

REM run the "FSUTIL" command and hack the output apart to get the free space in bytes; set this as a variable
for /f "usebackq tokens=2 delims=:" %%a in (`fsutil volume diskfree d: ^| find "Total # of avail free bytes"`) do set free.mb=%%a

REM clean up any white space on the variable
set free.mb=%free.mb: =%

REM trim off the last 6 characters (to give an approximation of the free space in megabytes -- due to limitations on the size of numbers CMD can work with)
set free.mb=%free.mb:~0,-6%

REM if trimming the last 6 character resulted in an empty variable (IE free space was < 999999 bytes) then assume 0 bytes free
if not defined free.mb (set free.mb=0)

REM compare the free space to the minimum we defined; if the threshold hasn't been reached do nothing,
REM otherwise call a sub-procedure to delete some backups
if %free.mb% GTR %min.mb% (echo:There's enough free space [Approx %free.mb%MB]) ELSE (call :delete.backups)

pause

REM end of the script
goto :EOF

REM start of a sub-procedure
:delete.backups

REM enable delayed variable expansion -- too difficult to explain what this means here, check the main site
setlocal enabledelayedexpansion

REM set a counter to 0, we'll use this to count files when deciding what to delete
set count=0

REM obtain a bare (/b) directory listing of only directories (/a:d) contained in location defined by "%backups%", sorted in date order (/o:d)
for /f "usebackq tokens=*" %%a in (`dir /a:d/b/o:d "%backups%"`) do (
    REM count how far we are in the directory listing
    set /a count+=1
    REM if we're at line <= the amount defined by "%delete%", we need to delete the file; otherwise exit from the "for" loop by forcefully exiting the sub-procedure
    if !count! LEQ %delete% (echo:I'm deleting "%backups%%%a") ELSE (endlocal & exit /b)
)

REM the script isn't likely to get this far, but we ought to close the "setlocal" above if it does
endlocal

REM exit the sub-procedure and return to where we "called" it above
goto :EOF
I've not actually put the commands in to remove the backups, just a place holder so you can put it there, and I've assumed that they are in directories rather than files (change "dir /a:d" to "dir /a:-d" if they are files).
Edited to include some remarks and tidy up a couple of things

Last edited by bluesxman (25 Jul 2009 18:39)

cmd | *sh | ruby | chef

----------------------------

#3 25 Jul 2009 18:07
blockie


Thank you bluesman,

Because I'm really a novice when it come to interrupting your batch file. Would you please annotate it with REM so I can figure out what each section does. I will probably test it in sections. In its final configuration it will run unattended so, then, I will not need the echo commands. Please leave them in now for testing.

----------------------------

#4 26 Jul 2009 17:50
blockie


I have searched the commands and found "delolder". It appears that I can use this without determining the free space, etc. All I would have to do is figure out how many backup files would go on the 300Gb hard disk, they are all approximately the same size, and each time I make a backup delete the oldest file.

I want to test this theory out. However, it requires that I have "delolder,cmd" installed on my computer, but I don't know where to download it from. Any suggestions?

----------------------------

#5 27 Jul 2009 03:02
sarbjitsinghgill


Can you use following

Code: Select all

DelOldLogs.bat   I am using with  win 2k and 2k3 .

@echo off
if %1.==. goto :eof

echo %date%--%time% >>%0.log

forfiles.exe /p H:\logs S00*.log /d -%1 /c "cmd /c del  @path"

echo %errorlevel% >>%0.log

goto :eof
The syntax will be

Code: Select all

DelOldLogs.bat <days>
For example DelOldLogs.bat 5
will delete all files before 4 days from today.

The code is complete batch file expecting num of days back from today. If you want keep 5 days' files then you specify 6.

Main code is forfiles line. Other two lines are for logging output or errors.

This batch file can be scheduled to run daily.

Last edited by sarbjitsinghgill (27 Jul 2009 03:09)

Sarbjit Singh Gill
IBM certified DBA, MQ Solution Developer

----------------------------

#6 27 Jul 2009 03:09
blockie


The following what, Sarbjit?

----------------------------

#7 27 Jul 2009 03:12
sarbjitsinghgill


blockie you may refer to forfiles syntax at

https://ss64.com/nt/forfiles.html

Last edited by sarbjitsinghgill (27 Jul 2009 03:13)

Sarbjit Singh Gill
IBM certified DBA, MQ Solution Developer

----------------------------

#8 27 Jul 2009 03:34
blockie


Tried it to select files older than 100 days. It was interactive so I have to figure out how to make it run from a batch file and then delete the files selected.
Bill

----------------------------

#9 27 Jul 2009 08:37
sarbjitsinghgill


Well, as far as I understand, it is good for 99 days. I mean two digits days.
If you need to select files older than that, can you supply this parameter as date. You might need to calculate date dynamically.
I do not have DateMath script, if you need exact date via CMD script you may consider using one on this (ss64.com) site.

Alternatively you may search xxcopy.exe online. I did not need that because for me forfiles has worked very well.

http://www.softrecipe.com/System-Utilit ... xcopy.html

Use /DB#<n> and /RS switches with XXCOPY.EXE where you will be able to mentions days,months or even years.
Please note that I have not used this, so be careful.

Last edited by sarbjitsinghgill (27 Jul 2009 08:42)

Sarbjit Singh Gill
IBM certified DBA, MQ Solution Developer

----------------------------

#10 27 Jul 2009 13:42
sarbjitsinghgill
blockie wrote:

Tried it to select files older than 100 days. It was interactive so I have to figure out how to make it run from a batch file and then delete the files selected.
Bill
Interactive -ness can be handled via input redirection. If needed you feed responses using < my_reponse.txt

I think xxcopy.exe will be better option.

Thanks

Last edited by sarbjitsinghgill (27 Jul 2009 13:44)

Sarbjit Singh Gill
IBM certified DBA, MQ Solution Developer

----------------------------

#11 27 Jul 2009 16:01
blockie


I wrote a very small bt file to incorporate "delplder.cmd". When I run the bat I get the error msg that delolder is not an internal or external command. How do I use "delolder,cmd"?

The current bat file I am testing is ;

Code: Select all

D:
pause
Call delolder.cmd
pause        REM:  this is where I can read the error msg.
----------------------------

#12 27 Jul 2009 17:12
sarbjitsinghgill


send me output of dir d:*.cmd

To me it seems that your editor might have saved it some other place.

also if file is there then type d:delolder.cmd

Last edited by sarbjitsinghgill (27 Jul 2009 17:15)

Sarbjit Singh Gill
IBM certified DBA, MQ Solution Developer

----------------------------

#13 27 Jul 2009 17:33
sarbjitsinghgill


Bill,

see here I created a .cmd file with echo command

Code: Select all

    D:\SGILL\WORK>echo echo hello>gill.cmd

Code: Select all

    D:\SGILL\WORK>dir d:*.cmd
    Volume in drive D is data
    Volume Serial Number is 54CB-4745

    Directory of D:\SGILL\WORK

    27/07/2009  12:23 PM                12 gill.cmd
                   1 File(s)             12 bytes
                   0 Dir(s)  45,431,144,448 bytes free
Then I called it 4 different ways

1.

Code: Select all

D:\SGILL\WORK>call gill

D:\SGILL\WORK>echo hello
hello
2.

Code: Select all

D:\SGILL\WORK>call gill.cmd

D:\SGILL\WORK>echo hello
hello
3.

D:\SGILL\WORK>gill.cmd

D:\SGILL\WORK>echo hello
hello

4.

Code: Select all

D:\SGILL\WORK>gill

D:\SGILL\WORK>echo hello
hello

D:\SGILL\WORK>
Last edited by sarbjitsinghgill (27 Jul 2009 17:35)

Sarbjit Singh Gill
IBM certified DBA, MQ Solution Developer

----------------------------

#14 27 Jul 2009 18:24
blockie


Sarbjit,
Thanks for your concern and help.
I worked on and tested a bat file using the command "delolder.exe". Finally got the syntax to where I wanted. Works great. Here it is;

Code: Select all

Setlocal
D:
c:\scripts\delolder *.* /keep:30
----------------------------

#15 28 Jul 2009 00:14
Simon Sheppard
blockie wrote:

I have searched the commands and found "delolder". It appears that I can use this without determining the free space, etc. All I would have to do is figure out how many backup files would go on the 300Gb hard disk, they are all approximately the same size, and each time I make a backup delete the oldest file.

I want to test this theory out. However, it requires that I have "delolder,cmd" installed on my computer, but I don't know where to download it from. Any suggestions?
Here
https://ss64.com/nt/syntax-delolder.html

----------------------------

#16 28 Jul 2009 15:45
JWG
blockie wrote:

I have searched the commands and found "delolder". It appears that I can use this without determining the free space, etc.
I've found a few other utilities that help do this...
BACKUP9.exe http://gammadyne.com/cmdline.htm
DELOLD.exe http://www.xs4all.nl/~chi/utilities/index.html
DELAGE.exe http://home.mnet-online.de/horst.muc/win.htm

The BACKUP9 doesn't delete by date, it goes by generations. All work, each as advantages.

Consider backing up daily for either a week or a month, then doing a backup monthly. Extend this to quarters & years and you can keep your data much longer in the same space. (Probably well beyond the point where program modifications render the data useless!)

----------------------------

#17 28 Jul 2009 16:38
bluesxman


On the subject of what JWG says -- I have an automated backup running on here that uses the command line version in WinRAR to compress files with the "ARCHIVE" attribute set (Windows sets this "on" when a file is updated) and then turn of the attribute. After an initial backup of all files, it now only saves the files I've updated each day. It used to chew through 250MB daily, in the last 5 weeks it's backed up a minuscule 25MB.

cmd | *sh | ruby | chef
Post Reply