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

Author Topic: A batch file to delete files based on time created/modified  (Read 26994 times)

0 Members and 2 Guests are viewing this topic.

AlanSmith1000

    Topic Starter


    Newbie

    • Experience: Familiar
    • OS: Windows 8
    A batch file to delete files based on time created/modified
    « on: October 04, 2014, 07:36:38 AM »
    I am trying out some time lapse photography to capture building work over the next 18 months. Unfortunately the software I am using  cannot be scheduled so I am getting photos 24/7. I thought the simplest way to deal with this would be a daily batch file to delete the photos taken between 0000 & 0800 and 1700 & 2359. However, I can only find how to create a batch file based on the date, but not on the time. Can anyone help? Alternative solutions are also welcome. I am using Windows 8.1

    Salmon Trout

    • Guest
    Re: A batch file to delete files based on time created/modified
    « Reply #1 on: October 04, 2014, 08:29:29 AM »
    Are the image files themselves named with date/time or do the names follow some other scheme?

    foxidrive



      Specialist
    • Thanked: 268
    • Experience: Experienced
    • OS: Windows 8
    Re: A batch file to delete files based on time created/modified
    « Reply #2 on: October 04, 2014, 08:53:27 AM »
    Salmon Trout may give you a batch solution but here is a powershell solution wrapped inside a batch file.

    EDIT:

    if your PC is setup to enable powershell scripts then run this in the folder with the images and it
    should echo the del command to the screen for each image that is outside the time range shown
     - it will delete ALL files from previous days in the current directory because they are outside the time range.

    Remove the echo from echo del "%%a" to make it delete the images instead of list it to the screen after you have verified that the correct files are listed.


    Code: [Select]
    @echo off
    set "file=%temp%\psdaterange.ps1"

    (
    echo( $folderPath = '%~dp0'
    echo(
    echo( $date1 = Get-Date '08:00'
    echo( $date2 = Get-Date '17:00'
    echo(
    echo( foreach( $item in (Get-ChildItem -file $folderPath^) ^)
    echo( {
    echo( if( $item.LastWriteTime -lt $date1 -or $item.LastWriteTime -gt $date2 ^)
    echo(     {
    echo(         Write-Host $folderPath$item
    echo(     }
    echo( }
    ) >"%file%"

    for /f "delims=" %%a in ('powershell "%file%"') do echo del "%%a"
    del "%file%"

    pause
    « Last Edit: October 04, 2014, 09:19:08 AM by foxidrive »

    Salmon Trout

    • Guest
    Re: A batch file to delete files based on time created/modified
    « Reply #3 on: October 04, 2014, 10:36:52 AM »
    I was going to finish a VBscript/batch hybrid or pure VBscript solution I was working on, but the need has suddenly evaporated, unless the OP requests it. However it has occurred to me that it is quite easy, in Windows Explorer, to delete files created in a time range by choosing Details view and then sorting in date created order, using a mouse left click to select the first file to be deleted, then extending the selection by using the SHIFT and the down arrow keys until the last file to be deleted is reached, finally pressing the DEL key. Then the files will move to the Recycle Bin and can be recovered if needed, or SHIFT + DEL will remove them forever. Of course if there are lots of folders to be treated or if there are files from many days a scripted solution might be preferred.



    « Last Edit: October 04, 2014, 10:51:51 AM by Salmon Trout »

    foxidrive



      Specialist
    • Thanked: 268
    • Experience: Experienced
    • OS: Windows 8
    Re: A batch file to delete files based on time created/modified
    « Reply #4 on: October 04, 2014, 11:08:28 AM »
    Your VBS solution should work where the powershell one can be a problem if powershell is not configured to run scripts, I think.

    This failed in Windows 7 and worked in Windows 8.1
    It's the same code as above but the temporary file is eliminated:

    Code: [Select]
    @echo off

    pushd "%~dp0"
    for /f "delims=" %%a in ('powershell "foreach( $item in (Get-ChildItem -file)) {if( $item.LastWriteTime -lt '08:00' -or $item.LastWriteTime -gt '17:00' ) {Write-Host $item}}"') do echo del "%~dp0%%a"
    popd

    pause
    « Last Edit: October 04, 2014, 11:33:45 AM by foxidrive »

    DaveLembke



      Sage
    • Thanked: 662
    • Certifications: List
    • Computer: Specs
    • Experience: Expert
    • OS: Windows 10
    Re: A batch file to delete files based on time created/modified
    « Reply #5 on: October 04, 2014, 11:17:19 AM »
    Quote
    However it has occurred to me that it is quite easy, in Windows Explorer, to delete files created in a time range by choosing Details view and then sorting in date created order, using a mouse left click to select the first file to be deleted, then extending the selection by using the SHIFT and the down arrow keys until the last file to be deleted is reached, finally pressing the DEL key. Then the files will move to the Recycle Bin and can be recovered if needed, or SHIFT + DEL will remove them forever. Of course if there are lots of folders to be treated or if there are files from many days a scripted solution might be preferred.

    Reading down the list of replies etc, I came to same idea that Salmon did with why not just sort by date and select the range to delete. However if there is a need to automate the clean up of files, then i can see using a scripted method.

    In the past I had a script that would delete data from computer class lab computers in which we initially use to reimage them clean after each course was completed to make sure that they were all clean and fresh for the next class starting that next monday after completion on that friday, and instead of reimaging all the 20 systems, it was decided to just restrict the users on the lab systems to user privileges so that they couldnt install anything and very unlikely to get hit with malware, and give them a specific folder to save their work to with no other path to save to under privileges. Then instead of having to wipe entire systems clean and reimage which takes time, floods the network for about an hour or so on a Saturday and overworks the hard drives of each of the systems, its just a matter of running a quick batch routine from my system that would wipe out the data from that one folder on all 20 systems and replace the pdf's in that folder with the course content for that monday depending on what they were going to be learning. Verses prior reimaging each system to make clean and delete old student data and then repopulating the system with the course content data that is local to each system for students to be able to edit and highlight sections in their PDF's etc. This could have been added to a scheduled task etc instead of a manual start of the batch that ran out to all 20 systems with the folder shared for me to wipe clean and repopulate with clean copies of the course content vs the prior content that was altered with highlighted sections and personal notations.

    Salmon Trout

    • Guest
    Re: A batch file to delete files based on time created/modified
    « Reply #6 on: October 04, 2014, 11:21:34 AM »
    foreach( $item in (Get-ChildItem -file)) {if( $item.LastWriteTime -ge '08:00' -and $item.LastWriteTime -le '17:00' )

    Won't that select those files whose last write time is 8 AM or after - and - earlier than or at 5 PM. i.e. the ones taken during the working day (the ones he wants to keep?)

    Incidentally, Alan (if I can call you that?) I see you specify deleting images taken between midnight and 8 AM,  and between 5 PM and 11:59 PM, which is slightly ambiguous. Do you want to keep or delete files taken at 8:00 AM or 6:00 PM?

    foxidrive



      Specialist
    • Thanked: 268
    • Experience: Experienced
    • OS: Windows 8
    Re: A batch file to delete files based on time created/modified
    « Reply #7 on: October 04, 2014, 11:35:37 AM »
    Won't that select those files whose last write time is 8 AM or after - and - earlier than or at 5 PM. i.e. the ones taken during the working day (the ones he wants to keep?)

    yeah, I was editing and changing stuff and lost track of what was what.

    Thanks for the warning - I changed the post above.

    why not just sort by date and select the range to delete.

    That could be done but it's not going to be as straight forward and needs to consider both 12 and 24 hour time formats (also assuming that the files in the folder are from the current date).


    Salmon Trout

    • Guest
    Re: A batch file to delete files based on time created/modified
    « Reply #8 on: October 04, 2014, 03:32:30 PM »
    If we could just get a sample of %%~t from the OP, we'd know the local date/time format & it would just be something simple like if %hour% LSS 8 and if %hour% GTR 17, so if you are still there, Alan, let's hear from you!

    patio

    • Moderator


    • Genius
    • Maud' Dib
    • Thanked: 1769
      • Yes
    • Experience: Beginner
    • OS: Windows 7
    Re: A batch file to delete files based on time created/modified
    « Reply #9 on: October 04, 2014, 03:40:45 PM »
    More OP's get abducted by aliens here than any other section of the Forums...
    " Anyone who goes to a psychiatrist should have his head examined. "

    Squashman



      Specialist
    • Thanked: 134
    • Experience: Experienced
    • OS: Other
    Re: A batch file to delete files based on time created/modified
    « Reply #10 on: October 04, 2014, 04:31:32 PM »
    More OP's get abducted by aliens here than any other section of the Forums...
    No officer, there is nothing in my trunk. :-X

    foxidrive



      Specialist
    • Thanked: 268
    • Experience: Experienced
    • OS: Windows 8
    Re: A batch file to delete files based on time created/modified
    « Reply #11 on: October 04, 2014, 06:09:41 PM »
    Quote
    Quote from: patio on Today at 07:40:45 AM
        More OP's get abducted by aliens here than any other section of the Forums...



    No officer, there is nothing in my trunk. :-X

    Beware the ana1 probe... ;)

    Salmon Trout

    • Guest
    Re: A batch file to delete files based on time created/modified
    « Reply #12 on: October 05, 2014, 12:41:50 AM »
    Beware the ana1 probe... ;)

    Don't knock it if you haven't tried it...

    Salmon Trout

    • Guest
    Re: A batch file to delete files based on time created/modified
    « Reply #13 on: October 05, 2014, 04:25:49 AM »
    Code: [Select]
    @echo off
    setlocal enabledelayedexpansion
    echo Set fso = CreateObject("Scripting.FileSystemObject") > fhour.vbs
    echo Set f = fso.GetFile(trim(wscript.arguments(0))) >> fhour.vbs
    echo wscript.echo Hour (f.DateCreated) >> fhour.vbs
    for %%A in (s:\tdate\*.jpg) do (
        for /f "delims=" %%B in ('cscript //nologo fhour.vbs %%~dpnxA') do (
            set "action=Keep  "
            if %%B lss 8  set action=Delete
            if %%B geq 17 set action=Delete
            echo !Action! %%~tA %%~dpnxA
            if "!Action!"=="Delete" del "%%~dpnxA" > nul
            )
        )

    foxidrive



      Specialist
    • Thanked: 268
    • Experience: Experienced
    • OS: Windows 8
    Re: A batch file to delete files based on time created/modified
    « Reply #14 on: October 05, 2014, 05:07:29 AM »
    Yours is a better solution Salmon Trout, as it handles any days files.  You've done the hard work, I just polished it at the edges.

    I altered your code slightly so that the batch file can reside in the same folder,
    and removed the need for delayed expansion and the .vbs file is created in %temp%

    echoing the date/time stamp could be confusing because the last modified time can be different, so I removed that also.

    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: October 05, 2014, 05:46:17 AM by foxidrive »