| 1. |
Solve : A batch file to delete files based on time created/modified? |
|
Answer» 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.1Are the image files themselves named with date/time or do the names follow some other scheme? 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. Quote from: foxidrive on October 04, 2014, 11:08:28 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? Quote from: Salmon Trout on October 04, 2014, 11:21:34 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. Quote from: DaveLembke on October 04, 2014, 11:17:19 AM 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). 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! More OP's get abducted by aliens here than any other section of the Forums... Quote from: patio on October 04, 2014, 03:40:45 PM More OP's get abducted by aliens here than any other section of the Forums...No officer, there is nothing in my trunk. Quote Quote from: patio on Today at 07:40:45 AM Quote from: Squashman on October 04, 2014, 04:31:32 PM No officer, there is nothing in my trunk. Beware the ana1 probe... Quote from: foxidrive on October 04, 2014, 06:09:41 PM Beware the ana1 probe... Don't knock it if you haven't tried it... 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 ) ) 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 |
|