How to keep 2 most recent backups & delete the rest

Hello everyone,

I have a script to delete backup files from folders, with a deep search function included in this. What I want to do is be able to isolate Revit files (Which I have done), but then I need to differentiate between different revit projects in the same folder…
For Example I have:
“Test Project.0001”
“Test Project.0002”
“Test Project.0003”
“Test Project.0004”
“Test Project.0005”
“Test Project.0006”
“Test Project.0007”
Then in the same folder I also have:
“Another Project.0001”
“Another Project.0002”
“Another Project.0003”

What I would like to do is be able to put as many different projects as I have in a folder into separate lists, get the total number of backup files for each project, and delete all of them except the last 2.
So in the above example I would only be left with [“Another Project.0002” “Another Project.0003”] and [“Test Project.0006” “Test Project.0007”]
I can do most of this, but I don’t know how to isolate the different project files into two separate lists so that I can exclude the last 2 items in the list from the Deletion.
Any assistance or direction with this would be really helpful!
PS: Ignore the “Excluding first 3 backups” group, no longer going in that direction.

# Phython-Standard- und DesignScript-Bibliotheken laden
import sys
import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *
import os
from os import *
sys_path = r"C:\Program Files (x86)\IronPython 2.7\Lib"
sys.path.append(sys_path)
directory= IN[0]

files_in_directory= listdir(directory)
z=[]
output = []
for element in files_in_directory:
	if element.endswith(".rfa"):
		if len(element.split(".")) >2:
			output.append(element)

OUT=output

Place “.rfa” for “.rvt” and give a try. If it works you can then use os functions from python to delete the files.

Hi Deniz,

Thanks for the response, I get this error occurring:


Sorry I am very new to Python, so wouldn’t know exactly how to debug this.

import sys
import clr
sys_path = r"C:\Program Files (x86)\IronPython 2.7\Lib"
sys.path.append(sys_path)
import os
from os import *

directory= IN[0]

files_in_directory= listdir(directory)
z=[]
output = []
for element in files_in_directory:
	if element.endswith(".rvt"):
		if len(element.split(".")) >2:
			output.append(element)

OUT=output

It is weird. It works on my side. Can you try that one?

That one works thank you :slight_smile:

However, I end up with exactly the same results I already had… Do you perhaps know how to now split these different projects into two separate lists? Have the " rac_basic" files in one list and the “Export naming test” files in a separate list?
From there I can delete the files, but I don’t have the knowledge to split these items, especially when there are varying amounts of files for each project.

import sys
import clr
sys_path = r"C:\Program Files (x86)\IronPython 2.7\Lib"
sys.path.append(sys_path)
import os
from os import *

directory= IN[0]

files_in_directory= listdir(directory)
z=[]
output = []
for element in files_in_directory:
	if element.endswith(".rvt"):
		if len(element.split(".")) >2:
			output.append(element)
rac_basic = []
naming = []			
for el in output:

	if el.startswith("rac_basic"):
		rac_basic.append(el)
	else:
		naming.append(el)
		

OUT= rac_basic, naming

maybe like that?

Hi Deniz,

Yes!! That is exactly what I am looking for, thank you!

However, this also explains my problem better actually. That Project file name is going to change very often, as I want to run the script on several different folders, so there will always be different project names in the list. Do you think there is a way to grab the name of the project file in the list, then use that to feed into this section “if el.startswith(“rac_basic”):”, replacing the “rac_basic” with whatever the file name might be?
If not, I really appreciate what you have given me so far!

import sys
import clr
sys_path = r"C:\Program Files (x86)\IronPython 2.7\Lib"
sys.path.append(sys_path)
import os
from os import *

directory= IN[0]

files_in_directory= listdir(directory)
z=[]
test=[]
output = []

for element in files_in_directory:
	if element.endswith(".rvt"):
		if len(element.split(".")) >2:
			output.append(element.split(".")[0])
liste = [[] for i in range(len(set(output)))]
for i,x in enumerate(set(output)):
	for elem in output:
		if elem.startswith(x):
			
			liste[i].insert(i,elem)
		
			

OUT= liste

Can you give a try? :slight_smile:

Hi Deniz,

Once again, so so close, it is the exact structure that I need to use and that I am looking for, but I need that exact structure in a file path format, I think the current output is a string?
If you look below, I am going to take the actual files, drop the last two items, and delete the rest. But to use the node, I need path’s as an input.

Sorry that one would work better:

import sys
import clr
sys_path = r"C:\Program Files (x86)\IronPython 2.7\Lib"
sys.path.append(sys_path)
import os
from os import *

directory= IN[0]

files_in_directory= listdir(directory)
z=[]
test=[]
output = []

for element in files_in_directory:
	if element.endswith(".rfa"):
		if len(element.split(".")) >2:
			output.append(element.split(".")[0])

liste = [[] for i in range(len(set(output)))]

for i,x in enumerate(set(output)):
	for elem in output:
		if elem==(x):
			liste[i].append(elem)
		
			

OUT= liste
import sys
import clr
sys_path = r"C:\Program Files (x86)\IronPython 2.7\Lib"
sys.path.append(sys_path)
import os
from os import *

directory= IN[0]

files_in_directory= listdir(directory)
z=[]
test=[]
output = []

for element in files_in_directory:
	if element.endswith(".rvt"):
		if len(element.split(".")) >2:
			output.append(element.split(".")[0])

liste = [[] for i in range(len(set(output)))]

for i,x in enumerate(set(output)):
	for elem in output:
		if elem==(x):
			liste[i].append(elem)
			
for i, objekt in enumerate(files_in_directory):
	for y in set(output):
		if y == objekt.split(".")[0]:
			z.append(objekt)

for memo in set(output):
	if memo +".rvt" in z:
		z.remove(memo+".rvt")
	
OUT= liste,z

That one should be like you’d like to have :slight_smile:

That code has kept the two separate lists of strings and then added the full list of file paths without splitting: :grimacing:

Oh my bad _S you are right, my brain does not work today very effective :slight_smile:

Even in your “Not effective” state, you are far more effective than I am with Python :face_with_raised_eyebrow: :slight_smile:

Hello Sigma,

sorry I was away and could not complete the code :slight_smile:

import sys
import clr
sys_path = r"C:\Program Files (x86)\IronPython 2.7\Lib"
sys.path.append(sys_path)
import os
from os import *

directory= IN[0]

files_in_directory= listdir(directory)
z=[]
test=[]
output = []

for element in files_in_directory:
	if element.endswith(".rvt"):
		if len(element.split(".")) >2:
			output.append(element.split(".")[0])

liste = [[] for i in range(len(set(output)))]

for i,x in enumerate(files_in_directory):
	
	if x.endswith(".rvt"):
		for eleme in set(output):
			z.append(eleme)
			neu = set(output)
			if len(x.split(".")[1]) ==4 and x.split(".")[0] == eleme:
				liste[z.index(eleme)].append(x)
			
	
	
OUT= liste 

Can you try that?

No problem Deniz,

Thanks very much for your response.

I know get this error…
image

Which is weird because nothing else has changed…

What do connect as directory path?

I’m feeding in the exact same list as I have been for all of your code provided, a list of directory contents.

Please connect only directory, the code will list the content of directory. If you have more than 1 directory then we have to add a loop there.

Ahh thank you Deniz that works:)

Curios though, as all the code worked before on that node that I was feeding in, and now all of a sudden it stops. The reasons I took it from the “Directory.Contents” node is because that node has the ‘deepSearch’ function… With this method, I guess I would lose that functionality?