• BAT – Conditional delete from folder

    Home » Forums » AskWoody support » Windows » Windows – other » BAT – Conditional delete from folder

    Author
    Topic
    #462983

    Sorry to ask this – must have been asked a zillion times before. Searched in here, but couldn’t find an answer…

    Need to delete all files more than one week old from a specific folder. Want to do it from a BAT file, which I then intend to set with the AT scheduler.

    So all I need is something like this:

    IF “filedate” < today-7
    THEN DEL…

    TIA

    Viewing 1 reply thread
    Author
    Replies
    • #1180174

      I think our own BATcher posted on this recently, so wait for him…

      Meanwhile, in WinXP, I just tried a search putting in parameters as you see here. It correctly found 31 files in my “Word” folder, including three in a subfolder. Obviously, I didn’t try deleting them.

    • #1180177

      Need to delete all files more than one week old from a specific folder. Want to do it from a BAT file, which I then intend to set with the AT scheduler.
      So all I need is something like this:
      IF “filedate” < today-7
      THEN DEL…

      I wonder whether you have one of the Resource Kit Tools packages, which included FORFILES.EXE? If you have, then it's a one-liner!
      Otherwise, it is surprisingly complicated.
      It would help me if you told me what date format you get when typing DIR – in the UK it's dd/mm/yyyy e.g. 06/10/2009 for today.
      Let me know,
      and I'll have a go…

      BATcher

      Plethora means a lot to me.

      • #1180218

        It would help me if you told me what date format you get when typing DIR – in the UK it’s dd/mm/yyyy e.g. 06/10/2009 for today.

        I’ve written it assuming UK date format – so let me know what your’s is!

        BATcher

        Plethora means a lot to me.

        • #1180262

          I’ve written it assuming UK date format – so let me know what your’s is!

          Hi BATcher,

          Format is dd/mm/yyyy

          Currently I don’t have the FORFILES.exe available, but I’ll have a look around to see if I can get it.

          …and the other solution would be?

          TIA

          • #1180273

            …and the other solution would be?

            The other solution would be the BATch file which I will upload this afternoon when I get home…!

            BATcher

            Plethora means a lot to me.

            • #1180298

              The other solution would be the BATch file which I will upload this afternoon when I get home…!

              Looking forward to that – thanks a million

            • #1180342

              Looking forward to that – thanks a million

              Herewith, uploaded as DelOlder.txt…

              Beware that filenames containing an ampersand (&) cannot be processed and give a console error message.

              BATcher

              Plethora means a lot to me.

            • #1180447

              Herewith, uploaded as DelOlder.txt…

              Beware that filenames containing an ampersand (&) cannot be processed and give a console error message.

              JC

              That was one huge amount of code for such a simple task

              I’ll test it during next week.

              THX – appreciate your prompt responses.

            • #1180456

              JC
              That was one huge amount of code for such a simple task

              As I said, if you’d got FORFILES it is (almost!) a one-liner. It takes a lot more effort in BATch to do the job which that program was designed to do in 21K of (probably) Assembly language.

              BATcher

              Plethora means a lot to me.

            • #1180460

              As I said, if you’d got FORFILES it is (almost!) a one-liner…

              Does this look like a “safe” place to get it? This fella’s a MS MVP, so I would think it’s OK:

              Download Free Windows 2000 Resource Kit Tools

            • #1180461

              That website is OK, as far as I know. Daniel Petri is a well-known MVP.

            • #1180463

              Does this look like a “safe” place to get it? This fella’s a MS MVP, so I would think it’s OK:

              Download Free Windows 2000 Resource Kit Tools

              Hi Al,

              Any particular reason why you chose Win2000 and not eg. Win XP or Vista??
              – are there no resource kits there or is FORFILES not available for those?

              (Haven’t had time to look further into this – so apologies if it sounds like a dumb question)

              TIA

            • #1180468

              Any particular reason why you chose Win2000 and not eg. Win XP or Vista??

              I don’t know anything about the tools, so when I searched for it, i.e. FORFILES.EXE, the TechNet site came up at the top of the list showing the NT Resource Kit. In looking for a place to download it, I came upon this Petri fella’s web site.

              For all I know there may not be a Resource Kit at the XP or Vista level. Someone else will have to comment on that.

            • #1180486

              I don’t know anything about the tools, so when I searched for it, i.e. FORFILES.EXE, the TechNet site came up at the top of the list showing the NT Resource Kit. In looking for a place to download it, I came upon this Petri fella’s web site.

              For all I know there may not be a Resource Kit at the XP or Vista level. Someone else will have to comment on that.

              There was an XP Resource Kit, a later one called Windows XP Support Tools, and Windows Server 2003 Resource Kit Tools…
              But the last time FORFILES.EXE appeared was in the Windows 2000 Resource Kit. (For NT it was in the original Resource Kit, Supplements 2, 3 and 4, and the Updates).
              (What is this Vista thing of which you speak?!)

              Daniel Petri’s website is probably the single most useful website for Systems Administrators…

              BATcher

              Plethora means a lot to me.

            • #1180487

              If you get hold of FORFILES the deeply opaque syntax is shown on Technet.
              Here’s an example of its use to delete all files older than 10 days:
              forfiles -p “” -s -m *.* -d -10 -c “Cmd /C del @FILE”

              And another variant:
              forfiles -p R:MyFiles -s -m *.* -d -365 -c “cmd /c echo 0x22@Path@File0x22”
              where you’d replace ‘echo’ by ‘del’ when you were happy it was working…

              LATER…
              And here’s a really old BATch file using FORFILES (the NT version)

              Code:
              @echo off
              :: +----------+
              :: | DELOLDER |	Delete files older than nn days from the target directory
              :: +----------+									John Gray  23OCT2000
              
              ::   rewritten version of Robert Amann's original (robert.anmann@prodigy.net)
              ::   requires FORFILES.EXE (NT 4 Resource Kit) and CHOICE.EXE
              ::   note that FORFILES cannot handle URLs and must use a drive letter
              
              if "%2" == "" echo. & echo Number of days not specified   & goto explain
              if "%1" == "" echo. & echo Target directory not specified & goto explain
              setlocal
              :: ensure the number of days is numeric, and reduce the number by 1
              ::   (this is because FORFILES uses less than or equal to the number of days)
              set /a nod=%2+0
              if %nod% NEQ %2 echo. & echo Number of days parameter is not numeric & goto explain
              set /a nod=nod-1
              
              goto delete
              :explain
              echo.
              echo DELOLDER [directoryname] [numberofdays]
              echo		   !			   !
              echo		   !			   delete files older than this number of days
              echo		   !
              echo		   single directory from which files are to be deleted
              echo			 (subdirectories NOT included)
              echo.
              echo Beware of including this BATch file in the directory to be deleted!
              echo There is no problem with FORFILES.EXE because it is in use.
              goto exit
              
              :delete
              echo %~n0 : you are about to delete all files older than %2 days
              echo %~n0 :   from the directory %1
              :: if CHOICE cannot be executed, default is to bypass FORFILES
              choice Reply Y to perform the deletion, or N to bypass  /c:ny
              ::											errorlevel:  12
              if errorlevel 2 forfiles -p%1 -d-%nod% -v -c"cmd /C del 0x22@FILE0x22"
              endlocal
              :exit

              BATcher

              Plethora means a lot to me.

    Viewing 1 reply thread
    Reply To: BAT – Conditional delete from folder

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: