My Import Script is importing everything twice! can anyone see Why?

Hi Everyone,

I made an import script which imports files by selecting 1 folder and importing all files in that folder and subfolders.
It also puts the imports in layers with the subfolder Name.
But now I just realised is importing everything twice and I can’t figure out why?

Can someone take a look and see if you can figure out why?

Thanks so much
Chico

Import_Files.rvb(1.7 KB)

I also copied it here so you don’t have to download it.

Call BatchProcessFiles()

Sub BatchProcessFiles()

	Dim sFolder, oFSO, oFolder

	' Allow the user to interactively pick a folder
	sFolder = Rhino.BrowseForFolder(, "Select folder to process", "Batch Process Files")
	If VarType(sFolder) <> vbString Then Exit Sub

	' Create a file system object
	Set oFSO = CreateObject("Scripting.FileSystemObject") 

	' Get a folder object based on the selected folder
	Set oFolder = oFSO.GetFolder(sFolder)
  
	' Process the folder
	Call RecurseFolder(oFolder)

End Sub

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' RecurseFolder
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Sub RecurseFolder(oFolder)

	Dim oFile, oSubFolder

	' Process each file in the folder
	For Each oFile In oFolder.Files
		Call ProcessFile(oFolder,oFile)
	Next

	' Process each subfolder in this folder
	For Each oSubFolder In oFolder.SubFolders
		Call RecurseFolder(oSubFolder)
	Next

End Sub 

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' ProcessFile
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Sub ProcessFile(oFolder,oFile)

	Dim strOpen,arrLCO,strSubFolder
	
	For Each oFile In oFolder.Files
		
		strOpen = LCase(oFile.Path)
			
		Call Rhino.Command ("_NoEcho _-Import "&chr(34)&strOpen&chr(34)&" _Enter")
		arrLCO=Rhino.LastCreatedObjects(False)
		
		strSubFolder = oFolder.Name
		rhino.AddLayer(strSubFolder)
		Rhino.ObjectLayer arrLCO,strSubFolder
		
	Next

End Sub

I just seen that it imports more then Twice all files

If I have 4 files in a subfolder it imports everyfile in that subfolder 4 times…

I can’t see why, I bet is something really stupid and I can’t see.

I don’t think you want to have the

For Each oFile in oFolder.Files

in your ProcessFile function

Hi Steve,

that was exactly it, now works perfect. it makes sense now that I think about it.

Thanks so much.

Dear Quico, i think not. (talking about the version before you´ve edited your post and the current version above). The first version crashed rhino because it was running in an endless loop importing the files from strFolder over and over again, ignoring any subfolders.

The current version is not less dangerous. You asume you can import any file type into rhino and simply send _Enter to the commandline, regardless of the amount of import options each file format has. OBJ or STEP files for example will ask for different import options. At this point, your script completes the option prompts by sending the path of the next file to import. You might want to change the _Enter at the end of the Import command string to “_EnterEnd” to prevent this.

Please note that validating file extensions before batch importing files into Rhino should never be ommited.

c.

See if this examples is helpful (or not…)

https://github.com/mcneel/rhinoscript/blob/master/BatchImport.rvb

Hi everyone,

I know this script isn’t perfect, But I’m writing it for a specific task of importing always the same file type and folder organization.

With more time and after learning alot more about scripting I think I can make it much better.
I only started with scripts this week, never had any lesson or anyone helping.

This has been a great experience, really enjoying this.
and specially thanks to everyone for your help, without forums like this and you guys I could have never got this far!!

Good luck and happy scripting Chico !