1.

Solve : Batch file for deleting files outside of time window?

Answer»

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"
    )
)
pausePerhasp 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.
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!

The set "action=" was there for a reason, as you've discovered

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)Holy crap you're right and it works!

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

JeremyI 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


Discussion

No Comment Found