473,395 Members | 1,456 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How to find the latest file name with integer value in its name ?

Hello everybody
Suppose there is folder "A", Drive "C:\" with 15 pictures named like following
P6V15-1.jpg
P6V15-2.jpg
P6V15-3.jpg
P6V15-4.jpg
P6V15-5.jpg
P6V15-6.jpg
P7V25-1.jpg
P7V25-2.jpg
P7V25-3.jpg
P7V25-4.jpg
P7V25-5.jpg
P7V25-6.jpg
P8V5-1.jpg
P8V5-2.jpg
P8V5-3.jpg

In example above "P6V15-1.jpg" is the first picture for PateintID 7 on his 15th visit and "P6V15-6.jpg" is the last picture of same patient on same visit. How can I find in code that P6V15-6.jpg is the last picture? I want this because I want to name the next picture "P6V15-7.jpg" via code

I have used this code to find the number of pictures with specific visit by a patient but can not find the last part of the latest picture

Expand|Select|Wrap|Line Numbers
  1. Function FileCount(strPath As String, StrName As String) As Integer
  2. Dim objFSO As Object, objFolder As Object, objFile As Object, i As Integer 'varDate As Variant, strName As String
  3.  
  4. ' Specify the folder...
  5. Set objFSO = CreateObject("Scripting.FileSystemObject")
  6. Set objFolder = objFSO.GetFolder(strPath)
  7.  
  8. i = 0
  9. For Each objFile In objFolder.Files
  10. If objFile.Name Like StrName & "*.jpg" Then
  11. i = i + 1
  12. End If
  13. Next 'objFile
  14.  
  15. FileCount = i
  16.  
  17. Set objFSO = Nothing
  18. Set objFolder = Nothing
  19. Set objFile = Nothing
  20. End Function

Thanks in anticipation
Nov 14 '16 #1

✓ answered by jforbes

The following code will probably do what you are asking for. It's based on what you provided. It requires you to provide the Path and the first part of the File Name, and it will return the next File Name to be used:
Expand|Select|Wrap|Line Numbers
  1. Function getNextFileName(strPath As String, StrName As String) As String
  2.     Dim objFSO As Object, objFolder As Object, objFile As Object, i As Integer 'varDate As Variant, strName As String
  3.     Dim iMax As Integer
  4.  
  5.     ' Specify the folder...
  6.     Set objFSO = CreateObject("Scripting.FileSystemObject")
  7.     Set objFolder = objFSO.GetFolder(strPath)
  8.  
  9.     ' Find the current Max
  10.     For Each objFile In objFolder.Files
  11.         If objFile.Name Like StrName & "*.jpg" Then
  12.             i = Val(Mid(objFile.Name, Len(StrName) + 1, Len(objFile.Name) - (Len(StrName) + 4)))
  13.             If i > iMax Then iMax = i
  14.         End If
  15.     Next 'objFile
  16.  
  17.     ' Determine next File Name
  18.     iMax = iMax + 1
  19.     getNextFileName = StrName & iMax & ".jpg"
  20.  
  21.     Set objFSO = Nothing
  22.     Set objFolder = Nothing
  23.     Set objFile = Nothing
  24.  
  25. End Function
But I have a feeling that there may be more needed, which is what Phil seems to be addressing. The above function will require you to know the both the Patient as well as the Visit Number. If you know the Visit Number, then great, but if you are attempting to figure that out also, things will get more complicated. Either way, let us know what you come up with.

6 1074
PhilOfWalton
1,430 Expert 1GB
It's quite complicated, but here are the steps that you need to do:

