Link to home
Create AccountLog in
Microsoft DOS

Microsoft DOS

--

Questions

--

Followers

Top Experts

Avatar of Sterna
Sterna

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

Zero AI Policy

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


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 pbarrettepbarrette

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
Create Account

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

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


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 SternaSterna

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

Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


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

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


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

Microsoft DOS

Microsoft DOS

--

Questions

--

Followers

Top Experts

Microsoft Disk Operating System (MS-DOS) was an operating system for x86-based personal computers, and traces of it are still found in the Windows operating system. DOS is still used in some embedded systems and for certain legacy 16-bit networks.