Trouble calling DeleteIfOld as a function

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
rabaggett
Posts: 2
Joined: 17 May 2017 04:52

Trouble calling DeleteIfOld as a function

#1 Post by rabaggett » 17 May 2017 05:13

How do I delete all files older than 15 days in a folder and it's subfolders? I get 'DeleteIfOld is not recognized...' error from the file below.

What Am I Doing Wrong?

Code: Select all

@echo off
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION

call:jdate tnow "%%date%%"

FOR /R %%F IN (*.bmp) DO (
    call:ftime tfile "%%F"
    set /a "days=tnow-tfile"
    call DeleteIfOld %%F 15 %%tnow%%
)

ECHO.&PAUSE&GOTO:EOF


::-----------------------------------------------------------------------------------
::-- Functions start below here
::-----------------------------------------------------------------------------------


:ftime JD filename attr -- returns the file time in julian days
::                      -- JD    [out]    - valref file time in julian days
::                      -- attr  [in,opt] - time field to be used, creation/last-access/last-write, see 'dir /?', i.e. /tc, /ta, /tw, default is /tw
:$created 20060101 :$changed 20090322 :$categories DateAndTime
:$source http://www.dostips.com
SETLOCAL
set file=%~2
set attr=%~3
if not defined attr (call:jdate JD "- %~t2"
) ELSE (for /f %%a in ('"dir %attr% /-c "%file%"|findstr "^^[0-9]""') do call:jdate JD "%%a")
( ENDLOCAL & REM RETURN VALUES
    IF "%~1" NEQ "" (SET %~1=%JD%) ELSE (echo.%JD%)
)
EXIT /b


:jdate JD DateStr -- converts a date string to julian day number with respect to regional date format
::                -- JD      [out,opt] - julian days
::                -- DateStr [in,opt]  - date string, e.g. "03/31/2006" or "Fri 03/31/2006" or "31.3.2006"
:$reference http://groups.google.com/group/alt.msdos.batch.nt/browse_frm/thread/a0c34d593e782e94/50ed3430b6446af8#50ed3430b6446af8
:$created 20060101 :$changed 20080219
:$source http://www.dostips.com
SETLOCAL
set DateStr=%~2&if "%~2"=="" set DateStr=%date%
for /f "skip=1 tokens=2-4 delims=(-)" %%a in ('"echo.|date"') do (
    for /f "tokens=1-3 delims=/.- " %%A in ("%DateStr:* =%") do (
        set %%a=%%A&set %%b=%%B&set %%c=%%C))
set /a "yy=10000%yy% %%10000,mm=100%mm% %% 100,dd=100%dd% %% 100"
set /a JD=dd-32075+1461*(yy+4800+(mm-14)/12)/4+367*(mm-2-(mm-14)/12*12)/12-3*((yy+4900+(mm-14)/12)/100)/4
ENDLOCAL & IF "%~1" NEQ "" (SET %~1=%JD%) ELSE (echo.%JD%)
EXIT /b

:DeleteIfOld name days tnow -- deletes file or directory if older than given number of days
::                          -- name [in] - name of file or directory
::                          -- days [in] - number of days to expire
::                          -- tnow [in] - today's date in julia days
:$created 20060101 :$changed 20080219 :$categories DateAndTime,FileOperation
:$source www.DosTips.com
SETLOCAL
set "days=%~2"
set "tnow=%~3"
call:ftime tfile "%~1"
set /a "diff=tnow-tfile"
if %diff% LEQ %days% EXIT /b
set "attr=%~a1"
rem ECHO.%attr%, %attr:~0,1%, %~nx1 is %diff% days old
if /i "%attr:~0,1%"=="d" (
    rd /Q /S "%~1"
) ELSE (
    del /Q "%~1"
)
EXIT /b

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Trouble calling DeleteIfOld as a function

#2 Post by aGerman » 17 May 2017 13:41

Whenever you call a label you need to prepend the colon to the label name.

Code: Select all

FOR /R %%F IN (*.*) DO (
    call :ftime tfile "%%F"
    set /a "days=tnow-tfile"
    call :DeleteIfOld "%%F" 15 %%tnow%%
)

Steffen

rabaggett
Posts: 2
Joined: 17 May 2017 04:52

Re: Trouble calling DeleteIfOld as a function

#3 Post by rabaggett » 18 May 2017 07:47

Problem solved. Works perfectly. Thanks much!

Post Reply