1) Load the file names into an array
2) Sort the file names alphabetically (There is no guarantee that the Scripting.FileSystemObject will read the files in alphabeticical order)
3) Use the Instr function to look for the dash -
4) Save the Patient part of the file name
5) Use the Instr function to look for the folowing dot.
6) Get the numeric part of the file name (it starts 1 letter after the patient part and end 1 letter before the dot.
7) Convert that to an integer. (Note that the sort would give files in the order
Expand|Select|Wrap|Line Numbers
  1. P6V15-1.jpg
  2. P6V15-11.jpg
  3. P6V15-12.jpg
  4. P6V15-2.jpg
  5.  
8) Save the integer
9) Read the next file name from the array.
10) If the Patient part is the same as the saved patient part then If the numeric part is greater, save that interger instead.
11) If the Patient part is not the same as the saved patient part, then the last record is the maximum that you are wanting. Save the new patinet part and set the numeric part back to 0.
Then loop round again from 3)

This may be over complicated, because I suspect you could load only the files for the patient you are interested in into the array.

Phil
Nov 14 '16 #2
jforbes
1,107 Expert 1GB
The following code will probably do what you are asking for. It's based on what you provided. It requires you to provide the Path and the first part of the File Name, and it will return the next File Name to be used:
Expand|Select|Wrap|Line Numbers
  1. Function getNextFileName(strPath As String, StrName As String) As String
  2.     Dim objFSO As Object, objFolder As Object, objFile As Object, i As Integer 'varDate As Variant, strName As String
  3.     Dim iMax As Integer
  4.  
  5.     ' Specify the folder...
  6.     Set objFSO = CreateObject("Scripting.FileSystemObject")
  7.     Set objFolder = objFSO.GetFolder(strPath)
  8.  
  9.     ' Find the current Max
  10.     For Each objFile In objFolder.Files
  11.         If objFile.Name Like StrName & "*.jpg" Then
  12.             i = Val(Mid(objFile.Name, Len(StrName) + 1, Len(objFile.Name) - (Len(StrName) + 4)))
  13.             If i > iMax Then iMax = i
  14.         End If
  15.     Next 'objFile
  16.  
  17.     ' Determine next File Name
  18.     iMax = iMax + 1
  19.     getNextFileName = StrName & iMax & ".jpg"
  20.  
  21.     Set objFSO = Nothing
  22.     Set objFolder = Nothing
  23.     Set objFile = Nothing
  24.  
  25. End Function
But I have a feeling that there may be more needed, which is what Phil seems to be addressing. The above function will require you to know the both the Patient as well as the Visit Number. If you know the Visit Number, then great, but if you are attempting to figure that out also, things will get more complicated. Either way, let us know what you come up with.
Nov 14 '16 #3
PhilOfWalton
1,430 Expert 1GB
I suspect that just counting the "right files" won't work.
If files were originally 1,2,3,4 and file 3 has been deleted, counting will say there 3 files, and then try to add a duplicate file 4.

Phil
Nov 14 '16 #4
PhilOfWalton
1,430 Expert 1GB
Sorry, jforbes. Just re-read your post, and humbly withdraw my last post. That should work with the provisos you mention other possibly than I'm not sure whether you have to convert i to an integer.

Expand|Select|Wrap|Line Numbers
  1. i = CInt(Mid(objFile.Name, Len(StrName) + 1, Len(objFile.Name) - (Len(StrName) + 4)))
  2.  
Phil
Nov 14 '16 #5
ADezii
8,834 Expert 8TB
I was able to arrive at a solution using a different approach:
  1. Recursively called the Dir() Function to load all File Names into a Temporary Table.
  2. Created a simple TOTALS Query which GROUPS BY the File Name Prefixes, namely: P6V15, P7V25, etc. The 2nd Column on this Query displays the MAX Integer Value representing the Sequence Number in the File Name, such as: 6 in P7V25-6.jpg, 122 in P54V18-122.jpg, etc. The Final Field simply concatenates all three Fields to produce the actual 'Last' File Name, such as: P7V25-6.jpg.
  3. The advantage in this approach, as I see it, is that you can display all the Last File Names for your Prefixes as previously indicated.
  4. Admittedly this approach is a little unorthodox, so I'll post the Code along with a Demo if you wish, only if someone specifically requests it.
