(Lviv community of .NET developers)

Delete Old files from Command Line

January 29, 2008 11:23 by alexk

Very often administrator in any company have to solve simple problem: delete from server old files. How we can do this?

  1. Write command line script that will do this for us and run it periodically by windows scheduler service
  2. write custom application that will do the same
  3. find freeware or shareware with required functionality

In our case I want to introduce COMMAND LINE version of problem solving


This is script that works with DATETIME data types in command line. I name it delete_old_files.cmd

@echo off
SET START_FLD=%1
SET DIFF_DAYS=%2
:: Read the Date format from the registry
::call :ReadDateFormat

:: change format to universal one dd.mm.yyyy and separator DOT
SET sDate=.
SET iDate=1

:: get TODAY value
for /F "tokens=6" %%d in ('net time \\%COMPUTERNAME%^|findstr">\\%COMPUTERNAME%^|findstr /I /C:"%COMPUTERNAME%"') do set today=%%d
call :ConvertDateToJDate %today%
SET JToday=%JDate%

:: change format to COMPUTER locale format. we set DD.MM.YYYY and separator char DOT
SET sDate=.
SET iDate=1

for /r "%START_FLD%" %%i in (*.*) do call :DeleteIfOld "%%i" %%~fsi %%~ti 

goto :EOF

::===================================::
::   -   S u b r o u t i n e s   -   ::
::===================================::
:DeleteIfOld
:: %1 - full file path
:: %2 - full file path in old DOS 8.3 format
:: %3 - file date and time
call :ConvertDateToJDate %3
if %JToday% GTR %JDate% (
  set /a diff= %JToday% - %JDate%
) else (
  set /a diff= 0
)

if %diff% GTR %DIFF_DAYS% (
  ECHO %1 File age is in days : %diff%
  del %2
)

:: AND now delete old files
if %diff% GTR %DIFF_DAYS% del %2
goto :EOF

:ConvertDateToJDate
:: %1 - date to parse and convert

call :ParseDate %1
CALL :JDate %GYear% %GMonth% %GDay%
goto :EOF

:JDate
:: Convert date to Julian
:: %1 - YYYY
:: %2 - MM
:: %3 - DD
:: Returns   : Julian date in variable JDate

