Filename based on folder structure

Filename based on folder structure

zikmund
Contributor Contributor
1,000 Views
14 Replies
Message 1 of 15

Filename based on folder structure

zikmund
Contributor
Contributor

Hi,

 

Is it possible to generate file name based on the folder structure where I want this file to be saved?

 

Example:

I have folder structure like this: C:\VaultWorkspace\Projects\AA\BB\CCC\D\EE\FFFFF

And file should have name: AABB-CCC-D-EE-FFFFF.iam

 

Thanks all for your help.

Milan

0 Likes
1,001 Views
14 Replies
Replies (14)
Message 2 of 15

Andrii_Humeniuk
Advisor
Advisor
Accepted solution

Hi @zikmund . Please try this code:

 

Sub main	
	oDoc = ThisDoc.Document
	Dim oFileDlg As Inventor.FileDialog = Nothing
	InventorVb.Application.CreateFileDialog(oFileDlg)
	oFileDlg.Filter = "Inventor Files (*.iam;*.ipt)|*.iam;*.ipt|All Files (*.*)|*.*"
	oFileDlg.ShowSave()
	MyFile = oFileDlg.FileName
	If oFileDlg.FileName Is Nothing Or oFileDlg.FileName = "" Then Exit Sub
	Dim sPath As String = System.IO.Path.GetDirectoryName(oFileDlg.FileName)
	Dim sExten As String = System.IO.Path.GetExtension(oFileDlg.FileName)
	Dim sF_Name As String = System.IO.Path.GetFileNameWithoutExtension(oFileDlg.FileName)
	Dim sCutName As String = sPath.Remove(0, sPath.IndexOf("Projects\") + 9)
	Dim sA_Name As String = sCutName.Substring(0, sCutName.IndexOf("\"))
	sCutName = sCutName.Remove(0, sCutName.IndexOf("\") +1)
	Dim sB_Name As String = sCutName.Substring(0, sCutName.IndexOf("\"))
	sCutName = sCutName.Remove(0, sCutName.IndexOf("\") +1)
	Dim sC_Name As String = sCutName.Substring(0, sCutName.IndexOf("\"))
	sCutName = sCutName.Remove(0, sCutName.IndexOf("\") +1)
	Dim sD_Name As String = sCutName.Substring(0, sCutName.IndexOf("\"))
	Dim sE_Name As String = sCutName.Remove(0, sCutName.IndexOf("\") +1)
	Dim sFullName As String = sPath & "\" & sA_Name & sB_Name & "-" & sC_Name & "-" & sD_Name & _
								"-" & sE_Name & "-" & sF_Name & sExten
	oDoc.SaveAs(sFullName, False)
End sub

Andrii Humeniuk - CAD Coordinator, Autodesk Certified Instructor

LinkedIn | My free Inventor Addin | My Repositories

Did you find this reply helpful ? If so please use the Accept as Solution/Like.

EESignature

Message 3 of 15

zikmund
Contributor
Contributor

Hi Andrii,

 

I created new rule (I tried assembly and part) and copied the code, but when I ran it, it just opened the save dialog and File name row is empty.

 

Milan

0 Likes
Message 4 of 15

Andrii_Humeniuk
Advisor
Advisor
Accepted solution

FFFFF - is name file or folder? If folders then you need this code:

 

Sub main
	oDoc = ThisDoc.Document
	Dim oFileDlg As Inventor.FileDialog = Nothing
	InventorVb.Application.CreateFileDialog(oFileDlg)
	oFileDlg.Filter = "Inventor Files (*.iam;*.ipt)|*.iam;*.ipt|All Files (*.*)|*.*"
	oFileDlg.FileName = "Select the file storage path"
	oFileDlg.ShowSave()
	MyFile = oFileDlg.FileName
	If MyFile Is Nothing Or MyFile = "" Then Exit Sub
	Dim sPath As String = System.IO.Path.GetDirectoryName(oFileDlg.FileName)
	Dim sExten As String = System.IO.Path.GetExtension(oFileDlg.FileName)
	Dim sCutName As String = sPath.Remove(0, sPath.IndexOf("Projects\") + 9)
	Dim sA_Name As String = sCutName.Substring(0, sCutName.IndexOf("\"))
	sCutName = sCutName.Remove(0, sCutName.IndexOf("\") +1)
	Dim sB_Name As String = sCutName.Substring(0, sCutName.IndexOf("\"))
	sCutName = sCutName.Remove(0, sCutName.IndexOf("\") +1)
	Dim sC_Name As String = sCutName.Substring(0, sCutName.IndexOf("\"))
	sCutName = sCutName.Remove(0, sCutName.IndexOf("\") +1)
	Dim sD_Name As String = sCutName.Substring(0, sCutName.IndexOf("\"))
	sCutName = sCutName.Remove(0, sCutName.IndexOf("\") +1)
	Dim sE_Name As String = sCutName.Substring(0, sCutName.IndexOf("\"))
	Dim sF_Name As String = sCutName.Remove(0, sCutName.IndexOf("\") +1)
	Dim sFullName As String = sPath & "\" & sA_Name & sB_Name & "-" & sC_Name & "-" & sD_Name & _
								"-" & sE_Name & "-" & sF_Name & sExten
	oDoc.SaveAs(sFullName, False)
End Sub

 

 

You need to choose the path to save the file, only then the rule will understand the required file name.

Снимок экрана 2023-05-04 095028.png

Andrii Humeniuk - CAD Coordinator, Autodesk Certified Instructor

LinkedIn | My free Inventor Addin | My Repositories

Did you find this reply helpful ? If so please use the Accept as Solution/Like.

EESignature

Message 5 of 15

zikmund
Contributor
Contributor

Hi Andrii,

 

Thanks for your help. It works perfectly for the assemblies! Amazing!

 

Can you help me also with the parts? Our idea is that each part will be named AABB-CCC-D-EE-FFFFF.GGGG

Where GGGG will be autogenerated sequence.

 

Do you think it's possible?

 

With best regards

Milan

 

 

0 Likes
Message 6 of 15

Andrii_Humeniuk
Advisor
Advisor

The file name will be the time of its creation. Example: 638187949256360534. This will help avoid file name mismatches.

Sub main
	oDoc = ThisDoc.Document
	Dim oFileDlg As Inventor.FileDialog = Nothing
	InventorVb.Application.CreateFileDialog(oFileDlg)
	oFileDlg.Filter = "Inventor Files (*.iam;*.ipt)|*.iam;*.ipt|All Files (*.*)|*.*"
	oFileDlg.FileName = "Select the file storage path"
	oFileDlg.ShowSave()
	MyFile = oFileDlg.FileName
	If MyFile Is Nothing Or MyFile = "" Then Exit Sub	
	Dim sPath As String = System.IO.Path.GetDirectoryName(oFileDlg.FileName)
	Dim sCutName As String = sPath.Remove(0, sPath.IndexOf("Projects\") + 9)
	Dim sA_Name As String = sCutName.Substring(0, sCutName.IndexOf("\"))
	sCutName = sCutName.Remove(0, sCutName.IndexOf("\") +1)
	Dim sB_Name As String = sCutName.Substring(0, sCutName.IndexOf("\"))
	sCutName = sCutName.Remove(0, sCutName.IndexOf("\") +1)
	Dim sC_Name As String = sCutName.Substring(0, sCutName.IndexOf("\"))
	sCutName = sCutName.Remove(0, sCutName.IndexOf("\") +1)
	Dim sD_Name As String = sCutName.Substring(0, sCutName.IndexOf("\"))
	sCutName = sCutName.Remove(0, sCutName.IndexOf("\") +1)
	Dim sE_Name As String = sCutName.Substring(0, sCutName.IndexOf("\"))
	Dim sF_Name As String = sCutName.Remove(0, sCutName.IndexOf("\") + 1)
	Dim sFullDocName As String
	If TypeOf oDoc Is AssemblyDocument Then
		sFullDocName = sA_Name & sB_Name & "-" & sC_Name & "-" & sD_Name & "-" & sE_Name & "-" & sF_Name & ".iam"
	Else If TypeOf oDoc Is PartDocument Then
		sFullDocName = DateTime.Now.Ticks & ".ipt"
	End If
	Dim sFullName As String = sPath & "\" & sFullDocName
	oDoc.SaveAs(sFullName, False)
End Sub

 

Andrii Humeniuk - CAD Coordinator, Autodesk Certified Instructor

LinkedIn | My free Inventor Addin | My Repositories

Did you find this reply helpful ? If so please use the Accept as Solution/Like.

EESignature

0 Likes
Message 7 of 15

Andrii_Humeniuk
Advisor
Advisor

In this variant, the current waves and seconds when saving are taken as a unique number. But there is a possibility that there will be matches in the file names.

 

Sub main
	oDoc = ThisDoc.Document
	Dim oFileDlg As Inventor.FileDialog = Nothing
	InventorVb.Application.CreateFileDialog(oFileDlg)
	oFileDlg.Filter = "Inventor Files (*.iam;*.ipt)|*.iam;*.ipt|All Files (*.*)|*.*"
	oFileDlg.FileName = "Select the file storage path"
	oFileDlg.ShowSave()
	MyFile = oFileDlg.FileName
	If MyFile Is Nothing Or MyFile = "" Then Exit Sub	
	Dim sPath As String = System.IO.Path.GetDirectoryName(oFileDlg.FileName)
	Dim sCutName As String = sPath.Remove(0, sPath.IndexOf("Projects\") + 9)
	Dim sA_Name As String = sCutName.Substring(0, sCutName.IndexOf("\"))
	sCutName = sCutName.Remove(0, sCutName.IndexOf("\") +1)
	Dim sB_Name As String = sCutName.Substring(0, sCutName.IndexOf("\"))
	sCutName = sCutName.Remove(0, sCutName.IndexOf("\") +1)
	Dim sC_Name As String = sCutName.Substring(0, sCutName.IndexOf("\"))
	sCutName = sCutName.Remove(0, sCutName.IndexOf("\") +1)
	Dim sD_Name As String = sCutName.Substring(0, sCutName.IndexOf("\"))
	sCutName = sCutName.Remove(0, sCutName.IndexOf("\") +1)
	Dim sE_Name As String = sCutName.Substring(0, sCutName.IndexOf("\"))
	Dim sF_Name As String = sCutName.Remove(0, sCutName.IndexOf("\") + 1)
	Dim sShortName As String = sA_Name & sB_Name & "-" & sC_Name & "-" & sD_Name & "-" & sE_Name & "-" & sF_Name
	Dim sFullDocName As String
	If TypeOf oDoc Is AssemblyDocument Then
		sFullDocName = sShortName & ".iam"
	Else If TypeOf oDoc Is PartDocument Then
		sFullDocName = sShortName & "_" & DateTime.Now.Minute & DateTime.Now.Millisecond & ".ipt"
	End If
	Dim sFullName As String = sPath & "\" & sFullDocName
	oDoc.SaveAs(sFullName, False)
End Sub

 

 

Andrii Humeniuk - CAD Coordinator, Autodesk Certified Instructor

LinkedIn | My free Inventor Addin | My Repositories

Did you find this reply helpful ? If so please use the Accept as Solution/Like.

EESignature

0 Likes
Message 8 of 15

Andrii_Humeniuk
Advisor
Advisor
Accepted solution

This code checks whether the part exists in the folder and numbers them in order.

Sub main
	oDoc = ThisDoc.Document
	Dim oFileDlg As Inventor.FileDialog = Nothing
	InventorVb.Application.CreateFileDialog(oFileDlg)
	oFileDlg.Filter = "Inventor Files (*.iam;*.ipt)|*.iam;*.ipt|All Files (*.*)|*.*"
	oFileDlg.FileName = "Select the file storage path"
	oFileDlg.ShowSave()
	MyFile = oFileDlg.FileName
	If MyFile Is Nothing Or MyFile = "" Then Exit Sub	
	Dim sPath As String = System.IO.Path.GetDirectoryName(oFileDlg.FileName)
	Dim sCutName As String = sPath.Remove(0, sPath.IndexOf("Projects\") + 9)
	Dim sA_Name As String = sCutName.Substring(0, sCutName.IndexOf("\"))
	sCutName = sCutName.Remove(0, sCutName.IndexOf("\") +1)
	Dim sB_Name As String = sCutName.Substring(0, sCutName.IndexOf("\"))
	sCutName = sCutName.Remove(0, sCutName.IndexOf("\") +1)
	Dim sC_Name As String = sCutName.Substring(0, sCutName.IndexOf("\"))
	sCutName = sCutName.Remove(0, sCutName.IndexOf("\") +1)
	Dim sD_Name As String = sCutName.Substring(0, sCutName.IndexOf("\"))
	sCutName = sCutName.Remove(0, sCutName.IndexOf("\") +1)
	Dim sE_Name As String = sCutName.Substring(0, sCutName.IndexOf("\"))
	Dim sF_Name As String = sCutName.Remove(0, sCutName.IndexOf("\") + 1)
	Dim sShortName As String = sA_Name & sB_Name & "-" & sC_Name & "-" & sD_Name & "-" & sE_Name & "-" & sF_Name
	Dim sFullDocName As String
	If TypeOf oDoc Is AssemblyDocument Then
		sFullDocName = sShortName & ".iam"
	Else If TypeOf oDoc Is PartDocument Then
		Dim lUnqNumb As String = GetUnqNumb(sPath & "\" & sShortName)
		If lUnqNumb Is Nothing Or lUnqNumb = "" Then Exit Sub
		sFullDocName = sShortName & "_" & lUnqNumb & ".ipt"
	End If
	Dim sFullName As String = sPath & "\" & sFullDocName
	oDoc.SaveAs(sFullName, False)
End Sub

Private Function GetUnqNumb(sShortName As String) As String
	Dim sInt As String
	For i As Integer = 1 To 99999
		sInt = i
		If InStrRev(sInt, "", -1) = 1 Then
			sInt = "0000" & sInt
		Else If InStrRev(sInt, "", -1) = 2 Then
			sInt = "000" & sInt
		Else If InStrRev(sInt, "", -1) = 3 Then
			sInt = "00" & sInt
		Else If InStrRev(sInt, "", -1) = 4 Then
			sInt = "0" & sInt
		Else If InStrRev(sInt, "", -1) = 5 Then
			sInt = sInt
		End If
		If Not System.IO.File.Exists(sShortName & "_" & sInt & ".ipt") Then
			Return sInt
		End If
	Next i
	Return Nothing
End Function

Снимок экрана 2023-05-04 115221.png

 

Andrii Humeniuk - CAD Coordinator, Autodesk Certified Instructor

LinkedIn | My free Inventor Addin | My Repositories

Did you find this reply helpful ? If so please use the Accept as Solution/Like.

EESignature

Message 9 of 15

zikmund
Contributor
Contributor
Andrii, you're awesome!
This is exactly what I've been looking for! It works like a charm!
Thank you very very much.

With best regards
Milan
Milan
Message 10 of 15

zikmund
Contributor
Contributor

@Andrii_Humeniuk Maybe one last request of your help.

 

Is it possible to reserve GGGG from 0001 to 0999 for sub-assemblies and 1000 - 9999 for parts?

 

Milan

0 Likes
Message 11 of 15

Andrii_Humeniuk
Advisor
Advisor
Accepted solution

Please check if this code works as you wanted.

 

Sub main
	oDoc = ThisDoc.Document
	Dim oFileDlg As Inventor.FileDialog = Nothing
	InventorVb.Application.CreateFileDialog(oFileDlg)
	oFileDlg.Filter = "Inventor Files (*.iam;*.ipt)|*.iam;*.ipt|All Files (*.*)|*.*"
	oFileDlg.FileName = "Select the file storage path"
	oFileDlg.ShowSave()
	MyFile = oFileDlg.FileName
	If MyFile Is Nothing Or MyFile = "" Then Exit Sub	
	Dim sPath As String = System.IO.Path.GetDirectoryName(oFileDlg.FileName)
	Dim sCutName As String = sPath.Remove(0, sPath.IndexOf("Projects\") + 9)
	Dim sA_Name As String = sCutName.Substring(0, sCutName.IndexOf("\"))
	sCutName = sCutName.Remove(0, sCutName.IndexOf("\") +1)
	Dim sB_Name As String = sCutName.Substring(0, sCutName.IndexOf("\"))
	sCutName = sCutName.Remove(0, sCutName.IndexOf("\") +1)
	Dim sC_Name As String = sCutName.Substring(0, sCutName.IndexOf("\"))
	sCutName = sCutName.Remove(0, sCutName.IndexOf("\") +1)
	Dim sD_Name As String = sCutName.Substring(0, sCutName.IndexOf("\"))
	sCutName = sCutName.Remove(0, sCutName.IndexOf("\") +1)
	Dim sE_Name As String = sCutName.Substring(0, sCutName.IndexOf("\"))
	Dim sF_Name As String = sCutName.Remove(0, sCutName.IndexOf("\") + 1)
	Dim sShortName As String = sA_Name & sB_Name & "-" & sC_Name & "-" & sD_Name & "-" & sE_Name & "-" & sF_Name
	Dim sFullDocName As String
	Dim lUnqNumb As String
	If TypeOf oDoc Is AssemblyDocument Then
		If Not System.IO.File.Exists(sPath & "\" & sShortName & ".iam") Then
			sFullDocName = sShortName & ".iam"			
		Else
			lUnqNumb = GetUnqNumbAssemb(sPath & "\" & sShortName)
			sFullDocName = sShortName & "_" & lUnqNumb & ".iam"
		End If
	Else If TypeOf oDoc Is PartDocument Then
		lUnqNumb = GetUnqNumbPart(sPath & "\" & sShortName)
		If lUnqNumb Is Nothing Or lUnqNumb = "" Then Exit Sub
		sFullDocName = sShortName & "_" & lUnqNumb & ".ipt"
	End If
	Dim sFullName As String = sPath & "\" & sFullDocName
	oDoc.SaveAs(sFullName, False)
End Sub

Private Function GetUnqNumbAssemb(sShortName As String) As String
	Dim sInt As String
	For i As Integer = 1 To 999
		sInt = i
		If InStrRev(sInt, "", -1) = 1 Then
			sInt = "000" & sInt
		Else If InStrRev(sInt, "", -1) = 2 Then
			sInt = "00" & sInt
		Else If InStrRev(sInt, "", -1) = 3 Then
			sInt = "0" & sInt
		End If
		If Not System.IO.File.Exists(sShortName & "_" & sInt & ".iam") Then
			Return sInt
		End If
	Next i
	Return Nothing
End Function

Private Function GetUnqNumbPart(sShortName As String) As String
	Dim sInt As String
	For i As Integer = 1000 To 9999
		sInt = i
		If InStrRev(sInt, "", -1) = 1 Then
			sInt = "000" & sInt
		Else If InStrRev(sInt, "", -1) = 2 Then
			sInt = "00" & sInt
		Else If InStrRev(sInt, "", -1) = 3 Then
			sInt = "0" & sInt
		Else If InStrRev(sInt, "", -1) = 4 Then
			sInt = sInt
		End If
		If Not System.IO.File.Exists(sShortName & "_" & sInt & ".ipt") Then
			Return sInt
		End If
	Next i
	Return Nothing
End Function

Снимок экрана 2023-05-04 130141.png

Andrii Humeniuk - CAD Coordinator, Autodesk Certified Instructor

LinkedIn | My free Inventor Addin | My Repositories

Did you find this reply helpful ? If so please use the Accept as Solution/Like.

EESignature

Message 12 of 15

zikmund
Contributor
Contributor
I love you! This is exactly what I needed.

Thank you!
Message 13 of 15

zikmund
Contributor
Contributor

Hi @Andrii_Humeniuk,

 

I tested the file creations and it works awesome. However people complains they would like to keep also the possibility to enter unique file name by user.

Is it possible to add some kind of condition that if user fills the file name manualy, it will skip the automatic naming process, but if user not fills the name, it will go through the automation?

 

Thanks again.

Milan

 

 

0 Likes
Message 14 of 15

Andrii_Humeniuk
Advisor
Advisor
Accepted solution

Hi @zikmund . Yes, it can be done. When you change the text (Select the file storage path) when saving the file, a file with your name will be created.

Sub main
	oDoc = ThisDoc.Document
	Dim oFileDlg As Inventor.FileDialog = Nothing
	InventorVb.Application.CreateFileDialog(oFileDlg)
	oFileDlg.Filter = "Inventor Files (*.iam;*.ipt)|*.iam;*.ipt|All Files (*.*)|*.*"
	oFileDlg.FileName = "Select the file storage path"
	oFileDlg.ShowSave()
	MyFile = oFileDlg.FileName
	If MyFile Is Nothing Or MyFile = "" Then Exit Sub
	If Not MyFile.Contains("Select the file storage path") Then oDoc.SaveAs(MyFile, False):	Exit Sub
	Dim sPath As String = System.IO.Path.GetDirectoryName(oFileDlg.FileName)
	Dim sCutName As String = sPath.Remove(0, sPath.IndexOf("Projects\") + 9)
	Dim sA_Name As String = sCutName.Substring(0, sCutName.IndexOf("\"))
	sCutName = sCutName.Remove(0, sCutName.IndexOf("\") +1)
	Dim sB_Name As String = sCutName.Substring(0, sCutName.IndexOf("\"))
	sCutName = sCutName.Remove(0, sCutName.IndexOf("\") +1)
	Dim sC_Name As String = sCutName.Substring(0, sCutName.IndexOf("\"))
	sCutName = sCutName.Remove(0, sCutName.IndexOf("\") +1)
	Dim sD_Name As String = sCutName.Substring(0, sCutName.IndexOf("\"))
	sCutName = sCutName.Remove(0, sCutName.IndexOf("\") +1)
	Dim sE_Name As String = sCutName.Substring(0, sCutName.IndexOf("\"))
	Dim sF_Name As String = sCutName.Remove(0, sCutName.IndexOf("\") + 1)
	Dim sShortName As String = sA_Name & sB_Name & "-" & sC_Name & "-" & sD_Name & "-" & sE_Name & "-" & sF_Name
	Dim sFullDocName As String
	Dim lUnqNumb As String
	If TypeOf oDoc Is AssemblyDocument Then
		If Not System.IO.File.Exists(sPath & "\" & sShortName & ".iam") Then
			sFullDocName = sShortName & ".iam"			
		Else
			lUnqNumb = GetUnqNumbAssemb(sPath & "\" & sShortName)
			sFullDocName = sShortName & "_" & lUnqNumb & ".iam"
		End If
	Else If TypeOf oDoc Is PartDocument Then
		lUnqNumb = GetUnqNumbPart(sPath & "\" & sShortName)
		If lUnqNumb Is Nothing Or lUnqNumb = "" Then Exit Sub
		sFullDocName = sShortName & "_" & lUnqNumb & ".ipt"
	End If
	Dim sFullName As String = sPath & "\" & sFullDocName
	oDoc.SaveAs(sFullName, False)
End Sub

Private Function GetUnqNumbAssemb(sShortName As String) As String
	Dim sInt As String
	For i As Integer = 1 To 999
		sInt = i
		Select Case sInt.Length
		Case 1
			sInt = "000" & sInt
		Case 2
			sInt = "00" & sInt
		Case 3
			sInt = "0" & sInt
		End Select
		If Not System.IO.File.Exists(sShortName & "_" & sInt & ".iam") Then
			Return sInt
		End If
	Next i
	Return Nothing
End Function

Private Function GetUnqNumbPart(sShortName As String) As String
	For i As Integer = 1000 To 9999
		If Not System.IO.File.Exists(sShortName & "_" & i & ".ipt") Then
			Return i
		End If
	Next i
	Return Nothing
End Function

 

Andrii Humeniuk - CAD Coordinator, Autodesk Certified Instructor

LinkedIn | My free Inventor Addin | My Repositories

Did you find this reply helpful ? If so please use the Accept as Solution/Like.

EESignature

Message 15 of 15

zikmund
Contributor
Contributor
Hi Andrii,

Perfect. Many thanks again.

With best regards
Milan