File/Folder Purging - Best practice?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

File/Folder Purging - Best practice?

#1 Post by SIMMS7400 » 14 Sep 2020 04:18

Hi Folks -

I'm trying to developing a purging process but I haven't been able to land on anything that I really like yet so wanted to get your opinion. What is the best way to go about this?

Can I merely just use a forfiles to remove an entire directory based on AGE, or should I remove files first and then the folder/subfolders?

I have this piece of code:

Code: Select all

FORFILES /P "D:\Data\Imports" /S /M "*" /D -14 /C "CMD /C if @ISDIR==TRUE echo RD /S /Q @PATH"
In my \Import directory, I have sub-folders named as YYYY_MMDD to hold my imports that I load into my target application. Therefore, wondering the best, most effcieint way to purge a setup like this?

I understand my above code will not purge empty dirs, so in the case, I planned to just use a ROBOCOPY to take care of that.

Code: Select all

ROBOCOPY "D:\Data\Imports" "D:\Data\Imports" /S /MOVE
What are your thoughts?

Jedininja
Posts: 25
Joined: 11 Jan 2022 22:41
Location: CanafukpilesDa
Contact:

Re: File/Folder Purging - Best practice?

#2 Post by Jedininja » 12 Jan 2022 00:29

this is a method if you have reoccurring files that need to be deleted.

Code: Select all

if exist %systemdrive%/file.xxx del /f /q %systemdrive%/file.xxx
if exist %systemdrive%/file.xxx del /f /q %systemdrive%/file.xxx
if exist %systemdrive%/file.xxx del /f /q %systemdrive%/file.xxx
if exist %systemdrive%/file.xxx del /f /q %systemdrive%/file.xxx
or

off the top of my head to delete files below a numaric date

Will list files in a in directory with date and output first in to a file

Code: Select all

dir /s > %systemdrive%\path\file.txt
and then the forloop should skip the first 5 lines and the / delimiter and then the if statement will determine if the if the value is below the year 2022(it may be the day, or month). however i don't really have a way to test it or anything to delete. there is also "jarrgin" in dir /s so you will have to figure that part.

Code: Select all

FOR /F "eol=5 delims=/" %%i in (%systemdrive%\path\File.txt) do if /i %%i  lss 22 del /s %%i

Post Reply