Right content from directory and subfolders

Dear all,

I’m having some problem obtaining the correct contents from a directory.

Watching a tutorial on how to get .rvt files from a directory and related subfolders, I came up with this graph:

Cattura

Writing “.rvt” in the String Node, I’m taking in the list all Revit file in that directory and related subfolders.

Fact is I have some backup files (project.0001.rvt) in that directory, and they are showing in the list as well.
I don’t want the script to run also in those backup files.

There is a way to exclude backup files using a specific code in the string?

Thank you for anyone’s help.

EDIT: If there is a guide on how to use specific Dynamo syntax, I would be happy to read it.

an idea to filter the list to exclude files that have three zeros 000, i think this will help you

1 Like

Hi @khuzaimah.ElecEng

Thank you for your reply.

I’m sure this can work but not in my case.

After you save a Revit Project more than ten times, the backup numbering will be “0010”, “0012”, “0013” and so there will be no longer “000” to exclude. And excluding “00” will clash with my file naming convention.

The text in the string .rvt is using the star character ( * ) and I think is meaning to include every file ending with .rvt.
So maybe there is some other character to exclude part of the text? For instance /.00**/. I may say just stupid things

Here is an image of some c# that uses regular expressions to omit Revit files that have .0000.rvt nomenclature or have (Recovery), if someone saved a file in that location. Both only occur at the end of the file name. You could convert to Python fairly easily.

2 Likes

Thank you for your reply @kraftwerk15.

The c# script code you posted seems great, but I’m not capable of understanding it.
I started with Dynamo a few days ago, and I’m still not familiar with Python and other kinds of coding.

I thank you for your input, but I fear I’m not able to use it…
I hope that someone more capable than me will help me converting your c# :sweat:

EDIT: If there is a guide or someone willing to guide me through the process of conversion I will try my best from the very basics.

import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *
#The inputs to this node will be stored as a list in the IN variables.
files = IN[0]
passed = []
for i in files:
	last = i.rfind(".")
	if ".00" in i[last-5:last]:
		passed.append(False)
	elif "(Recovery)" in i[last-10:last]:
		passed.append(False)
	elif "Copy" in i[last-4:last]:
		passed.append(False)
	else:
		passed.append(True)

#Assign your output to the OUT variable.
OUT = passed

There is probably a prettier way to do this but Python has .rfind(’.’) or .rindex(’.’) which is equivalent to @kfraftwerk15’s .LastIndexOf(’.’) This requires you to use a string from object node though because I was too lazy to do it in python.

As for checking for containment, in python there is if x in y, so my code checks if the last 5 characters in the file name (before .rvt) contains the .00** or recovery/copy tag. You might need to adjust to work for your naming convention but should give a good start.

From there you can use a Filter by bool mask node to get the list. Just make sure you use the files output from Directory.Contents+ as the list input.

1 Like

Ok I’m going the other route of nodes instead of python, regular expressions, or using C# as you’re admittedly a newer user. Yes regular expressions and a more robust python code will help, but don’t go too far into the deep end as a newer user. Stay with Dynamo for now, and learn to think the problem though.

Think the problem though - if all files which are backups and end with “.****.rvt” then every file that is a backup will have two decimals in the file name. This will hold true for only backups as long as you aren’t using decimals in your paths or in file names (which you shouldn’t be as that’ll screw up a lot of Revit stuff), you then can count the number of times a decimal occurs in your path. String.AllIndiciesOf wired into a List.Count (lacing should be longest) to produce a series of 1 and 2 values. If it’s a 1, it’s a main file. if it’s a 2, it’s a backup. Use an == node to allow you to quickly check for the one or two value, and filter your list of files accordingly. The end result looks like this:

Simple and you can do it easily. If you do use decimals in your file names, first please stop doing that after today (this includes in the names of Revit families), then post back so we can help you code a better solution.

Note on the other solutions: Searching for “00” works in some cases, but that reference model you’ve added to a few hundred times over the years can end with “.0101.rvt”, or even “.9999.rvt”. Believe it or not you can even have a “.99999.rvt” (try renaming your last backup and saving your Revit file - you might be surprised what you get). Because of this the number sequence and spacing of the decimals is unreliable as a source - it’s unlikely to change but it could. Sorting by file name, dropping the extension, and then iterating each item against the next to remove the leading string from the trailing, adding a 0 to the front of any remaining strings and attempting to convert the result to a number is likely the "right’ way to filter this - if it worked then the file was most certainly a backup (after all it had the same name followed by .****.rvt. Sequencing of the iteration is the hard part there.

1 Like

@JacobSmall could probably expand more in DesignScript (I’m not the strongest on the syntax there), but you would also have to provide a fallback for those that saved the name of the file with a period in the name, but it was not intended to be a backup file. This is a problem on the end user’s part and probably an opportunity for training, but could potentially happen.

 ProjectNumber_worksharing.revit18.rvt

This is still a valid name, albiet poor file naming. That’s what the Last.IndexOf gets you is working from the right to the left in the file name because I always know, at least by default in Revit, that a backup will come through as a .0000.rvt at the end of the file name, of course unless a user changes it, i.e. another opportunity for training on when to not touch things.

He is also true that I only account for 4 characters at the end of the file name for the backup numbers. If it gets to 5 numbers, something bad had to happen.

Not as bad as what happens when people use decimals in file names or family names… but yes this is likely a sign of something bad… :wink:

Dear @kennyb6,

Thank you for your code, and with a List filter boolean mask is doing its job.
Thank you so much. :smiley:

@JacobSmall

You’re right; I should have tried to face the problem using nodes from Dynamo and its packages and get the right mindset to use the software. Thank you for the wise advice. I’ll keep it in mind for the next Dynamo challenge.

@JacobSmall and @kraftwerk15,

Why is not advised to use decimals in the file name? I actually worked with Revit using this naming convention for almost half a year and I never came up with any problem.
Just to check, with decimals, you’re meaning numbers separated by a dot ( 1.0 or 2.35 ) ?
Probably it is because I was not using Dynamo.

The convention I’m using is

XX-XXX-X-00-XX.rvt

Where X is an alphanumeric value. Can be only numbers, only letter or a mix of them. Only hyphen ( - ) is used as punctuation.

I’m interested in knowing more about in which extent is a risk to use decimals and if my convention using hyphen is safe from that risk :sweat_smile:

Thank you again for your time guys.

Very fair question and one that should be asked more often. Special Characters and length of filename can pose a lot of problems when automating tasks.

When you attempt to automate a number of tasks, Windows is usually not excited to see certain characters in file names. I’m sure you are used to seeing this:

image

A period is an acceptable character in the filename, but if you attempt to save a file with a period in the name while you are in Revit you will receive a message like this, though it will still let you save it:

image

Hyphens are OK. I steer more towards using underscores, mostly because it looks like a space should be there when reading the filename. Just personal preference.

 ProjectNumber_Central_R18.rvt
2 Likes