normally disk space is not a problem but I am going away for christmas
and thought I'd better quickly set up a script to avoid this filling
up the hard disk, but shoold the system stop fr whatever reason, I
will at least have the last X amount of records without causing a
system crash due to lack of disk space.
I am a bit rusty on my DOS batch scripts these days and would
appreciate some assistance on this.
This is almost what I have in mind (just need some help with the disk
space available and a way to delete the oldest file in directory)
***start***
set space_required = 1000000 (1GB)
:check_space
set free_space = disk space available on drive
:check_space
If free_space > space_required
goto exit
else
delete the oldest file in specified directory
endif
:exit
Some comments:
batch has a size limit in large integers and you will need to get your
free space in GB values
To delete the oldest file
for /f "delims=" %%a in ('dir "c:\folder" /b /o-d /a-d') do (
set "file=%%a"
)
del "%file%"
--
Regards,
Mic
An interesting enough question that I took a crack at it ...
@echo off
setlocal
set source="c:\folder"
set wait=300 % seconds %
set repeat=1
set cmd=wmic LOGICALDISK WHERE "deviceID='c:'" get
set cmd=%cmd% freespace /value /every:%wait% /repeat:
:Checkit
for /f "usebackq tokens=2 delims==" %%a in (
`%cmd%%repeat%`) do set freespace=%%a
set freespace=%freespace:~0,-9%
echo %freespace:~0,-1% GBytes @ %date% %time%
if not defined freespace goto :DelFile
if %freespace% lss 10 call :DelFile
set repeat=2 % activates five minute wait after first test %
goto :Checkit % forever %
:DelFile
dir %source% /a-d-h-s /od /b > %temp%\tmp.txt
set /p oldest=< %temp%\tmp.txt
echo del %source%.\%oldest% /q /f
set "oldest="
BTW, Mic, that DEL statement is missing the folder spec.
_______________________
Tom Lavedas
Hmm - a couple of points of criticism, if I may.
First, WMIC isn't installed on ALL versions. XP/H misses out, for instance.
And then I'd use
set lowlimit=123456789
set manyzeroes=00000000000000000000
set lowlimit=%manyzeroes%%lowlimit%
for /f "tokens=3" %%i in ( ' dir /-c x: ' ) do set
currentfree=%manyzeroes%%%i
if q%currentfree:~-20% lss q%lowlimit:~-20% echo not enough space
* setting lowlimit first, without the filler-zeroes allows the low limit to
be varied in a natural format.
* the /-c switch removes the thousands-separator from the DIR list;
significant data in resultant token 3 of the last line.
Filling with many-zeroes means that the negative substring operator can be
used in the IF statement to make the comparison insensitive to the 'natural'
zero-suppressed format. Poking a "q" (or any alphabetic) before each side
forces the comparison to be alphabetic; the "20" uses the last 20 digits
(which should be sufficient...) to be used
This means that any actual mathematical operation is conveniently avoided...
'course, it does mean that the report if required would need to ba
adjusted....
Nice work, Tom.
I modified the case where the disk is full, and the batch file would
have deleted a file and terminated.
@echo off
setlocal
set "source=c:\folder"
set wait=300 % seconds %
set repeat=1
set cmd=wmic LOGICALDISK WHERE "deviceID='c:'" get
set cmd=%cmd% freespace /value /every:%wait% /repeat:
:Checkit
for /f "usebackq tokens=2 delims==" %%a in (
`%cmd%%repeat%`) do set freespace=%%a
set freespace=%freespace:~0,-9%
echo %freespace:~0,-1% GBytes @ %date% %time%
if not defined freespace (
call :DelFile
) else (
if %freespace% lss 10 call :DelFile
)
set repeat=2 % activates five minute wait after first test %
goto :Checkit % forever %
:DelFile
if not exist "%source%.\" goto :EOF
pushd "%source%"
dir /a-d-h-s /od /b > %temp%\tmp.txt
set /p oldest=< %temp%\tmp.txt
echo del "%oldest%" /q /f
set "oldest="
popd
> BTW, Mic, that DEL statement is missing the folder spec.
Yep, thanks for catching that. In your source the del would fail on a
long filename so that's also changed above.
JFTR the DEL command is merely echoed to the console atm.
--
Regards,
Mic
Microsoft should be shot for making different versions with various
features enabled or disabled.
Think of how much easier support would be with a unified version - there
are enough releases of Windows to confuse the unwary anyway.
--
Regards,
Mic
Mic: I guess I was in a bit of a rush and didn't check enough. Thanks
for the fixes.
billious: I thought about using the DIR . /ad | find "free" approach,
but liked the fact that WMIC also provided the timing loop. So, I
went with it, anyway - hoping it would meet the bill for the OP.
_______________________
Tom Lavedas
Thinking about this, if you delete the oldest file every 5 minutes, then it
should maintain the same number of files as long as new files continue being
created. Assuming, the files do not grow dramatically each time a new file
is created, the following should be sufficent. Try it on a test folder where
a new file is created every 5 minutes.
@echo off
ping -n 300 127.0.0.1 >nul
for /f "delims=" %%a in ('dir "c:\folder" /b /o-d /a-d') do (
set "file=%%~fa"
)
del "%file%"
%0
This is what I ended up with & it appears to work. Decided to go with
approx 2Gb in the end. As I am using task schedular to run this
preiodically I did not want the time lapse in the batch.
******deloldfi.bat*************
@echo off
set /A free_space=0
set /A space_required=2147483640
:check_space
for /f "tokens=3 delims= " %%a in ('dir /-C ^|find "bytes free" ') do
(
set /A "free_space=%%a"
)
if %free_space% EQU 0 goto :exit
echo.
echo free_space
echo %free_space%
echo %space_required%
if %free_space% GTR %space_required% goto :exit
for /f "delims=" %%a in ('dir "c:\temp\" /b /o-d /a-d') do (
set "file=%%a"
)
del "c:\temp\%file%"
echo %file% deleted
set free_space=0
goto :check_space
:exit
set space_required=
set free_space=
*******end*************
Seasons greetings
C