Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to exclude folders with spaces in the name in contains()

7 REPLIES 7
SOLVED
Reply
Message 1 of 8
Thomas1365
465 Views, 7 Replies

How to exclude folders with spaces in the name in contains()

I am trying to create an iLogic rule which will sift through assemblies and extract information from the iProperties and export it to an excel spreadsheet.I have leveraged heavily off of a previous forum post which wants to do something similar: https://forums.autodesk.com/t5/inventor-ilogic-and-vb-net-forum/iproperties-export-to-excel/td-p/921...

 

I have a number of folders that I do not want to search through in the directory, such as OldVersions in the forum post above. The rule works great for excluding folders without and spaces in the name, but it won't run if I try to search for folders with a space.

 

I made a test folder where I have some parts and three folders: "OldVersions", "TestFolder" and "Test Folder" each with parts in them to try debug it.  Using the following lines of code will ignore the parts in OldVersions and TestFolder, and find the part in Test Folder:

 

	For Each File As IO.FileInfo In Folder.GetFiles("*.ipt",IO.SearchOption.AllDirectories)

	    If File.FullName.Contains("OldVersions" ) = False Then
			If File.FullName.Contains("TestFolder") = False Then
				FileList.Add(File.FullName)
			End If 
		End If	
		
	Next

 I want to exclude all 3 folders, and tried the following lines of code:

	For Each File As IO.FileInfo In Folder.GetFiles("*.ipt",IO.SearchOption.AllDirectories)

	    If File.FullName.Contains("OldVersions" ) = False Then
			If File.FullName.Contains("TestFolder") = False Then
				If File.FullName.Contains("Test Folder") = False Then
					FileList.Add(File.FullName)
				End If 
			End If 
		End If	
		
	Next

 However this fails to run, I think the rule gets to the line with "Test Folder" then cancels. How do I define folder names with the spaces?

 

Thanks in advance

Tags (2)
Labels (2)
7 REPLIES 7
Message 2 of 8
FINET_Laurent
in reply to: Thomas1365

Hi @Thomas1365,

 

Is there a specific reason why you are using .contains intead of equal ? If you are looking for specific folder name, I would do it like this (and also cleaning the code with guard clauses) : 

For Each File As IO.FileInfo In Folder.GetFiles("*.ipt",IO.SearchOption.AllDirectories)
	If File.FullName = "OldVersions" Then Continue For
	If File.FullName = "TestFolder" Then Continue For
	If File.FullName = "Test Folder" Then Continue For

	FileList.Add(File.FullName)

Next

Does this suits your needs  ?

 

Kind regards,

FINET L. 

 

If this post solved your question, please kindly mark it as "Solution"

If this post helped out in any way to solve your question, please drop a "Like"

@LinkedIn     @JohnCockerill

Message 3 of 8
Thomas1365
in reply to: FINET_Laurent

Thanks for the response @FINET_Laurent,

 

I think that's a better approach, however I do want to exclude those files so I tried:

For Each File As IO.FileInfo In Folder.GetFiles("*.ipt",IO.SearchOption.AllDirectories)
	If File.FullName Is Not "OldVersions" Then Continue For
	If File.FullName Is Not "TestFolder" Then Continue For
	If File.FullName Is Not "Test Folder" Then Continue For

	FileList.Add(File.FullName)

Next

 However it didn't work. Is it a syntax issue on my end?

Message 4 of 8
FINET_Laurent
in reply to: Thomas1365

Hi @Thomas1365,

I think it is indeed an issue on your side. You should replace the "Is Not" statement with a simple " = " as I did, and I suppose it will do the trick. To understand this better, here is some lecture : 

 

Guard Clause | DevIQ

Continue Statement - Visual Basic | Microsoft Learn

 

Kind regards,

FINET L.

If this post solved your question, please kindly mark it as "Solution"

If this post helped out in any way to solve your question, please drop a "Like"

@LinkedIn     @JohnCockerill

Message 5 of 8

Hi @Thomas1365 and @FINET_Laurent 

 

I think contains is required here, because File.Fullname is returning the file path.

 

You could use something like this:

 

Dim Folder As New IO.DirectoryInfo("C:\Temp")
Dim FileList As New List(Of String)

For Each File As IO.FileInfo In Folder.GetFiles("*.ipt", IO.SearchOption.AllDirectories)
	If File.FullName.contains("OldVersions") Then Continue For
	If File.FullName.contains("TestFolder") Then Continue For
	If File.FullName.contains("Test Folder") Then Continue For
	FileList.Add(File.FullName)
Next

 

or maybe something like this

 

Dim RootFolder As New IO.DirectoryInfo("C:\Temp")
Dim FileList As New List(Of String)

For Each oFolder As IO.DirectoryInfo In RootFolder.GetDirectories("*", IO.SearchOption.AllDirectories)

	If oFolder.Name = "OldVersions" Then Continue For
	If oFolder.Name = "TestFolder" Then Continue For
	If oFolder.Name = "Test Folder" Then Continue For

	For Each File As IO.FileInfo In oFolder.GetFiles("*.ipt", IO.SearchOption.AllDirectories)
		FileList.Add(File.FullName)
	Next
Next

 

Or even something like this:

 

Dim RootFolder As New IO.DirectoryInfo("C:\Temp")
Dim FileList As New List(Of String)
Dim ExcludeList = New String() {"OldVersions", "TestFolder", "Test Folder" }