: First strip leading zeroes
SET MM=%2
SET DD=%3
IF 1%MM% GTR 20 SET MM=%MM:~1%
IF 1%DD% GTR 20 SET DD=%DD:~1%
:: Algorithm based on Fliegel-Van Flandern algorithm from the Astronomical Almanac,
:: provided by Doctor Fenton on the Math Forum (http://mathforum.org/library/drmath/view/51907.html),
:: and converted to batch code by Ron Bakowski.
SET /A Month1 = ( %MM% - 14 ) / 12
SET /A Year1  = %1 + 4800
SET /A JDate  = 1461 * ( %Year1% + %Month1% ) / 4 + 367 * ( %MM% - 2 -12 * %Month1% ) / 12 - ( 3 * ( ( %Year1% + %Month1% + 100 ) / 100 ) ) / 4 + %DD% - 32075

:: cleanup variables
FOR %%A IN (Month1 Year1) DO SET %%A=
goto :EOF

:GDate
:: Convert Julian date back to "normal" Gregorian date
:: Argument : Julian date
:: Returns  : YYYY MM DD in variable GDate

:: Algorithm based on Fliegel-Van Flandern algorithm from the Astronomical Almanac,
:: provided by Doctor Fenton on the Math Forum (http://mathforum.org/library/drmath/view/51907.html),
:: and converted to batch code by Ron Bakowski.

SET /A P      = %1 + 68569
SET /A Q      = 4 * %P% / 146097
SET /A R      = %P% - ( 146097 * %Q% +3 ) / 4
SET /A S      = 4000 * ( %R% + 1 ) / 1461001
SET /A T      = %R% - 1461 * %S% / 4 + 31
SET /A U      = 80 * %T% / 2447
SET /A V      = %U% / 11
SET /A GYear  = 100 * ( %Q% - 49 ) + %S% + %V%
SET /A GMonth = %U% + 2 - 12 * %V%
SET /A GDay   = %T% - 2447 * %U% / 80
:: Clean up the mess
FOR %%A IN (P Q R S T U V) DO SET %%A=
:: Add leading zeroes
IF 1%GMonth% LSS 20 SET GMonth=0%GMonth%
IF 1%GDay%   LSS 20 SET GDay=0%GDay%
:: Return value
SET GDate=%GYear% %GMonth% %GDay%
goto :EOF

:ParseDate
:: Parse (Gregorian) date depending on registry's date format settings
:: Argument : Gregorian date in local date format,
:: Requires : sDate (local date separator), iDate (local date format number)
:: Returns  : GYear (4-digit year), GMonth (2-digit month), GDay (2-digit day)

:: MM/DD/YYYY
IF %iDate%==0 FOR /F "TOKENS=1-3 DELIMS=%sDate%" %%A IN ('ECHO.%1') DO (
  SET GYear=%%C
  SET GMonth=%%A
  SET GDay=%%B
)

:: DD/MM/YYYY
IF %iDate%==1 FOR /F "TOKENS=1-3 DELIMS=%sDate%" %%A IN ('ECHO.%1') DO (
  SET GYear=%%C
  SET GMonth=%%B
  SET GDay=%%A
)

:: YYYY/MM/DD
IF %iDate%==2 FOR /F "TOKENS=1-3 DELIMS=%sDate%" %%A IN ('ECHO.%1') DO (
  SET GYear=%%A
  SET GMonth=%%B
  SET GDay=%%C
)
goto :EOF

:ReadDateFormat
:: Read the Date format from the registry.
:: Arguments : none
:: Returns   : sDate (separator), iDate (date format number)

:: First, export registry settings to a temporary file:
START /W REGEDIT /E "%TEMP%.\_TEMP.REG" "HKEY_CURRENT_USER\Control Panel\International"

:: Now, read the exported data:
FOR /F "tokens=1* delims==" %%A IN ('TYPE "%TEMP%.\_TEMP.REG" ?| FIND /I "iDate"') DO SET iDate=%%B
FOR /F "tokens=1* delims==" %%A IN ('TYPE "%TEMP%.\_TEMP.REG" ?| FIND /I "sDate"') DO SET sDate=%%B

:: Remove the temporary file:
DEL "%TEMP%.\_TEMP.REG"

:: Remove quotes from the data read:
:: SET iDate=%iDate:"=%
FOR %%A IN (%iDate%) DO SET iDate=%%~A

:: SET sDate=%sDate:"=%
FOR %%A IN (%sDate%) DO SET sDate=%%~A
goto :EOF

And this is the second part of th script. I named it cleanup.cmd

@echo off

ECHO -= CLEANUP BEGIN =-

set DIFF_DATE_INTRL=1

call delete_old_files.cmd .\ArtfulbitsWebDB3 %DIFF_DATE_INTRL%
call delete_old_files.cmd .\confluence_db %DIFF_DATE_INTRL%
call delete_old_files.cmd .\confluence_db3 %DIFF_DATE_INTRL%
call delete_old_files.cmd .\Euro3 %DIFF_DATE_INTRL%
call delete_old_files.cmd .\MealDB %DIFF_DATE_INTRL%
call delete_old_files.cmd .\time_tracker %DIFF_DATE_INTRL%
call delete_old_files.cmd .\track_studio_1 %DIFF_DATE_INTRL%
call delete_old_files.cmd .\logs %DIFF_DATE_INTRL%

ECHO -= CLEANUP DONE =-

I can notice several issues:

  1. Script must be configured according to date/time format set on computer where sript planned to execute
  2. be careful with script running - delete operation is not always "exists in undo"

Enjoy


Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Comments

October 7. 2011 13:24

I loved this article

Pictures and Photos

October 7. 2011 17:19

Please write more of this

Landscape photos

Add comment


(Will show your Gravatar icon)