Delete x Second older files

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
mohdfraz
Posts: 69
Joined: 29 Jun 2011 11:16

Delete x Second older files

#1 Post by mohdfraz » 02 Aug 2012 03:37

Hi,

I have a folder c:\a it receives hundreds of *.txt files every minute e.g.
-60710.41123.563506944447
106530.41123.594097222223
117090.41123.594259259262
etc..

I want batch file code which can delete X minute older files. If we try to delete all files then running software stops as it is in the process of writing files while the other program tries to delete them.

Thanks

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Delete x Second older files

#2 Post by abc0502 » 02 Aug 2012 05:37

Hi, Here is the code, Test on some files first

This batch will keep running untill you stop it and if it find a file is older than Xtime it delete it
Put the batch in the same folder with the txt files
@echo off
cls
Mode 50,10
Title Xtime Delete
setlocal enabledelayedexpansion

:Loop
set "Xtime=1"

For /F "tokens=2,* delims=:" %%a in ('echo %time%') Do set CurrTime=%%a
For %%a in (*.txt) Do (
For /F "tokens=3 delims=: " %%b in ("%%~ta") Do (
set "Ftime=%%b"
set /a check = CurrTime - Ftime
IF !check! GEQ !Xtime! ( del /f /Q %%~fsa
) Else ( goto next )
)
)

:next
goto Loop

change the Xtime to suit your needs

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Delete x Second older files

#3 Post by foxidrive » 02 Aug 2012 08:58

@abc0502 - will am and pm indicators cause issues here? How about hours before 10am which have a leading space?


Some questions to the OP:

what is the process doing with the files?
Will the files be locked by the process?
How does the process decide which files need to be processed? And can the process delete the files after it uses them?


How about a script that skips the newest 50 files and deletes the rest?

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Delete x Second older files

#4 Post by abc0502 » 02 Aug 2012 10:35

Hi, Foxidrive
It seems that there is a miss undertanding, The Xtime is a minutes not hours,
he was asking for a period of minuts then it delete files if they was older than that period of time.

and it seems that he can delete the files that it is being written by the software but his problem is that the software itself is hanging when a file that is used by that software is deleted.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Delete x Second older files

#5 Post by foxidrive » 02 Aug 2012 11:08

Yes, sorry, I didn't examine your code closely. But what will happen if the file time is 59 minutes and the current time ticks over to 00 minutes?


I'd wonder if this is useful to the OP, if a quantity of the most recent files can be left for the process, say 500 as listed below - and wait 10 seconds between runs

Code: Select all

@echo off
:loop
for /f "skip=500 delims=" %%a in ('dir *.txt /a-d /b /o:-d') do (
echo "%%a"
del "%%a"
)
ping -n 10 localhost >nul
goto :loop

mohdfraz
Posts: 69
Joined: 29 Jun 2011 11:16

Re: Delete x Second older files

#6 Post by mohdfraz » 15 Aug 2012 07:33

abc0502 wrote:Hi, Here is the code, Test on some files first

This batch will keep running untill you stop it and if it find a file is older than Xtime it delete it
Put the batch in the same folder with the txt files
@echo off
cls
Mode 50,10
Title Xtime Delete
setlocal enabledelayedexpansion

:Loop
set "Xtime=1"

For /F "tokens=2,* delims=:" %%a in ('echo %time%') Do set CurrTime=%%a
For %%a in (*.txt) Do (
For /F "tokens=3 delims=: " %%b in ("%%~ta") Do (
set "Ftime=%%b"
set /a check = CurrTime - Ftime
IF !check! GEQ !Xtime! ( del /f /Q %%~fsa
) Else ( goto next )
)
)

:next
goto Loop

change the Xtime to suit your needs




abc0502, It worked, Thank you. You are great. Many thanks.

mohdfraz
Posts: 69
Joined: 29 Jun 2011 11:16

Re: Delete x Second older files

#7 Post by mohdfraz » 21 Aug 2012 03:57

But for my case, it did not worked as good as I was expecting so I came up with another idea which is 100% accurate and safe. Suppose u want to delete 1 minute older files in your folder1 so copy all folder files in Folder2 and then run this below code. It will syncronize and delete same file names in folder1 then u can delete all the folder2 files. This way ur runing software will not issue error and u can delete the files as well at given interval time.


Code: Select all


:Loop
SET Fold1=.\Folder1
SET Fold2=.\Folder2
FOR /F "Delims=" %%f in ('DIR  /b /on "%Fold2%\*.*"') DO (
   IF EXIST "%Fold1%\%%f" DEL /F /Q "%Fold1%\%%f"
   REM ECHO %%~nf  ---  %%~zf
)

ping -n 60 127.0.0.1

goto :Loop



Thanks

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: Delete x Second older files

#8 Post by Squashman » 21 Aug 2012 06:05

mohdfraz wrote:

Code: Select all


:Loop
SET Fold1=.\Folder1
SET Fold2=.\Folder2
FOR /F "Delims=" %%f in ('DIR  /b /on "%Fold2%\*.*"') DO (
   IF EXIST "%Fold1%\%%f" DEL /F /Q "%Fold1%\%%f"
   REM ECHO %%~nf  ---  %%~zf
)

ping -n 60 127.0.0.1

goto :Loop



I assume folder 2 is a backup folder and folder 1 is some drop folder that creates new files. Seems like the script would run faster if your listed the files in Folder 1 and then checked to see if they already existed in Folder 2 because Folder 2 should always have more files in it than folder 1. Then delete from folder 1 if they exist in folder 2.

mohdfraz
Posts: 69
Joined: 29 Jun 2011 11:16

Re: Delete x Second older files

#9 Post by mohdfraz » 21 Aug 2012 06:14

Squashman wrote:I assume folder 2 is a backup folder and folder 1 is some drop folder that creates new files. Seems like the script would run faster if your listed the files in Folder 1 and then checked to see if they already existed in Folder 2 because Folder 2 should always have more files in it than folder 1. Then delete from folder 1 if they exist in folder 2.


Once the files are in Folder2 the backup one then I do some process which keep the required file and delete rest all. So NO folder2 number of files never increase from Folder1.

But u need to check if this suites ur requirements.

ntoreax
Posts: 1
Joined: 18 Mar 2022 05:17

Re: Delete x Second older files

#10 Post by ntoreax » 18 Mar 2022 05:40

hello everyone
am new to the bat scripting allow me to ask a few question on the shared script
so what this one does is it picks files older than 1 min lets say depending on what value provided here ( set "Xtime=1")

@echo off
cls
Mode 50,10
Title Xtime Delete
setlocal enabledelayedexpansion

:Loop
set "Xtime=1"

For /F "tokens=2,* delims=:" %%a in ('echo %time%') Do set CurrTime=%%a
For %%a in (*.txt) Do (
For /F "tokens=3 delims=: " %%b in ("%%~ta") Do (
set "Ftime=%%b"
set /a check = CurrTime - Ftime
IF !check! GEQ !Xtime! ( del /f /Q %%~fsa
) Else ( goto next )
)
)

:next
goto Loop

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Delete x Second older files

#11 Post by aGerman » 18 Mar 2022 10:13

ntoreax wrote:
18 Mar 2022 05:40
allow me to ask a few question on the shared script
Sure. However you didn't ask any question.

Steffen

Post Reply