Batch file for deleting files

Robin -  
BerlinCalling Posted messages 2 Status Membre -
Good evening,
I want to create a Batch file that allows me to regularly clean up a folder where my backup software stores backup files. At the same time, I would like some sort of date filter.

Let me explain, I would like that when I run the batch, it prompts me to enter which date range of files I want to delete from the corresponding folder.

Can someone help me?

Thank you in advance...
Configuration: Windows XP Firefox 2.0.0.3

15 réponses

CH4NCE Posted messages 619 Registration date   Status Membre Last intervention   732
 
And here we go ;)
Copy and paste this code into a text file, then rename this file to WhatYouWant.bat
Put the .bat file in the directory with your logs, then execute it.
This will create the scheduled task and a purgelog.bat file that will be executed on the 1st of every month.
CLS @echo off REM looking at the path of the executable because you didn't give it to me ;) set chemin=%cd% REM Scheduling the tasks. REM At 01:00 on the first of each month we run purgelog.bat AT 01:00 /interactive /EVERY:01 cmd /c "%chemin%\purgelog.bat" REM Creating the batch in the right place echo REM checking the date to deduce the current month and make it the variable %%suppr%%. > purgelog.bat echo set suppr=%%date:~3,2%%>> purgelog.bat echo. >> purgelog.bat echo. >> purgelog.bat echo REM Giving -2 to %%suppr%% >> purgelog.bat echo IF %%suppr%%==01 set suppr=11>> purgelog.bat echo IF %%suppr%%==02 set suppr=12>> purgelog.bat echo IF %%suppr%%==03 set suppr=01>> purgelog.bat echo IF %%suppr%%==04 set suppr=02>> purgelog.bat echo IF %%suppr%%==05 set suppr=03>> purgelog.bat echo IF %%suppr%%==06 set suppr=04>> purgelog.bat echo IF %%suppr%%==07 set suppr=05>> purgelog.bat echo IF %%suppr%%==08 set suppr=06>> purgelog.bat echo IF %%suppr%%==09 set suppr=07>> purgelog.bat echo IF %%suppr%%==10 set suppr=08>> purgelog.bat echo IF %%suppr%%==11 set suppr=09>> purgelog.bat echo IF %%suppr%%==12 set suppr=10>> purgelog.bat echo. >> purgelog.bat echo. >> purgelog.bat echo REM Deleting all files with %%suppr%%. >> purgelog.bat echo del "%chemin%\Sauvegarde????-%%suppr%%-?????;??;??.zip" >> purgelog.bat exit

If this seems too complicated, download the file here:
http://cqgclub.dyndns.org/BDC/telech/InstallPurgelog.bat and run it in your logs directory.
I tested it, but still let me know.
If your computer is not on at the time the task is supposed to run, well, I don't know if it works anyway, but that's okay, go to the logs directory and double-click on purgelog.bat.
See you
--
it's hard to make a signature ;)
14
Rmo5050 Posted messages 13 Registration date   Status Membre Last intervention  
 
Great! ;-))

A huge thank you, it works perfectly.

Maybe see you next time...

Thank you
0
dolu07
 
Hello,

Thank you Ch4nce for the script, just one error for the lines regarding the month choice, to be replaced by these:

echo REM We give -2 to %%suppr%% >> purgelog.bat
echo IF %%suppr%%==01 (set suppr=11 else (>> purgelog.bat
echo IF %%suppr%%==02 (set suppr=12 else (>> purgelog.bat
echo IF %%suppr%%==03 set suppr=01>> purgelog.bat
echo IF %%suppr%%==04 set suppr=02>> purgelog.bat
echo IF %%suppr%%==05 set suppr=03>> purgelog.bat
echo IF %%suppr%%==06 set suppr=04>> purgelog.bat
echo IF %%suppr%%==07 set suppr=05>> purgelog.bat
echo IF %%suppr%%==08 set suppr=06>> purgelog.bat
echo IF %%suppr%%==09 set suppr=07>> purgelog.bat
echo IF %%suppr%%==10 set suppr=08>> purgelog.bat
echo IF %%suppr%%==11 set suppr=09>> purgelog.bat
echo IF %%suppr%%==12 set suppr=10 ))>> purgelog.bat

If we were in January, the variable suppr at 01 took the value 11 and then reverted to the value 09 upon reaching the 11th IF.
5
chaker
 
Hello

Sorry, I don’t understand the syntax

echo IF %%suppr%%==01 (set suppr=11 else (>> purgelog.bat
echo IF %%suppr%%==02 (set suppr=12 else (>> purgelog.bat

I have a problem with January in the ch4ance version; in January it deletes the files from October

Thank you for your help
2
CMP Posted messages 2026 Status Membre 350
 
Hello,
thank you for your intervention, I thought about that too, and if you know how I could do it differently, I would be interested.

Since my C: drive of 160GB contains about 90GB and my backup E: drive has the same capacity, also 160GB, I don't have room for 2 backups.

Ideally, I should do an incremental backup (I'm not sure if that's the exact term), but in the free version of Macrium Reflect, that's not possible. If you know how to do it, that would be great.

I don't want to change the backup program because Macrium allows me, in case of an issue, to "preview" the details of the backup and retrieve any folder or file without having to restore the entire backup, and I really like that.

Apart from backing up to a server, there isn't really a solid solution that protects me from theft, fire, etc.
In case something goes wrong during the backup, I can only hope that my C: drive doesn't fail at the same time.

From time to time, I back up my documents, images, etc. on DVD.

Thank you in advance for any ideas or suggestions.
1
nemesis2488
 
use Cobian Backup, it will allow you to do that.
0
CH4NCE Posted messages 619 Registration date   Status Membre Last intervention   732
 
Hi there.
I think I can help you.
Copy-paste your batch and tell me exactly what you want.
See you soon
--
it's hard to create a signature ;)
0
Robin
 
Actually, the batch is not done (since I don't know how to do it ;-)

The goal is as follows:

In my 'Backup' folder, every day there is a new backup file named with the current date as follows:

'Backup2007-02-24 22;34;24.zip'
'Backup2007-02-25 22;34;24.zip'
'Backup2007-02-26 22;34;24.zip'
'Backup2007-02-27 22;34;24.zip'
'Backup2007-02-28 22;34;24.zip'

So this folder gets heavier day by day and I would like to have a batch file that allows me to delete from the specified folder all the archives I do not wish to keep because they are too old.

Ideally, all files older than 30 days should disappear or only the 20 latest backups should be kept.

I hope I am clearer this way...
0
CH4NCE Posted messages 619 Registration date   Status Membre Last intervention   732 > Robin
 
Okay. So keep the last 20 or keep the last 30.
Let's make it simpler and round to the month. On the 1st of each month, the program will delete the files from the month before last month ;)
I'll do that for you ;)
--
it's hard to make a signature ;)
0
Robin > CH4NCE Posted messages 619 Registration date   Status Membre Last intervention  
 
Thank you, that's really nice...
See you!
0
Willy Nande
 
In my 'Log' folder, there are at least 10 new log files every day named differently without the date of the day as follows:

dsdsfttssvsvsvssgetetetete-gdg-d.log
gdgdtetetet-ddo-ofofofo-fhjfhfhp.log
willllleyeyey-erereret-dgge-err-rr-ro.log

So this folder is getting heavier day by day and I would like to have a batch file that allows me to delete all the archives older than 7 days that I do not want to keep because they are too old.

P.S: the path of the log directory depends on the machine.
0
Willy Nande
 
I'm sorry, but I can only provide translations. Please provide the text you would like translated.
0
krlou
 
Sorry to bring up the topic so long after, but I stumbled upon this script by chance...

One thing I don't understand in your script Ch4nce:

You retrieve the current month in suppr and assign it -2. ==> Ok
You test the possible values of suppr ==> Ok

Except that in your values, won't suppr have possible values from -1 to 10 and not from 1 to 12?

It doesn't change much in the functioning but it's just to verify because otherwise, I don't understand the script ^^
0
Rmo5050
 
Hello,
Thank you for your message, but I don't have the answer.

Check directly with Ch4nce since he's the one who found the "thing".

Have a good evening.
0
JPC
 
It's normal that you have a problem with the month of January and also February.

The correct syntax is that of dolu07.

In Ch4nce, if suppr=01, the first IF replaces suppr with 11, then the 11th IF replaces it with 09 -->>> ERROR

In Dolu07, the first IF replaces suppr with 11 and then does not execute the following IFs thanks to the ELSE command (
The same goes for the second IF if it's the month of February. For the other months, there is no double replacement, so no need for ELSE.

The 2 "))" at the end of the script are there to close the 2 "ELSE (" of the first and second IF.
0
Tgarnxx
 
Hello,
Great script you have. I would like to do the same thing, but every week.
Would the modification of the .bat be complicated or not?
Thanks
0
hc-ch
 
Hello,

I retrieved the above batch and when I run it in my directory, it deletes the most recent files and keeps the older ones... I don't understand where the problem is, maybe it's a matter of regional settings? I'm on Windows Server 2003 in English.

If anyone could give me a hand, that would be nice, thank you!
0
bob
 
Hello,
I know the posts are a bit old but it might still be useful...
Another method is to skip the first x files as follows (to be copied into a bat):

for /F "skip=3" %%A IN ('dir mydirectory\*.* /O:-D /B') do (
del /F/Q %%A
)

explanations:
for /F "skip=3"
=> iterates over the result of the command in parentheses without processing the first 3 results.
dir mydirectory\*.* /O:-D /B
=> /O:-D sorts by date from most recent to oldest
=> /B allows retrieving only the file name

This command keeps only the last 3 files in mydirectory.
0
zip > bob
 
Thank you Bob! That's exactly what I was looking for (with a small modification regarding the deletion)

for /F "skip=5" %%A IN ('dir F:\test\*.* /O:-D /B') do (del /F/Q F:\TEST\%%A)

See you soon!
0
jms667 > zip
 
Excellent bob and zip.
Thank you very much, it's already running in production ;-)
0
wild_child
 
Hello everyone

I am new to programming and all
I want to know how to use the command to delete 'rd' or if there's another way
here is what I've done but it's not working

echo off
cls
rd c:\documents and settings\administrator\desktop\New Bitmap Image.bmp
pause

As you can see, I don't know how to use "rd"
0
diction
 
utilizes del instead of rd
0
Kwoa
 
I created a batch file that allows you to delete all files in a directory by specifying their extension

@echo off
color a
title Deleter By kwoa
:start
echo ÉÍÍÍÍÍÍÍÍ»
echo º-=Kwoa=-º
echo ÈÍÍÍÍÍÍÍͼ
set /p path=Enter the path:
echo cd %path%
CD %path%
set /p ext=Which file format do you want to delete?
echo 1 : Yes
echo 2 : No
echo Are you sure you want to delete the files %ext%?
set /p confirmation=
if %confirmation%==1 del *%ext%
if %confirmation%==2 exit
:error
echo This path is not found, please try again
pause >nul
goto start
0
CMP Posted messages 2026 Status Membre 350
 
@echo off
del "E:\Reflect-Backup\*.mrimg"
0
CMP
 
I finally found it myself after long searches: (I don't know anything about this language)

it's a tiny command to solve a "big" problem and the solution in my case is:

monexemple.bat
@echo off
del /Q "E:\Reflect-Backup\*.mrimg"

then I created a scheduled task with this .bat file path and I'm able to execute it which allows me to automatically delete the existing backup just before my backup starts and there will always be space on my disk for the new backup.

everything works perfectly.
0
the_macraft
 
Hello

I don't think that's the right method.

Deleting a single backup before creating a new one...

What will you do if the new backup fails?
0
akh
 
en soie ns
0
NOK980
 
Hello,
what would be the exact syntax for a batch file that would allow deleting files (only files, without the directories themselves and without confirmation because it will be scheduled at night);
for example for the following directories:
C:\Documents and Settings\moi\Local Settings\History
C:\Documents and Settings\moi\Local Settings\TEMP
C:\RECYCLER
(the recycle bin does not have an address?)

thank you
0
BerlinCalling Posted messages 2 Status Membre
 
Hello everyone,

I'm bringing this topic back up because I need this program exactly! Let me explain: I would like the contents of the following folder (C:\Users\THA0EE\Desktop\test_tausch_ordner) to be deleted on the 1st of every month at 9 AM.

Unfortunately, I don't have sufficient knowledge on how to use a Batch file, but it would be extremely helpful if you could lend me a hand.

Thank you in advance for your help!
Berlin Calling
0
BerlinCalling Posted messages 2 Status Membre
 
To be precise, this would be to simultaneously delete the contents of several folders
(e.g. C:\Users\THA0EE\Desktop\test_tausch_ordner C:\Users\THA0EE\Desktop\My_documents
C:\C:\Users\THA0EE\Desktop\Personal_folder

etc...

Thank you very much!
0