Batch Delete a file older than X hours

Hello,

I am currently doing an internship at a large company, and I have been asked to create a Batch or VB script to delete temporary files, but only files older than 3 hours.
I am really not an expert in scripting; I am just starting out.
Here is the code I found on the internet that I tried to modify without success, and I received an error message.
@echo off

set fich=C:\Users\paje01\Desktop\TEST\test1
set jour=3

pause
forfiles -p %fich% -s -m *.* -d -0:03 "cmd /c del @FILE"

pause
@exit


I then get the following error message:

Press any key to continue...
Error: Invalid argument or option - "cmd /c del @FILE".
Enter "FORFILES /?" to display the syntax.
Press any key to continue...


So, if anyone has a solution for me :)
Thank you and have a good day.

4 answers

  • Votes
  • Date
  • Descending date
  1. Moderator
    Hello,

    In your command, there is a missing space between
    -3 -c
     

    However, this command processes days, not hours. You should look for another script.

    Best regards
    --
            a stranger is a friend you haven't met yet.
    1. Hello and thank you for your response,

      After some research, I managed to create a functional script for the days, but I'm still struggling with the hours. Here is my test:

       
      @ECHO OFF
      REM START OF THE SCRIPT

      REM DISPLAYING HOURS AND MINUTES

      for /f "tokens=1,2,3 delims=:" %%a in ('time /t') do set hour=%%a
      for /f "tokens=1,2,3 delims=:" %%a in ('time /t') do set minute=%%b
      echo Hour : %hour%
      echo Minute : %minute%

      REM ///////CREATING A FOLDER CONTAINING HOURS AND MINUTES

      REM ///////echo Test > file_%hour%%minute%.txt

      pause

      REM TIME AND FOLDER VARIABLES
      REM DIRECTORY CONTAINING OUR FILES
      SET REPDIR=C:\Users\paje01\Desktop\TEST\test1

      REM MAXIMUM NUMBER OF DAYS TO KEEP
      SET HMAX=%time:~0,2%

      pause

      REM DELETE FILES
      forfiles /P %REPDIR% /m *.* /D -%HMAX% /C "cmd /c del /q @path"

      pause
      REM ///// TEST
      REM /////ForFiles /P "%REPDIR%" /D -%DAYMAX% /C "CMD /C if @ISDIR==TRUE echo RD /Q /S @FILE &RD /Q /S @path"

      REM DISPLAY A MESSAGE AFTER DELETION

      echo Msgbox "The script has been executed successfully and your files have been deleted."
      pause
      1. And here is the one that works,

         @ECHO OFF REM START OF THE SCRIPT 

        REM TIME AND FOLDER VARIABLES
        REM DIRECTORY CONTAINING OUR FILES
        SET REPDOSS=C:\Users\paje01\Desktop\TEST\test1
        REM MAXIMUM RETENTION DAYS
        SET JOURMAX=1

        REM DELETE FILES
        forfiles /P %REPDOSS% /m *.* /D -%JOURMAX% /C "cmd /c del /q @path"
        pause

        REM DELETE FOLDERS
        REM ForFiles /P "%REPDOSS%" /D -%JOURMAXS% /C "CMD /C if @ISDIR==TRUE echo RD /Q /S @FILE &RD /Q /S @path"

        PAUSE
        1. Contributor
          Hello,

          I took your code to eliminate files/folders older than 1 day, then I created the code to delete those older than 3 hours:

          @echo off
          setlocal enableextensions enabledelayedexpansion
          chcp 1252 >nul

          REM DIRECTORY CONTAINING OUR FILES
          SET REPDOSS=C:\Users\paje01\Desktop\TEST\test1

          REM MAXIMUM NUMBER OF DAYS OF RETENTION
          SET JOURMAX=1

          cd /d %REPDOSS% || exit /b 1

          REM DELETE FILES
          forfiles /D -%JOURMAX% /C "cmd /c if @ISDIR==FALSE (del /f /q @file)"

          REM DELETE FOLDERS
          forfiles /D -%JOURMAX% /C "cmd /c if @ISDIR==TRUE (rd /Q /S @file)"

          PAUSE

          :: GET CURRENT TIME AND CONVERT IT TO MINUTES
          for /f "tokens=1,2,3 delims=:" %%a in ('time /t') do (
          set H_actuelle=%%a
          set M_actuelle=%%b

          if !H_actuelle! LSS 10 (set H_actuelle=!H_actuelle:~-1!)
          if !M_actuelle! LSS 10 (set M_actuelle=!M_actuelle:~-1!)

          set /a total_actuelle=!H_actuelle!*60+!M_actuelle!
          )

          pause

          :: LIST FILES AND FOLDERS
          for /f "delims=" %%K in ('dir /b') do (

          :: GET TIME FOR EACH FILE/FOLDER
          for /f "skip=5 tokens=1,2,3,* delims=: " %%A in ('2^>nul dir "%%~K" ^| findstr /v "octets"') do (
          set heure=%%B
          set minute=%%C

          :: CONVERT TIME TO MINUTES
          if !heure! LSS 10 (set heure=!heure:~-1!)
          if !minute! LSS 10 (set minute=!minute:~-1!)

          set /a total_fichier=!heure!*60+!minute!

          :: DETERMINE THE DIFFERENCE AND DELETE FILES/FOLDERS THAT ARE TOO OLD
          set /a ecart=!total_actuelle!-!total_fichier!

          if !ecart! GTR 180 (2>nul rd /s /q "%%~K" || del /f "%%~K")
          if !ecart! LSS -1440 (2>nul rd /s /q "%%~K" || del /f "%%~K")
          if !ecart! LSS 0 (if !ecart! GTR -1260 (2>nul rd /s /q "%%~K" || del /f "%%~K"))
          )
          )
          pause

          exit /b 0


          “Artificial intelligence is defined as the opposite of natural stupidity.”
          1. Hello, did it work for you? Because it's not working for me!
          2. Contributor
            @barbabe0057Hello,

            If you want help, you need to describe your problem precisely; it's not enough to say it doesn't work.

            Which part of the script isn't working? The first part that deletes folders/files older than 1 day, or the second part that deletes files/folders older than 3 hours?

            Is your problem regarding the files, the folders, or both?
          3. @barnabe0057

            Hello,

            At my place, it deletes all the files and shows me "missing operator" in the command prompt.

            Do you have any idea?

            Otherwise, the forfiles command works great for 1 day, too bad we can't do less than a day.