Nov 14 '16 #6
Thanks jforbes
I have changed your code a little to fulfill my requirements and its working fine.
New code is as follows
Expand|Select|Wrap|Line Numbers
  1.     Expand|Select|Wrap|Line Numbers
  •  
  •     
  •  
  • Function NextFileInt(StrPath As String, StrName As String) As Integer
  • Dim objFSO As Object, objFolder As Object, objFile As Object, i As Integer, iMax As Integer 'varDate As Variant, strName As String
  •  
  •     ' Specify the folder...
  • Set objFSO = CreateObject("Scripting.FileSystemObject")
  • Set objFolder = objFSO.GetFolder(StrPath)
  •  
  •     ' Find the current Max
  • For Each objFile In objFolder.Files
  •  If objFile.Name Like StrName & "*.*" Then
  •   i = Left$(Replace(objFile.Name, StrName, ""), InStrRev(Replace(objFile.Name, StrName, ""), ".") - 1)
  •    If i > iMax Then iMax = i
  •  End If
  • Next 'objFile
  •  
  • iMax = iMax + 1
  • NextFileInt = iMax
  •  
  • Set objFSO = Nothing
  • Set objFolder = Nothing
  • Set objFile = Nothing
  • End Function
  •  
  •     
  •  
  •  
  • Thanks a lot once again
    Nov 15 '16 #7

    Sign in to post your reply or Sign up for a free account.

    Similar topics

    1
    by: Victoria Chin | last post by:
    Could someone point me in the right direction? I've migrated three FrontPage 2002 websites to a new web server. Each web site has a Access 2000 database on the data server. I have the global.asa...
    4
    by: Amir | last post by:
    Hi, I'd like to know if it's possible to pass an image name (the IMG SRC attribute) from HREF element to a function that is activated by onClick event and creates a Web page. I have this HREF...
    1
    by: davidgordon | last post by:
    Hi, I'm using the FileOpenDialog API that everyone has told me about which is posted everywhere on this forum.....works like a dream. When I select a file I get the full path such as : ...
    4
    by: Richard Muller | last post by:
    Hi All, I wrote a HelloWorld.cs with HelloWorld class - I compiled it and it ran fine. I wanted to confirm that a class name had to match the file name, so I: - copied the .cs file to...
    0
    by: Charles | last post by:
    I receive weekly updates via a download file for an application written in VB.NET 2003. The file is delimited by "|" (single pipe) and I currenly have a format file setup to update my data in one...
    1
    by: Simon | last post by:
    Dear reader, The function: Dim refCurr as Reference refCurr.Name
    5
    by: Rafael Olarte | last post by:
    Dear friends, I appreciate any feedback on this problem. I am trying to be able to produce an output with first, middle, and last name using a const string value. Therefore, no matter what...
    3
    by: markcash | last post by:
    I have a web application where I am displaying the current inmate population for the county jail. Part of the information that I display is the mugshot. Unfortunately, they have changed the way...
    0
    by: vasikaran | last post by:
    Hi , i hava one ftp script in batch file programming , ftp script is working fine.. but my is i dont know how much files and what are files avaiulable in remote folder ,,,, if any errors or...
    185
    by: jacob navia | last post by:
    Hi We are rewriting the libc for the 64 bit version of lcc-win and we have added a new field in the FILE structure: char *FileName; fopen() will save the file name and an accessor function will...
    0
    by: Charles Arthur | last post by:
    How do i turn on java script on a villaon, callus and itel keypad mobile phone
    0
    by: ryjfgjl | last post by:
    If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
    0
    by: ryjfgjl | last post by:
    In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
    0
    by: emmanuelkatto | last post by:
    Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
    0
    BarryA
    by: BarryA | last post by:
    What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
    0
    marktang
    by: marktang | last post by:
    ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
    0
    Oralloy
    by: Oralloy | last post by:
    Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
    0
    jinu1996
    by: jinu1996 | last post by:
    In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
    0
    by: Hystou | last post by:
    Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

    By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

    To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.