incremental file names

incremental file names

michielXWZ7U
Enthusiast Enthusiast
261 Views
2 Replies
Message 1 of 3

incremental file names

michielXWZ7U
Enthusiast
Enthusiast

Hi guys, i am using some code that will save my file in the name of the following

 

"MP-001-plaat " & length & "X" & width & "X" & Thickness & "mm"

 In the line the ''001'' inside ''MP-001-plaat'' represents the part number in the folder of the project. Inside the folder i arrange all the parts incremental. So every part starts with ''MP-00n' with n+1 for every new part.

 

Anyone knows how i can adjust the code that every new created parts checks the numbers in the folder location, and adds the next number to the next saved part.

 

The full code that i am using is:

 

Dim oDoc As Inventor.Document = ThisApplication.ActiveDocument
Dim FName As String = oDoc.FullFileName
Dim FNP As Integer = InStrRev(FName, "\", -1)
Dim oPath As String = Left(FName, FNP)
Dim oNewName As String = "MP-001-plaat " & lengte & "X" & breedte & "X" & Thickness & "mm"

' If the user closes the input box, then stop the function
'If oNewName = vbNullString Then Exit Sub

Dim oType As String
Select Case oDoc.DocumentType
Case kPartDocumentObject:     oType = ".ipt"
Case kAssemblyDocumentObject: oType = ".iam"
Case kDrawingDocumentObject:  oType = ".idw"
End Select

oDoc.SaveAs(oPath & oNewName & oType, True)

 With kind regards, michiel

0 Likes
262 Views
2 Replies
Replies (2)
Message 2 of 3

vpeuvion
Advocate
Advocate

Hi,

Here is a piece of code that displays all the filenames present in the folder. It remains to find the largest number and thus give the next number to your part. I hope this can help you move forward.

Vincent.

Dim fileEntries As String() = System.IO.Directory.GetFiles(oPath)
For Each fileName As String In fileEntries
MessageBox.Show(fileName)
Next
0 Likes
Message 3 of 3

WCrihfield
Mentor
Mentor

Hi @michielXWZ7U.  I don't know where/how your "lengte", "breedte", or "Thickness" terms are being defined in your code, but I assumed that they may be either the names of local parameters or simply defined above this block of code somewhere.  Here is something you can try for your naming scenario.  It makes use of the same GetFiles method mentioned in @vpeuvion post, but also follows through with the rest of the code that I assume you are looking for.  See if this works out for your needs.  There is a lot of stuff in there, so if you have any questions, feel free to ask them.

 

Dim oDoc As Inventor.Document = ThisApplication.ActiveDocument
oPath = System.IO.Path.GetDirectoryName(oDoc.FullFileName)
oDirS = System.IO.Path.DirectorySeparatorChar
oExt = System.IO.Path.GetExtension(oDoc.FullFileName)
Dim oPNTemp As String = ""
Dim oPN As String = ""
oFileNames = System.IO.Directory.GetFiles(oPath, "*" & oExt, System.IO.SearchOption.TopDirectoryOnly )
For Each oFileName In oFileNames
	oName = System.IO.Path.GetFileNameWithoutExtension(oFileName)
	If oName.StartsWith("MP-") And oName.Contains("-plaat ") Then
		oPNTemp = oName.Split("-")(1)
		If String.IsNullOrEmpty(oPN) Then oPN = oPNTemp
		If CInt(oPNTemp) > CInt(oPN) Then oPN = oPNTemp
	End If
Next
oPN = Strings.Format(CStr(CInt(oPN) + 1), "000")
Dim oNewName As String = "MP-" & oPN & "-plaat " & lengte & "X" & breedte & "X" & Thickness & "mm"
oDoc.SaveAs(oPath & oDirS & oNewName & oExt, True)

 

If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click (LIKE or KUDOS) 👍.

If you want and have time, I would appreciate your Vote(s) for My IDEAS 💡 or you can Explore My CONTRIBUTIONS

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes