Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Autoexec.bat routine to delete files based upon date in a specific directory

Posted on 2007-04-11
4
Medium Priority
?
1,139 Views
Last Modified: 2011-10-03
Windows CE- need to create a dos routine that will run in the autoexec.bat file.  This routine needs to simply scan a preset directory for files older than (current date minus XX days) and delete those files.  nothing else.  simple.
0
Comment
Question by:webnetxpress
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 

Author Comment

by:webnetxpress
ID: 18900213
what, no takers?  I'll increase the points to 250!!!!!!

and let me narrow it down-
I need the autoexec to parse a directory (say c:\temp\alarms\) and delete any file older than (today's date minus 90 days).  this can be done with a single command line or a subroutine, by directly deleting the file or moving the affected ones to another temp directory and then deleting them there, whatever is easiest...

so come on, someone must know a way to do this?????
0
 
LVL 11

Accepted Solution

by:
asian_niceguy earned 1000 total points
ID: 18936029
The following link may be of interest http://www.computing.net/programming/wwwboard/forum/13717.html

In any case here is the code. Save it as DELOLD.BAT, and put it in the directory to be parsed. In your autoexec put a command to run DELOLD.BAT /90 and you *should* be dandy...

@echo off
SET OLDERTHAN=%1
IF NOT DEFINED OLDERTHAN GOTO SYNTAX
set FileDir=%~2

if "%FileDir%"=="" set FileDir=.

for /f "tokens=2" %%i in ('date /t') do set thedate=%%i

set mm=%thedate:~0,2%
set dd=%thedate:~3,2%
set yyyy=%thedate:~6,4%

set /A dd=%dd% - %OLDERTHAN%
set /A mm=%mm% + 0

:LOOPDATE

if /I %dd% GTR 0 goto DONE
set /A mm=%mm% - 1
if /I %mm% GTR 0 goto ADJUSTDAY
set /A mm=12
set /A yyyy=%yyyy% - 1

:ADJUSTDAY
if %mm%==1 goto SET31
if %mm%==2 goto LEAPCHK
if %mm%==3 goto SET31
if %mm%==4 goto SET30
if %mm%==5 goto SET31
if %mm%==6 goto SET30
if %mm%==7 goto SET31
if %mm%==8 goto SET31
if %mm%==9 goto SET30
if %mm%==10 goto SET31
if %mm%==11 goto SET30
if %mm%==12 goto SET31

:SET31
set /A dd=31 + %dd%
goto LOOPDATE

:SET30
set /A dd=30 + %dd%
goto LOOPDATE

:LEAPCHK
set /A tt=%yyyy% %% 4
if not %tt%==0 goto SET28
set /A tt=%yyyy% %% 100
if not %tt%==0 goto SET29
set /A tt=%yyyy% %% 400
if %tt%==0 goto SET29

:SET28
set /A dd=28 + %dd%
goto LOOPDATE

:SET29
set /A dd=29 + %dd%

:DONE
if /i %dd% LSS 10 set dd=0%dd%
if /I %mm% LSS 10 set mm=0%mm%
for %%i in (%FileDir%\*.*) do (set FileName=%%i call :PROCESSFILE %%~ti)

set mm=
set yyyy=
set dd=
set thedate=
goto EXIT

:SYNTAX
ECHO.
ECHO USAGE:
ECHO DELOLD X [Dir]
ECHO Where:
echo X is the number of days previous to Today.
echo Dir is the optional directory where files exist. Defaults to current directory.
ECHO.
ECHO EX: "DELOLD 5" Deletes files older than 5 days.
echo "DELOLD 120 c:\temp Deletes files from the c:\temp directory that are older
echo than 120 days.
GOTO EXIT

:PROCESSFILE
set temp=%1

set fyyyy=%temp:~6%

if /I %fyyyy% LSS 100 set fyyyy=20%fyyyy%
if /I %fyyyy% GTR 2069 set fyyyy=19%temp:~6%

set fmm=%temp:~0,2%
set fdd=%temp:~3,2%

:: +*************************************+
:: | This is where the files are deleted |
:: | Change the ECHO command to DEL to |
:: | delete. ECHO is used for test. |
:: +*************************************+

if /I %yyyy%/%mm%/%dd% GEQ %fyyyy%/%fmm%/%fdd% (echo %FileName%)

set temp=
set fyyyy=
set fmm=
set fdd=

:EXIT
0

Featured Post

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question