(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

April 14. 2010 22:20

ferndale mi

ferndale mi

May 8. 2010 22:52

hi..

hj

May 10. 2010 10:56

I'll try to use this one from your article, though I'm too late to find out this interesting information that you've posted with.

Dog Food Allergy

May 12. 2010 18:50

I wish to know more about this because it actually catches my attention. Keep on posting valuable information about this dude! Thank you so much for sharing.

Hotel U Leva

May 19. 2010 12:45

The article is not loading properly for me. Any ideas why this is? Your other ones are working ok. Thanks.

hometown lapeer

May 28. 2010 00:02

WOW. It was really amazing. I can’t help not to admire your post. It was definitely great. Kudos for an awesome job! :D

Foot Fungus Treatment

June 14. 2010 10:48

www.mywalkies.co.uk We offer anyone who has a busy life-style, or other commitments, who very own a puppy (or two!) the possibility so you may find out a puppy walker who's eager and willing to offer you the assistance. Merely by registering totally totally free, and making your individual profile, you can search for canine walkers within driving distance. It is possible to try their profile, and select who you intend to walk your canine. We also provide canine walkers the opportunity to place your adverts onto your person profile on this web site; whether you have spare time, seeking for added funds, or just want physical exercise with some organization! Once more just merely register totally totally free and inside minutes you can location what it really is essential to offer on your profile page.

dog walk

June 15. 2010 11:03

www.mywalkies.co.uk We provide any person that has a busy way of life, or other commitments, who own a puppy (or two!) the chance every single kid find a puppy walker who is eager and willing to provide the assistance. Merely by registering free of charge, and making your really own profile, you can lookup for puppy walkers within your town. You are able to try their profile, and pick who you would like to stroll your dog. We also provide puppy walkers the opportunity to spot your adverts onto your private profile on this web web site; whether or not you might have spare time, looking for additional money, or just want exercise with some company! Once again just simply register for free of charge and inside of minutes place what it can be important to offer on your profile page.

Dog walkers

June 20. 2010 12:33

Whoa! Many thanks! I always wished to generate in my web page something like that. Can I quote portion of your post to my weblog?

music ludacris

June 26. 2010 12:50

i really enjoyed reading the blog - it was very informative without being boring, something that is very important.

penusi david

July 5. 2010 22:51

The Big 10 Football Nation Forum may be a site exactly where it is possible to examine just about something in regards to the NCAA Big 10 Football Conference. You may possibly also examine about all kinds of other points like other NCAA Football Conferences, other sports activities, everyday chit chat, and all sorts of other topics. There will also be a special VIP Section in which you may acquire, market, trade, or have sports bets with other members. http://www.big10footballnation.net/forums/forum.php

football forum

July 9. 2010 05:05

It is an ironic habit of human beings to run faster when we have lost our way.

pay day loans

July 12. 2010 14:25

It’s nice to finally find a site where the blogger knows what they are talking about.

write research papers

July 15. 2010 02:13

I'll try to use this one from your article, though I'm too late to find out this interesting information that you've posted with.

vibram fivefingers

July 20. 2010 02:17

Aw, this was a extremely high quality post. ou have really put a lot of energy into your article and it is just wonderfull!

Air force one

July 21. 2010 08:21

Thanks a good deal! I truly enjoyed reading this.Looking through these posts and the information you've provided I can appreciate that I still have a lot of things to learn. I will keep reading and keep re-visiting.
http://www.nikeairjordan.cc/air-jordan-6-vi-20/

jordans 6

Add comment


(Will show your Gravatar icon)