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.
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?
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
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
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.
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
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
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?