Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Batch file for deleting files outside of time window  (Read 4893 times)

0 Members and 1 Guest are viewing this topic.

Jerm

    Topic Starter


    Greenhorn

    • Experience: Experienced
    • OS: Windows 7
    Batch file for deleting files outside of time window
    « on: December 29, 2017, 01:15:46 PM »
    Hi folks,

    I recently revived a thread about a script that deletes files that were created outside of a certain time window (ie before 8am and after 6pm).

    I think the problem I'm having is that this script is using the "date created" criteria but the "date created" corresponds to the date that I created a copy of the file for processing, not when the picture was actually taken.

    Does anyone know how to modify the following code so that it uses either the "date modified" or the "date taken" criteria? I think "date taken" is metadata specific to .jpg's so that might not work in a batch script but there must be a way to use "date modified". All the "date modified" tags on my photos correspond to the time they were taken by the camera.

    Thanks a ton!

    Jeremy

    Code: [Select]
    @echo off
    :: (modified) original code by Salmon Trout - Oct 2014
    :: - deletes files with creation time earlier than 08:00 and after 16:59 (on files from any day)
    ::

    >  "%temp%\fhour.vbs" echo Set fso = CreateObject("Scripting.FileSystemObject")
    >> "%temp%\fhour.vbs" echo Set f = fso.GetFile(trim(wscript.arguments(0)))
    >> "%temp%\fhour.vbs" echo wscript.echo Hour (f.DateCreated)

    for %%A in ("c:\folder\*.*") do (
        for /f "delims=" %%B in ('cscript /nologo "%temp%\fhour.vbs" "%%~fA"') do (
            set "action="
            if %%B lss 8  set action=Delete
            if %%B geq 17 set action=Delete
            if /i not "%%~fA"=="%~f0" if defined action echo deleting "%%~fA" & del "%%~fA"
        )
    )
    pause
    « Last Edit: December 29, 2017, 01:45:46 PM by Jerm »

    Geek-9pm


      Mastermind
    • Geek After Dark
    • Thanked: 1026
      • Gekk9pm bnlog
    • Certifications: List
    • Computer: Specs
    • Experience: Expert
    • OS: Windows 10
    Re: Batch file for deleting files outside of time window
    « Reply #1 on: December 29, 2017, 01:24:05 PM »
    Perhasp tis is of some help...
    https://www.ocregister.com/2010/07/21/how-to-determine-when-a-photo-was-shot/
    Quote
    Q. I have some old pictures, mostly JPGs, that I have copied to my wife’s PC, and the dates for the files are different on her PC than on mine. Why is that and which ones are correct? My PC is running Windows XP, and hers has Windows 7.

    A. JPGs are the most common file type used by digital cameras. When we transfer the files from our cameras to our PC, either by using a card reader or by connecting the camera to our PC with a USB cable, we end up with a bunch of these JPGs. After a period of time or after they have been edited, it’s frequently difficult to determine exactly when the pictures were taken.

    For some reason the engineers at Microsoft have not been consistent in how dates for JPG files are reported. The dates for other files such as Microsoft Word documents are handled more consistently. Pictures such as JPGs and other image formats are a special case since the digital cameras that we use today actually store the date that a picture was taken as part of the picture or JPG file.

    There are several ways to transfer the pictures to a PC to remind us when they were shot, such as placing the date taken in the name of the folder used to house the files. Of course, that doesn’t help those of us who didn’t do this. But there are ways to determine when the pictures were taken. Before we get to some of these ways, let’s discuss how Microsoft Windows handles JPG file dates.
    The above article  goes on to explain the special case of JPEG files.

    Jerm

      Topic Starter


      Greenhorn

      • Experience: Experienced
      • OS: Windows 7
      Re: Batch file for deleting files outside of time window
      « Reply #2 on: December 29, 2017, 01:45:19 PM »
      Thanks for the reply Geek, sounds like "date taken" may be an option. Data can be viewed in windows explorer by "date taken".

      I'm less convinced now that my problem is in fact the "date created" issue because I copied a bunch of photos (at 1pm), ran the macro, and it deleted the photos despite appearing to pass the "keep" criteria of being created between 8am and 5pm.

      In order to get the macro working I had to change the file path and add an action to the FOR loop. In its original version the macro simply has
      Code: [Select]
      set "action="
      and will hang up until something is added after the equals sign. I'm not sure if that's part of my problem as well. So the macro I'm currently trying to run is as follows:

      Code: [Select]
      @echo off
      :: (modified) original code by Salmon Trout - Oct 2014
      :: - deletes files with creation time earlier than 08:00 and after 16:59 (on files from any day)
      ::

      >  "%temp%\fhour.vbs" echo Set fso = CreateObject("Scripting.FileSystemObject")
      >> "%temp%\fhour.vbs" echo Set f = fso.GetFile(trim(wscript.arguments(0)))
      >> "%temp%\fhour.vbs" echo wscript.echo Hour (f.DateCreated)

      for %%A in ("c:\349GOPRO\*.*") do (
          for /f "delims=" %%B in ('cscript /nologo "%temp%\fhour.vbs" "%%~fA"') do (
              set "action=Keep"
              if %%B lss 8  set action=Delete
              if %%B geq 18 set action=Delete
              if /i not "%%~fA"=="%~f0" if defined action echo deleting "%%~fA" & del "%%~fA"
          )
      )
      pause

      FYI photos are being taken with Gopro Hero 3 and 4's and I'm running a couple PCs, one with windows 7 and one with windows 10.

      Currently it is deleting everything regardless of date created/modified/taken. If anyone knows what my problem is I'd greatly appreciate some help. Thanks!


      BC_Programmer


        Mastermind
      • Typing is no substitute for thinking.
      • Thanked: 1140
        • Yes
        • Yes
        • BC-Programming.com
      • Certifications: List
      • Computer: Specs
      • Experience: Beginner
      • OS: Windows 11
      Re: Batch file for deleting files outside of time window
      « Reply #3 on: December 29, 2017, 03:07:34 PM »
      The set "action=" was there for a reason, as you've discovered :P

      With each file, it unsets the variable via set "action=", then it sets that variable if the hour of the DateCreated is less than 8 or greater than 18. Since you've changed it to always set the variable, it now deletes every file.

      I'd expect that what is seen as a hang may be that it is processing many files which do not meet the criteria. It will only output information when it deletes files.

      Also, regarding your original notes regarding modification dates,  You can change the DateCreated line reference to this to change the date being used:

      Code: [Select]
      >> "%temp%\fhour.vbs" echo wscript.echo Hour (f.DateLastModified)
      I was trying to dereference Null Pointers before it was cool.

      Jerm

        Topic Starter


        Greenhorn

        • Experience: Experienced
        • OS: Windows 7
        Re: Batch file for deleting files outside of time window
        « Reply #4 on: December 29, 2017, 04:25:06 PM »
        Holy crap you're right and it works!

        I have been saved many hours of manual deleting. Thanks a ton for the response.

        Jeremy

        Squashman



          Specialist
        • Thanked: 134
        • Experience: Experienced
        • OS: Other
        Re: Batch file for deleting files outside of time window
        « Reply #5 on: December 29, 2017, 08:44:04 PM »
        I suppose if you would have read the code and took the time to understand it you could have seen how obvious it was to fix.
        https://ss64.com/vb/filesystemobject.html