saving out duplicate sub-assemblies

saving out duplicate sub-assemblies

D.Wheeler3GADA
Advocate Advocate
354 Views
3 Replies
Message 1 of 4

saving out duplicate sub-assemblies

D.Wheeler3GADA
Advocate
Advocate

I am trying to write code so when an assembly is being saved out if it sees an existing file of the same name in the directory, it just adds an 01, 02, 03 ...etc. to 99. Is there an easy way to code this?

 

Thanks!

0 Likes
Accepted solutions (2)
355 Views
3 Replies
Replies (3)
Message 2 of 4

Michael.Navara
Advisor
Advisor
Accepted solution

Something like this?

 

 

Dim fullFileName = ThisDoc.Document.FullFileName()
Dim dirName = IO.Path.GetDirectoryName(fullFileName)
Dim fileName = IO.Path.GetFileName(fullFileName)
Dim ext = IO.Path.GetExtension(fullFileName)
Dim fileNamePrefix = fileName.Split("-")(0)

Dim newFullFilename As String
For i = 1 To 99
	newFullFilename = String.Format("{0}\{1}-{2:00}{3}", dirName, fileNamePrefix, i, ext)
	If Not IO.File.Exists(newFullFilename) Then Exit For
Next

ThisDoc.Document.SaveAs(newFullFilename, False)

 

Message 3 of 4

WCrihfield
Mentor
Mentor
Accepted solution

Hi @D.Wheeler3GADA.  This is just another similar example of some fairly simple iLogic code for adding a 2 digit, incrementing Integer suffix to the end of a proposed file name for the SaveAs method.  I put that code into its own custom Sub routine 'SaveAsWithSuffix', because it's fairly popular, and will work with whichever document you supply it.

 

Sub Main
	'specify the document to SaveAs
	oDoc = ThisDoc.Document
	'specify proposed new full file name
	oFullFileName = "C:\Temp\My Assembly.iam"
	'use the custom Sub routine to ensure it succeeds
	SaveAsWithSuffix(oDoc, oFullFileName)
End Sub

Sub SaveAsWithSuffix(oDoc As Document, oNewFullName As String)
	If Not System.IO.File.Exists(oNewFullName) Then
		Try
			oDoc.SaveAs(oNewFullName, False)
		Catch oEx As Exception
			Logger.Error("SaveAs Failed!" & vbCrLf & oEx.Message & vbCrLf & oEx.StackTrace)
		End Try
	Else
		oPathAndName = System.IO.Path.ChangeExtension(oNewFullName, vbNullString)
		oExt = System.IO.Path.GetExtension(oNewFullName)
		Dim oAltNewFullName As String = ""
		For i As Integer = 1 To 99
			oSuffix = i.ToString("D2")
			oAltNewFullName = oPathAndName & oSuffix & oExt
			If System.IO.File.Exists(oAltNewFullName) Then Continue For
			Try
				oDoc.SaveAs(oAltNewFullName, False)
			Catch oEx As Exception
				Logger.Error("SaveAs Failed!" & vbCrLf & oEx.Message & vbCrLf & oEx.StackTrace)
			End Try
			Exit For
		Next
	End If
End Sub

 

 

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 4 of 4

D.Wheeler3GADA
Advocate
Advocate

Michael.Navara,

       Thank you for the coding. Works great!

0 Likes