Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Already Existing file - Create file with increment (1)

Status
Not open for further replies.

mnash60

Materials
Feb 21, 2012
29
0
0
US
I have a journal file creates an image with a unique file name. I what I want to do is when the journal file is ran for the 2nd time I need the file name to create a (1) and increments (2) (3) and so one. Below is the code I have been working with if i could get some help, I would greatly appreciate it.

Thanks in advance!

Option Strict Off
Imports System
Imports System.IO
Imports NXOpen
Imports NXOpen.UF

Module Module1
Sub Main()

Dim theSession As Session = Session.GetSession()
Dim ufs As UFSession = UFSession.GetUFSession()
Dim workPart As Part = theSession.Parts.Work


'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
'Get parent directory with IO commands
Dim directoryInfo As System.IO.DirectoryInfo
directoryInfo = System.IO.Directory.GetParent(IO.Path.GetDirectoryName(workPart.FullPath))
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Dim strCurrentDate As String = Format(Today, "MMddy")
Dim tempSelectedObjects() As NXObject
Dim step214File As String
Dim Filenumber as Integer
'Dim outputFile3 As String

Dim outputFile2 As String
Dim outputPath As String = IO.Path.GetDirectoryName(workPart.FullPath)
Dim outputFile As String = IO.Path.GetFileNameWithoutExtension(workPart.FullPath)
outputFile = mid(outputfile, 1, 11)
outputFile2 = IO.Path.Combine(outputPath, outputFile & "_ANT_” & strCurrentDate)

If File.Exists(outputFile2) Then

filenumber = 0
Do
fileNumber += 1

Loop While File.Exists(outputFile2)

outputFile2 = IO.Path.Combine(outputPath, outputFile & “_ANT_” & fileNumber, strCurrentDate)

End If



Dim strPartJpg As String = ""

strPartJpg =outputFile2
ufs.Disp.CreateImage(strPartJpg, UFDisp.ImageFormat.Jpeg, UFDisp.BackgroundColor.White)

End Sub

Public Function GetUnloadOption(ByVal dummy As String) As Integer

'Unloads the image when the NX session terminates
GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination

End Function

End Module




 
Replies continue below

Recommended for you

Check to see if the file already exists before the export. If it does, add an increment to the file name and test again. Repeat until you find a file name that doesn't already exist.

Here's some pseudocode:

Code:
'generate base file name
myFile = "C:\temp\somefile"

dim i as integer = 0
do
  i += 1
  myFile & i
loop until NOT myFile.exists

www.nxjournaling.com
 
Status
Not open for further replies.
Back
Top