# Batch delete files created between two TIMES in every day



## martindeltrice (Feb 27, 2012)

Hi,

I have found many batch files to delete anc clean up files by date range and have tried to alter these to suit my needs but am having a problem getting it all to work.

HELP 

Our system creates a log file of our station output for every hour of the day and night, so as you can imagine it gets pretty tiresome deleting the ones we don't need when it comes to logger backup time...

I need a batch file that will look at a folder and all of the sub folders within it and delete files from a specified time range but for all dates.

i.e. all files within the folders created between 01:00hrs and :06:00hrs on all dates.

Can anyone help please? Suggestions, ideas... Anything?

Thanks in advance.

Martin


----------



## martindeltrice (Feb 27, 2012)

* Addendum *

It needs to look at the MODIFIED time not the created time - My apologies.


Martin


----------



## Squashman (Apr 4, 2003)

Maybe this will help you along.
Putting the Hour the minute and AM or PM each into a variable. From there you can do IF statements to see if your time is between your range of hours.

```
@echo off
setlocal enabledelayedexpansion
for /F "tokens=* delims=" %%G in ('dir *.txt /a-d /b /s') do (
	SET TD=%%~tG
	SET AMPM=!TD:~-2!
	SET HOUR=!TD:~-8,2!
	SET MIN=!TD:~-5,2!
	echo FileName: %%G Hour:!HOUR! Minutes:!MIN! !AMPM!
)
```


----------



## martindeltrice (Feb 27, 2012)

Thank you so much for that. 

I am not great at this batch file stuff but am taking a rather sharp learning curve.

You may hear a scream for more help if it all goes even further wrong than it has in the past... 

Kind regards
Martin


----------



## Squashman (Apr 4, 2003)

Just a couple of IF statements is all you need.

```
@echo off
setlocal enabledelayedexpansion

for /F "tokens=* delims=" %%G in ('dir *.txt /a-d /b /s') do (
	SET TD=%%~tG
	SET AMPM=!TD:~-2!
	SET HOUR=!TD:~-8,2!
	SET MIN=!TD:~-5,2!
	IF "!AMPM!"=="AM" (
		IF 1!HOUR!!MIN! LEQ 10600 (
			IF 1!HOUR!!MIN! GEQ 10100 DEL "%%G"
		)
	)
)
```


----------



## martindeltrice (Feb 27, 2012)

Just been trying it out and sorry if I sound really stupid...

have set the file extension from *.txt to *.ogg as the logger records audio output from the station as .ogg files - that seems fine, however when I run the batch file it looks at the files (have checked with echo output to screen, but it's not deleting anything in the time range we have set.

Is it possible that the script is looking at the time created and not the time modified? The time modified is the one that I need to look at for deletion.

I am trying to find the switch / variable now to make it look at the modified time but have only found the dir /t:w so far and dont appear to be able to make it work.

I just wish I knew more and understood it as it's so very frustrating to be so close but so far away...

Martin


----------



## Squashman (Apr 4, 2003)

The script uses the modified date by default.

Show me the output of a few of the files with this code.


```
@echo off
for /F "tokens=* delims=" %%G in ('dir *.ogg /a-d /b /s') do echo FileName: %%G Date and Time: %%~tG
```


----------



## martindeltrice (Feb 27, 2012)

Hi,

I am so sorry to put you through this but do appreciate the help an awfull lot.

Info attached as requested.


----------



## martindeltrice (Feb 27, 2012)

My apologies again, I may have figured it out.

It looks like the AM/PM indication is causing the problem so I have removed it from the line that checks if a file needs to be deleted or not.

I have also changed the set hour and set minutes lines to reflect this and it appears to work...

What I now have is:

@echo off
setlocal enabledelayedexpansion
for /F "tokens=* delims=" %%G in ('dir *.ogg /a-d /b /s') do (
SET TD=%%~tG
SET HOUR=!TD:~-5,2!
SET MIN=!TD:~-2,2!
IF 1!HOUR!!MIN! LEQ 10600 (
IF 1!HOUR!!MIN! GEQ 10100 DEL "%%G"
)
)

It seems to work - does it look ok to you? You know what you are doing I am only learning! 

thanks in advance


----------



## Squashman (Apr 4, 2003)

Exactly. Your Regional time settings are set for 24 hour format. So it was not outputting the AM or PM. I don't see to many people who do it that way. That is why I asked to see the output from that previous command.

Just a little tip for you as well. You can copy and paste text from the cmd prompt. A screen shot was over kill.
Right click the top of the Windows and select Edit then Select all. Then press enter. This copies to the clipboard.

I have my cmd window setup so that I can just drag my mouse and select and then type enter. Can't remember the setting to do that.


----------



## Squashman (Apr 4, 2003)

Just another tip.
It helps a lot if you put CODE tags around your code. This helps distinguish code you are posting from comments. If you quote one of my posts above you can see how that is done. This also helps keep the formatting and indenting that you see with my code above. Readability is key to understanding any programming language.
http://en.wikipedia.org/wiki/BBCode


----------



## martindeltrice (Feb 27, 2012)

Thanks so very much for all of that...

I hope that one day I may be able top return the favour to someone else but I still have a long way to go yet.

If there is anything I can ever do in return then please don't hesitate to ask...

The extra info / hints is great... Thanks once again.

Kindest regards
Martin


----------

