Page 1 of 1

Delete x Second older files

Posted: 02 Aug 2012 03:37
by mohdfraz
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

Re: Delete x Second older files

Posted: 02 Aug 2012 05:37
by abc0502
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

Re: Delete x Second older files

Posted: 02 Aug 2012 08:58
by foxidrive
@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?

Re: Delete x Second older files

Posted: 02 Aug 2012 10:35
by abc0502
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.

Re: Delete x Second older files

Posted: 02 Aug 2012 11:08
by foxidrive
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

Re: Delete x Second older files

Posted: 15 Aug 2012 07:33
by mohdfraz
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.

Re: Delete x Second older files

Posted: 21 Aug 2012 03:57
by mohdfraz
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

Re: Delete x Second older files

Posted: 21 Aug 2012 06:05
by Squashman
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.

Re: Delete x Second older files

Posted: 21 Aug 2012 06:14
by mohdfraz
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.

Re: Delete x Second older files

Posted: 18 Mar 2022 05:40
by ntoreax
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

Re: Delete x Second older files

Posted: 18 Mar 2022 10:13
by aGerman
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