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!

File With Largest Number In File Name - Journal 2

Status
Not open for further replies.

rafl

Mechanical
May 30, 2011
41
0
0
PL
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.
 
Replies continue below

Recommended for you

rafl,

For your consideration...

HTH, Joe


Code:
Option Strict Off

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

Module list_files

Sub Main()

 [highlight #4E9A06]Dim largest As String = Nothing[/highlight]
 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)			 
[highlight #4E9A06]	 if Intf > largest then largest = Intf[/highlight]
[highlight #4E9A06]'[/highlight]lw.WriteLine(Intf)
 
 End If
 Next fsi
[highlight #4E9A06]lw.WriteLine("Largest part number = " & largest)[/highlight]
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
 
A few observations:
[ul]
[li]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[/li]
[li]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[/li]
[/ul]

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
 
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:
Dim largest As Integer = Nothing
and
Code:
Intf = ctype(MakeInt(Filename), integer)
and
Code:
lw.WriteLine("Largest part number = " & largest.ToString)

However the discussion then becomes Integer vs Long since a 10 digit part number would fail. ;-)

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
 
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
 
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.
 
Status
Not open for further replies.
Back
Top