×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

File With Largest Number In File Name - Journal
2

File With Largest Number In File Name - Journal

File With Largest Number In File Name - Journal

(OP)
I'm working on a journal that would get the largest number from file names in specified folder and display the result in a listing window. This is what I got so far:


CODE

Option Strict Off

Imports NXOpen
Imports System
Imports System.IO
Imports System.Windows.Forms

Module list_files

Sub Main()

 Dim s As Session = Session.GetSession()
 Dim lw As ListingWindow = s.ListingWindow()
 'Dim foldername As String = ""
 Dim dir As DirectoryInfo = new DirectoryInfo("D:\test")
 Dim fsi As FileSystemInfo
 Dim Filename As String
 
lw.Open()

 For Each fsi In dir.GetFileSystemInfos("*.prt")
 If (TypeOf fsi is FileInfo) Then
 Dim f As FileInfo = CType( fsi, FileInfo )	 
 Dim Intf As Integer
	 Filename  =  f.Name 
	 Intf = MakeInt(Filename)			 
	 
lw.WriteLine(Intf)
 
 End If
 Next fsi

End Sub

Public Function MakeInt(ByVal stringint As String) As String
Dim lngCount As Long
Dim strOut As String
if Len(stringint) > 0 then
  for lngCount = 1 to len(stringint)
    if isnumeric(mid$(stringint, lngCount, 1)) then
      strOut = strOut & mid$(stringint, lngCount, 1)
    end if
  next lngCount
end if
MakeInt = strOut
End Function

End Module 

Any help would be appreciated.

RE: File With Largest Number In File Name - Journal

rafl,

For your consideration...

HTH, Joe


CODE --> VB

Option Strict Off

Imports NXOpen
Imports System
Imports System.IO
Imports System.Windows.Forms

Module list_files

Sub Main()

 Dim largest As String = Nothing
 Dim s As Session = Session.GetSession()
 Dim lw As ListingWindow = s.ListingWindow()
 'Dim foldername As String = ""
 Dim dir As DirectoryInfo = new DirectoryInfo("e:\")
 Dim fsi As FileSystemInfo
 Dim Filename As String
 
lw.Open()

 For Each fsi In dir.GetFileSystemInfos("*.prt")
 If (TypeOf fsi is FileInfo) Then
 Dim f As FileInfo = CType( fsi, FileInfo )	 
 Dim Intf As Integer
	 Filename  =  f.Name 
	 Intf = MakeInt(Filename)			 
	 if Intf > largest then largest = Intf
'lw.WriteLine(Intf)
 
 End If
 Next fsi
lw.WriteLine("Largest part number = " & largest)
End Sub

Public Function MakeInt(ByVal stringint As String) As String
Dim lngCount As Long
Dim strOut As String
if Len(stringint) > 0 then
  for lngCount = 1 to len(stringint)
    if isnumeric(mid$(stringint, lngCount, 1)) then
      strOut = strOut & mid$(stringint, lngCount, 1)
    end if
  next lngCount
end if
MakeInt = strOut
End Function

End Module 

RE: File With Largest Number In File Name - Journal

A few observations:
  • the function MakeInt returns a string value, I'd expect Intf = MakeInt(Filename) to throw an error (trying to assign a string value to an integer variable), if it doesn't, there's probably an implicit conversion going on in the background
  • if Intf > largest then... Intf is an integer, largest is a string; perhaps those implicit conversions are working out here too, but better to assign the values properly to know for sure
What do your part numbers look like that you are comparing? Perhaps one of the TryParse methods will make life a little easier for you...

www.nxjournaling.com

RE: File With Largest Number In File Name - Journal

Cowski,

Yes, good advice (as always).

The declaration for largest as string was (hastily) based on the MakeInt declaration (not the implicit integer conversion of Intf).

One possible solution that would be more robust is:

CODE --> VB

Dim largest As Integer = Nothing 
and

CODE --> VB

Intf = ctype(MakeInt(Filename), integer) 
and

CODE --> VB

lw.WriteLine("Largest part number = " & largest.ToString) 

However the discussion then becomes Integer vs Long since a 10 digit part number would fail. winky smile

Regarding TryParse, my quick test of the supplied MakeInt handled names with only numbers, only alphas (returned zero), and mixtures in varying locations within the mixtures and it returned all the numeric characters in the basename of the part filespec. Depending on the naming convention (and the strict adherence to the convention) this could be good enough for the original poster or it could be risky. For example, ABC12341_v1.prt, abc12_3411.prt and 1234_11.prt all return the same value of 123411.

Regards,

Joe

RE: File With Largest Number In File Name - Journal

Joe,
Your last 2 paragraphs are exactly why I asked about what the part numbers look like...

rafl,
To give any useful advice, I think we need some more background to the problem at hand. I understand you want to look in a specific folder and return the "largest number in a file name"; but knowing nothing of your file names or file system, makes it difficult. A description of the root problem you are trying to solve would also be helpful. I don't see how getting the largest number in a file name is even useful; are you looking for the latest revision of a file? or perhaps the most recently assigned file? or...?

www.nxjournaling.com

RE: File With Largest Number In File Name - Journal

(OP)
Thanks for the replies. They were really helpful. As you can see I'm new to VB.net. I tried to create an array of integer and then find the largest number in that array, but I didn't succeed. The code I posted is a modified VB program that i found on GTAC Solution Center.
Files that I'm working with are cam machining files. The filename is basically just "project_name-file_number". I will use this code in journal that creates new file with new name (which is "project_name-largest number+1"). Joe's code works great. "File_number" is a six-digit number, so I'm not getting any error. MakeInt function works as expected, it gets rid off the project name and the file extension.

RE: File With Largest Number In File Name - Journal

So you have what you need, or are there other issues to work out?

www.nxjournaling.com

RE: File With Largest Number In File Name - Journal

(OP)
Yes, thats everything. Thank You for your help

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources