# Solved: RE: Batch delete files created between two TIMES in every day



## thibaultmol (Jun 12, 2012)

hi forum,

i was looking for a way to free up some space on my hard drive of my server, it's makes a backup of the program every hour and i want it so that it only leaves one file per day automaticly if the file is older than 1 month(30 days). So i found this thread: http://forums.techguy.org/dos-other/1042906-batch-delete-files-created-between.html but i don't know THAT much about batch files, can someone please help me with this

btw: the filetype that is used is .mcb
the folder is "d:\minecraft server backups\official laatste versie\"

thx, sry for being such a noob at batch files...


----------



## Squashman (Apr 4, 2003)

Your problem is way different than his.
You didn't specify what operating system you are using either.

And this really makes no sense to me.


> it's makes a backup of the program every hour and i want it so that it only leaves one file per day automaticly if the file is older than 1 month(30 days).


----------



## thibaultmol (Jun 12, 2012)

yeah sorry, my operating system is windows 7 64bit

so what i have are files like this(these files are in the folder:"d:\minecraft server backups\official laatste versie\") :



> MCMA-backup-04012012-140000_Scheduled Backup.mcb(this file is from 01/04/2012 at 14:00)
> MCMA-backup-04012012-150000_Scheduled Backup.mcb
> MCMA-backup-04012012-180000_Scheduled Backup.mcb
> ....
> MCMA-backup-04042012-110000_Scheduled Backup.mcb(so this file is from 04/04/2012 at 11:00 for example)


and i want it to delete some files so that only 1 file is left every day, but it can only delete files that are older than 30 days


----------



## Squashman (Apr 4, 2003)

You are going to have to give a better example than that because your last comment still doesn't make sense to me. You either want to keep one backup for each day or delete files older than 30 days. Which is it.

Windows 7 has the FORFILES command and can easily chose files that are older than 30 days.
http://technet.microsoft.com/en-us/library/cc753551(v=ws.10).aspx


----------



## thibaultmol (Jun 12, 2012)

so i want to delete files so that there is only one backup every day, but the files that it deletes can't be before 30 days(sorry for my bad english)
so basicly what i mean is that for example i have these files:
name: date:
file1 01/04/2012
file2 01/04/2012
file3 01/04/2012
file4 02/04/2012
file5 02/04/2012
file6 10/06/2012
file7 10/06/2012
file8 11/06/2012

these files will be deleted: file1, file2, file4 but not file3 because there has to be one left and also not file6,file7 and file8 because they were made in the last 30 days even thought there are multiple a day


----------



## Squashman (Apr 4, 2003)

I understand what you want now but I don't believe my skills are good enough to accomplish what you want.


----------



## thibaultmol (Jun 12, 2012)

oh okay, but i doesn't have to be a batch file, if you could recommend a program that does that that would work too


----------



## Squashman (Apr 4, 2003)

thibaultmol said:


> oh okay, but i doesn't have to be a batch file, if you could recommend a program that does that that would work too


Don't you think I would of if I knew already and spent time already responding to this thread.


----------



## thibaultmol (Jun 12, 2012)

yeah sry


----------



## Squashman (Apr 4, 2003)

Give this a try. Copy some old and new files to a test folder and put this batch file in their as well. 
This batch file is based on all your file names being in this format.

```
MCMA-backup-04012012-140000_Scheduled Backup.mcb
MCMA-backup-04012012-150000_Scheduled Backup.mcb
MCMA-backup-04012012-180000_Scheduled Backup.mcb
```


```
@echo off
setlocal enabledelayedexpansion

IF EXIST temp.log DEL /Q temp.log
FORFILES /D -30 /M *.mcb /C "cmd /c echo @file>>temp.log"

SET Fdate=00000000

FOR /F "TOKENS=1-3* DELIMS=-" %%G IN ('sort /R temp.log') DO (
	IF NOT "%%I"=="!Fdate!" (
		SET Fdate=%%I
	) ELSE (
		DEL "%%G-%%H-%%I-%%J"
	)
)
DEL /Q temp.log
```


----------



## thibaultmol (Jun 12, 2012)

i think the .mcb is not used when deleting the files
because this is the error that i get


> Could Not Find C:\Users\thibault\Desktop\test folder\MCMA-backup-04052012-090000
> _Scheduled


i doesn't have the .mcb and at the end there is also " backup"
so that one should be: MCMA-backup-04052012-090000_Scheduled Backup.mcb
thx for doing it anyways


----------



## Squashman (Apr 4, 2003)

Only way that would happen is if you didn't copy and paste my example directly from the forum. They only way it would miss putting the "Backup.mcb" with a space before it onto the end of the file name is if you changed my FOR LOOP code to use a Space as a delimiter.


----------



## thibaultmol (Jun 12, 2012)

sry, this may sound stupid but i don't really know what you mean,because english is not my mother language and i don't know that much about batch files


----------



## Squashman (Apr 4, 2003)

thibaultmol said:


> sry, this may sound stupid but i don't really know what you mean,because english is not my mother language and i don't know that much about batch files


No Technical Forums in Belgium?

Upload your Batch file to the forums as a text file attachment. I will take a look at it when I get home. I don't have Windows 7 at work so I can't test it here. Still on XP for a few more weeks.


----------



## thibaultmol (Jun 12, 2012)

there are technical forums in belgium but they are better in popular country's and why do i have to upload the batch file? isn't it the same as you posted a few messages ago?


----------



## thibaultmol (Jun 12, 2012)

btw, if you want to test it out with a couple of files:
http://www.molcraft.be/test_files.zip
it's kind of a large zip but yeah...


----------



## Squashman (Apr 4, 2003)

You can attach text files directly to the forums.
Click the Go Advanced button next to the Quick Reply Button and then click Manage Attachments to upload.

I tested this batch file last night and it worked just fine based on the File name examples that you gave me. So I need to see if you accidentally edit some of the code by mistake. That is why you need to upload it.


----------



## thibaultmol (Jun 12, 2012)

the text file just has the same as what you posted: http://forums.techguy.org/8381821-post10.html
i just copied and pasted it into a .bat file (so that's the only thing that is in that .bat file)


----------



## Squashman (Apr 4, 2003)

thibaultmol said:


> the text file just has the same as what you posted: http://forums.techguy.org/8381821-post10.html
> i just copied and pasted it into a .bat file (so that's the only thing that is in that .bat file)


Well us two Belgians could be stubborn and argue about this all day but when I help someone and ask for specific information or for something to be done I expect it to be done if the person wants help. I don't get paid to do this, I do it so that others may learn.


----------



## Squashman (Apr 4, 2003)

remove the surrounding Quotes from this line

```
DEL "%%G-%%H-%%I-%%J"
```


----------



## thibaultmol (Jun 12, 2012)

Sry, but it was the same file as you suggested, and btw: i would have to change it to a txt because batch files can't be uploaded
I'l try it without the quotes when i get back from school
Thx for helping the whole way


----------



## Squashman (Apr 4, 2003)

thibaultmol said:


> Sry, but it was the same file as you suggested, and btw: i would have to change it to a txt because batch files can't be uploaded


Thanks for the stating the obvious. I told you in my previous posts it had to be uploaded as a text file. And a batch file is just a text file. The only thing that makes it executable is the file association within Windows. If I wanted to I could setup the file association to execute files with a TXT extension. All scripts from non compiled interpreted languages are just plain old text files.

I have been using the Internet for 20 years and trust me when I say I don't trust what most people say until I see it. I can't tell you how many times I have written scripts and people didn't copy and paste it correctly from the website or forum or they didn't give me the correct data information which completely made everything blow up.

If you want to be a stubborn Belgian like my Grandfather was go right ahead. But don't expect people to help you if you are not going to provide the information they request when you ask a question.


----------



## thibaultmol (Jun 12, 2012)

Sorry, my behavior may have been a little bit too angry+- but it's because i'm at the beginning of my exams and i'm kind of stressed
so here is the file:
btw,deleting the "" didn't fix it, it know shows nothing not even an error


----------



## Squashman (Apr 4, 2003)

I don't know what to tell you then. It should work. First time it didn't work for you was because your filenames had a space and I was double quoting. If the filenames didn't have spaces the script worked fine. Forfiles is quoting its output so we didn't need he extra quotes anyways. 

If you turn the echo on you may be able to see what it is doing and see something I can't see. Not sure what language you are using but that does have an affect on batch files as well.


----------



## Squashman (Apr 4, 2003)

Well here it is. Ran your test files you gave me and the batch file works perfectly.


```
C:\Users\Squash\batch files\Keep1Backup>dir *.mcb
 Volume in drive C has no label.
 Volume Serial Number is 36E8-BEDE

 Directory of C:\Users\Squash\batch files\Keep1Backup

04/01/2012  02:00 PM        14,668,884 MCMA-backup-04012012-140000_Scheduled Backup.mcb
04/01/2012  03:00 PM        14,686,436 MCMA-backup-04012012-150000_Scheduled Backup.mcb
04/01/2012  06:00 PM        14,727,384 MCMA-backup-04012012-180000_Scheduled Backup.mcb
04/04/2012  11:00 AM        28,312,389 MCMA-backup-04042012-110000_Scheduled Backup.mcb
04/04/2012  03:59 PM        28,799,762 MCMA-backup-04042012-155932_voor 1.2.5.mcb
04/07/2012  01:00 PM        28,799,767 MCMA-backup-04072012-130000_Scheduled Backup.mcb
04/08/2012  07:00 AM        28,799,760 MCMA-backup-04082012-070000_Scheduled Backup.mcb
04/08/2012  08:00 AM        28,799,765 MCMA-backup-04082012-080000_Scheduled Backup.mcb
06/12/2012  10:00 AM        41,378,621 MCMA-backup-06122012-100000_Scheduled Backup.mcb
06/12/2012  11:00 AM        41,378,621 MCMA-backup-06122012-110000_Scheduled Backup.mcb
              10 File(s)    270,351,389 bytes
               0 Dir(s)  184,931,811,328 bytes free

C:\Users\Squash\batch files\Keep1Backup>Keep1backup.bat

C:\Users\Squash\batch files\Keep1Backup>dir *.mcb
 Volume in drive C has no label.
 Volume Serial Number is 36E8-BEDE

 Directory of C:\Users\Squash\batch files\Keep1Backup

04/01/2012  06:00 PM        14,727,384 MCMA-backup-04012012-180000_Scheduled Backup.mcb
04/04/2012  03:59 PM        28,799,762 MCMA-backup-04042012-155932_voor 1.2.5.mcb
04/07/2012  01:00 PM        28,799,767 MCMA-backup-04072012-130000_Scheduled Backup.mcb
04/08/2012  08:00 AM        28,799,765 MCMA-backup-04082012-080000_Scheduled Backup.mcb
06/12/2012  10:00 AM        41,378,621 MCMA-backup-06122012-100000_Scheduled Backup.mcb
06/12/2012  11:00 AM        41,378,621 MCMA-backup-06122012-110000_Scheduled Backup.mcb
               6 File(s)    183,883,920 bytes
               0 Dir(s)  185,020,583,936 bytes free
```
Original code with the Quotes removed.

```
@echo off
setlocal enabledelayedexpansion

IF EXIST temp.log DEL /Q temp.log
FORFILES /D -30 /M *.mcb /C "cmd /c echo @file>>temp.log"

SET Fdate=00000000

FOR /F "TOKENS=1-3* DELIMS=-" %%G IN ('sort /R temp.log') DO (
	IF NOT "%%I"=="!Fdate!" (
		SET Fdate=%%I
	) ELSE (
		DEL %%G-%%H-%%I-%%J
	)
)
DEL /Q temp.log
```


----------



## thibaultmol (Jun 12, 2012)

Is it okay if i will give you my teamviewer id en temp password via DM, so than you can see what i did wrong, i can only do this after the 26th of june(because i'm done with the exams at that point, we will find a date and time after the 26th


----------



## Squashman (Apr 4, 2003)

No. Sorry. I am not going to do remote support like that. I used to do network support for a couple of Law offices here in town and if there is one thing I have learned from my good lawyer friends is you don't want to put yourself in a situation that will compromise the integrity of your business. I always had signed contracts with anyone I did onsite or remote support for. This is a support forum, not a remote desktop support forum.

The batch file will work just fine. Copy it into the directory that has all your backup files and execute it. You can see from my example above that it works.

So here is what you are going to do. You are going to execute the same commands I did above in that order and copy and paste the output from the cmd prompt.

So open up a cmd prompt and change to the directory that has your backup files.
Now type the following commands.
dir *.mcb
Keep1backup.bat (or whatever you named your batch file)
dir *.mcb

Now copy that output from the screen to the forum and put CODE tags around it like I did above.


----------



## thibaultmol (Jun 12, 2012)

THANK YOU SO MUCH, i don't know what i did wrong, but i tried it again now with your advice and it worked, thank you so much for helping me make batch file, and sorry for asking stupid things or giving stupid answer, thank you that you kept helping me


----------

