Computing.Net > Forums > Programming > Delete Oldest file * batch *

Delete Oldest file * batch *

Reply to Message Icon

Original Message
Name: Genocide (by Greaper6)
Date: October 17, 2007 at 15:46:38 Pacific
Subject: Delete Oldest file * batch *
OS: WinXP Media Center Editio
CPU/Ram: ...
Model/Manufacturer: Dell
Comment:

Ello again!

I made a batch to run the netstat commands and log them ever 5 minutes. as you may guess that those files will build up ALOT.

The Code:

@ECHO OFF
@TITLE Netstat Logger
@mode con cols=25 lines=5
@SET D1=%date:~-4,10%
@SET D2=%date:~7,-5%
@SET D3=%date:~4,-8%
@CLS
@CD\
@IF NOT exist "Netstat logs" Mkdir "Netstat logs"
@CD "Netstat logs"
:Loop
@CLS
@ECHO @ECHO This log was take at %TIME%! >>%D3%-%D2%-%D1%.txt
@Netstat -a > NUL >>%D3%-%D2%-%D1%.txt
@---------------- >>%D3%-%D2%-%D1%.txt
@ECHO Above is " Netstat -a " loggings !! >>%D3%-%D2%-%D1%.txt
@---------------- >>%D3%-%D2%-%D1%.txt
@Netstat -b > NUL >>%D3%-%D2%-%D1%.txt
@---------------- >>%D3%-%D2%-%D1%.txt
@ECHO Above is " Netstat -b " loggings !! >>%D3%-%D2%-%D1%.txt
@---------------- >>%D3%-%D2%-%D1%.txt
@Netstat -e > NUL >>%D3%-%D2%-%D1%.txt
@---------------- >>%D3%-%D2%-%D1%.txt
@ECHO Above is " Netstat -e " loggings !! >>%D3%-%D2%-%D1%.txt
@---------------- >>%D3%-%D2%-%D1%.txt
@Netstat -n > NUL >>%D3%-%D2%-%D1%.txt
@---------------- >>%D3%-%D2%-%D1%.txt
@ECHO Above is " Netstat -n " loggings !! >>%D3%-%D2%-%D1%.txt
@---------------- >>%D3%-%D2%-%D1%.txt
@Netstat -o > NUL >>%D3%-%D2%-%D1%.txt
@---------------- >>%D3%-%D2%-%D1%.txt
@ECHO Above is " Netstat -o " loggings !! >>%D3%-%D2%-%D1%.txt
@---------------- >>%D3%-%D2%-%D1%.txt
@Netstat -r > NUL >>%D3%-%D2%-%D1%.txt
@---------------- >>%D3%-%D2%-%D1%.txt
@ECHO Above is " Netstat -r " loggings !! >>%D3%-%D2%-%D1%.txt
@---------------- >>%D3%-%D2%-%D1%.txt
@Netstat -s > NUL >>%D3%-%D2%-%D1%.txt
@---------------- >>%D3%-%D2%-%D1%.txt
@ECHO Above is " Netstat -s " loggings !! >>%D3%-%D2%-%D1%.txt
@---------------- >>%D3%-%D2%-%D1%.txt
@CLS
@ECHO.
@Echo Logging has Finished!!
@ECHO.
@ECHO Next log in 5 minutes!!
@Ping.exe localhost -n 300 > NUL


I need to add to this a code that after the 10th file is made, the oldest txt file ( the first one made ) gets deleted.. ty in advance.


Report Offensive Message For Removal


Response Number 1
Name: Razor2.3
Date: October 17, 2007 at 23:12:01 Pacific
Reply: (edit)

Untested:

FOR /F %%a IN ('DIR /a-d ^| FIND /C "."') DO SET count=%%a
IF %count% GTR 10 CALL :DelOld
GOTO :EOF

:DelOld
FOR /F "delims=" %%a IN ('DIR /b /o-d /a-d') DO SET me=%%a
DEL %me%


Report Offensive Follow Up For Removal

Response Number 2
Name: Mechanix2Go
Date: October 18, 2007 at 02:04:46 Pacific
Reply: (edit)

Modified slightly to prevent false count:

==================================
@echo off
setlocal

FOR /F %%a IN ('DIR /b/a-d ^| FIND /v /C ""') DO SET count=%%a
IF %count% GTR 10 CALL :DelOld
GOTO :EOF

:DelOld
FOR /F "delims=" %%a IN ('DIR /b /o-d /a-d') DO SET me=%%a
DEL %me%

===================================

If you just want to keep the 10 newest:

====================================

@echo off

FOR /F "skip=10 delims=" %%a IN ('DIR /b /o-d /a-d') DO (
DEL %%a
)



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

M2



Report Offensive Follow Up For Removal

Response Number 3
Name: Genocide (by Greaper6)
Date: October 18, 2007 at 12:32:14 Pacific
Reply: (edit)

So is this what I need to keep the 10 newest?

@echo off
setlocal

FOR /F %%a IN ('DIR /b/a-d ^| FIND /v /C ""') DO SET count=%%a
IF %count% GTR 10 CALL :DelOld
GOTO :EOF

:DelOld
FOR /F "skip=10 delims=" %%a IN ('DIR /b /o-d /a-d') DO (
DEL %%a
)



Report Offensive Follow Up For Removal

Response Number 4
Name: Mechanix2Go
Date: October 18, 2007 at 12:38:43 Pacific
Reply: (edit)

Just this:
============================
@echo off

FOR /F "skip=10 delims=" %%a IN ('DIR /b /o-d /a-d') DO (
DEL %%a
)



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

M2



Report Offensive Follow Up For Removal

Response Number 5
Name: Genocide (by Greaper6)
Date: October 18, 2007 at 12:41:17 Pacific
Reply: (edit)

Ty, if it works like i want to, Ill know in the next ten days :) Untill then. P34C3


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Delete Oldest file * batch *

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




Have you ever used OpenOffice?

Yes, as my main suite.
Yes, occationally.
Yes, but only once.
No, never.


View Results

Poll Finishes In 5 Days.
Discuss in The Lounge