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:
Any help would be appreciated.
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
For your consideration...
HTH, Joe
CODE --> VB
RE: File With Largest Number In File Name - Journal
- 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
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
CODE --> VB
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.
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
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
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
www.nxjournaling.com
RE: File With Largest Number In File Name - Journal