Link to home
Start Free TrialLog in
Avatar of Sterna
Sterna

asked on

Batch file to Delete files older than x hours

To the brain-boffins out there:
I need to delete all .pdf in a folder older than 24h, but I don't know much about batch files. I'll probably run it via a scheduled task.
Thanx for your expert advice.
sl
Avatar of _nn_
_nn_

How about this?

@       echo off


rem     +====================================================================
rem     | This package needs the new CMD.EXE extensions to be loaded and
rem     | will refuse to run, if they can not be enabled.
rem     +====================================================================

        verify other 2>NUL
        setLocal enableExtensions
        if errorLevel 1 (
            echo.
            echo *ERROR*
            echo.
            echo Sorry - this program requires that extensions to CMD.EXE are
            echo enabled.  Please consult your manual for instruction on how
            echo to proceed.

            goto :EOF
        )

        set delcount=0

rem     Use the DIR command to get a simple listing of file names sorted by date.
rem     The file Timestamp.log marks the time when this script was last run.
rem     Assuming that this was at least 24 hours ago, any files that is sorted
rem     *before* the timestamp is safe to delete.  When the timestamp file is
rem     encountered, we are done.

        for /f "delims=" %%f in ('dir /b /o:d') do (

            if /i "%%f" == "Timestamp.log" goto :done
            if /i "%%~xf" == ".PDF" call :delete "%%f"
        )
:done

rem     Update the Timestamp.log file.  The update 'moves' the log file
rem     ahead in the list of files sorted by date.  Any PDF file modified
rem     after the new timestamp will thus not be deleted the next the
rem     del_marked script is run.

        echo %date% %time% - deleted %delcount% files >> "Timestamp.log"
        echo %date% %time% - deleted %delcount% files
        goto :EOF

:delete
        set /a delcount=delcount + 1
        del "%~1"
        goto :EOF

--
MiniMax
ASKER CERTIFIED SOLUTION
Avatar of pbarrette
pbarrette

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yeah, I think I sort of answered it, but without feedback from the originator, it is hard to tell if the solution fits the problem 100%. Same goes for expert 'pbarrette' I think.
--
MiniMax
Hi Pasha,

I'll put my hand up on this one as well. The old "Delete my files after X many Y's" question...

pb
Avatar of Sterna

ASKER

I'd like to finalize this Question! Sorry I took so long ~ had to work on another project in the meanwhile. Still haven't implemented the code, but it seems perfect. Thank you MiniMaxXPC! Your help much appreciated.
Happy Programming.
Hi Sterna,

If you have recieved a working answer, you should close out this question yourself by accepting a comment as an answer and assigning it a grade value.

Remember that the grade you assign does not change the amount of points that you spent on the question. Instead, it changes the amount of points that the expert recieves for answering it. So please be generous to MiniMax.

Thanks,
pb
Hi

I have same kind of problem I need 2 copy all files & folders modified not all to other network  computer I already shared that directory where I want put files and folders  but I need to Schedule  I want this batch file run  daily 11:30 pm evening  .

Muhammad Amir
Hi Amir,

To schedule the batch file, you will need to use the "Scheduling Service". Look at "Control Panel"->"Scheduled Tasks" and add a new scheduled task. Since you will be sending it over the network, you will need to run the task as a user who has access to the network shares. Then you will need to modify the batchfile to either map the network drive, or specify the UNC to the shared folder.

Like this:
COPY "%FileName%" "\\Computer\Share\%FileName%"

pb
thank you very much pbarrette! 4 your quick reply

http://www.clickamir.com/forum.cfm
For the 1st code example, the delete after X minutes was great.  But there seemed to be a bug when just after midnight.   If  the time is 1AM, and I delete 30 minutes, I should get the same day but the HHMN would be 0030 for the time portion of the calcdate variable.  It always seemd to be off by 1 hour, therefore by 1 whole day+hour when you were close to midnight.   I changed the :LoopHrs and :LoopDate as follows.  Thanks a heap pbarrette for the code, I still couldn't have got this far without your help.


:LoopHrs
IF /I %HR% GTR 0 (GOTO LoopDate)
SET /A HR=%HR% + 24      :: Changed from 23
SET /A DD=%DD% - 1
  
GOTO LoopHrs
:LoopDate
SET /A HR=%HR%
:: Added next  4 lines
IF /I %HR% EQU 24 (
  SET /A HR=0       :: If at 24 hours set to 00 for midnight
  SET /A DD=%DD%+1  :: Add the day back that was previously subtracted.
)
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

Open in new window

has anyone tried

FORFILES /P C:\DIRECTORY\SUB-DIRECTORY\  /S /M *.* /D -1 /C "CMD /C DEL @FILE"


This works for me like a charm and deletes all files in the sub-directories and all sub-sub directories older than 1 day. Look up the dos help for this command
FORFILES /?

HTH..

Sandeep