For Each oFolder As IO.DirectoryInfo In RootFolder.GetDirectories("*", IO.SearchOption.AllDirectories)

	If ExcludeList.Contains( oFolder.Name) Then Continue For

	For Each File As IO.FileInfo In oFolder.GetFiles("*.ipt", IO.SearchOption.AllDirectories)
		FileList.Add(File.FullName)
	Next
Next

I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com

Message 6 of 8

Thanks for your help @Curtis_Waguespack and @FINET_Laurent .

 

I liked the 3rd rendition the most as it's the cleanest and I can easily add more filenames to exclude (the real application has many more different file names and sub folders). This seems closer to working how I want, however it doesn't return the parts within the root folder.

 

I tried to solve this by taking the RootFolder up a level, however it then returns everything within the folder because (I believe) it's only checking for the "ExcludeList" the initial time and then adds it to the list. I think I need to add another layer to the For loop to check again within each folder.

 

I think it might help if I explain a bit better my testing folder arrangement:

  •  S:\Testing\Macro Testing:
    • S:\Testing\Macro Testing\Testing Folder
      • S:\Testing\Macro Testing\Testing Folder\OldVersions
        • I DONT WANT THIS.ipt
      • S:\Testing\Macro Testing\Testing Folder\Test Folder
        • I DONT WANT THIS.ipt
      • S:\Testing\Macro Testing\Testing Folder\TestFolder
        • I DONT WANT THIS.ipt
      • Cube 2.ipt
      • Cube 1.ipt

So I know if I see the "I DONT WANT THIS" part it's looked in the wrong folder (I also have it returning the file directory to double check).

 

If I run the RootFolder as "S:\Testing\Macro Testing\Testing Folder" I will get no results in my exported excel, I believe this is because the script doesn't say to check in the RootFolder itself for .ipt files. So I changed the RootFolder to be a level up as: "S:\Testing\Macro Testing". This then returns every part including the "I DONT WANT THIS" as I mentioned.

 

I have tried to add another check for the ExcludeList after finding applicable folders:

Dim RootFolder As New IO.DirectoryInfo("S:\Testing\Macro Testing")
Dim FileList As New List(Of String)
Dim ExcludeList = New String() {"OldVersions", "TestFolder", "Test Folder"}

For Each oFolder As IO.DirectoryInfo In RootFolder.GetDirectories("*", IO.SearchOption.AllDirectories)
	If ExcludeList.Contains(oFolder.Name) Then Continue For
		For Each oSubFolder As IO.DirectoryInfo In oFolder.GetDirectories("*", IO.SearchOption.AllDirectories)
			If ExcludeList.Contains(oSubFolder.Name) Then Continue For
				For Each File As IO.FileInfo In oSubFolder.GetFiles("*.ipt", IO.SearchOption.AllDirectories)
					FileList.Add(File.FullName)
				Next
			For Each File As IO.FileInfo In oFolder.GetFiles("*.ipt", IO.SearchOption.AllDirectories)	
				FileList.Add(File.FullName)
			Next 
		Next 

Next

However this returns nothing when running the script. I think I need to better define to first look for parts in the first level of the folders, and then look at the folders, then ignore any from the ExcludeList.

 

Thanks so much for your help. This is my first time trying to make anything in iLogic  and I also don't have any programming background, so I apologise if I'm missing obvious things.

Message 7 of 8

Hi @Thomas1365

 

Try this version. It first gets a list of folders, then looks through those folders for files

 

I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com

Dim RootFolder As New IO.DirectoryInfo("C:\Temp")
Dim FileList As New List(Of String)
Dim ExcludeList = New String() {"OldVersions", "TestFolder", "Test Folder" }

Dim FolderList As New List(Of IO.DirectoryInfo)

'add root folder
FolderList.Add(RootFolder)

'get sub folders
For Each oFolder As IO.DirectoryInfo In RootFolder.GetDirectories("*", IO.SearchOption.AllDirectories)
	If ExcludeList.Contains(oFolder.Name) Then Continue For
	FolderList.Add(oFolder)
Next

For Each oFolder In FolderList
	For Each File As IO.FileInfo In oFolder.GetFiles("*.ipt", IO.SearchOption.TopDirectoryOnly)
		FileList.Add(File.FullName)
	Next
Next

 

Message 8 of 8

Hi @Curtis_Waguespack,

 

That works! Fantastic. Thank you very much.

 

I have added another layer to the smarts of the script to eliminate parts from the list I do not want to open. I've achieved this by adding another string for "ExcludeListParts", below removed Cube 2 from being added to the file list:

 

Dim RootFolder As New IO.DirectoryInfo("S:\Testing\Macro Testing")
Dim FileList As New List(Of String)
Dim ExcludeList = New String() {"OldVersions", "TestFolder", "Test Folder"}
Dim ExcludeListParts = New String() {"Cube 2.ipt"}
Dim FolderList As New List(Of IO.DirectoryInfo)

'add root folder
FolderList.Add(RootFolder)

'get sub folders
For Each oFolder As IO.DirectoryInfo In RootFolder.GetDirectories("*", IO.SearchOption.AllDirectories)
	If ExcludeList.Contains(oFolder.Name) Then Continue For
	FolderList.Add(oFolder)
Next

For Each oFolder In FolderList
	For Each File As IO.FileInfo In oFolder.GetFiles("*.ipt", IO.SearchOption.TopDirectoryOnly)
		If ExcludeListParts.Contains(File.Name) Then Continue For
		FileList.Add(File.FullName)
	Next
Next

Is there a way to eliminate a file with a certain key word from being added to the list?

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Technology Administrators


Autodesk Design & Make Report