Finding photos (1 Viewer)

John Sh

Member
Local time
Tomorrow, 02:25
Joined
Feb 8, 2021
Messages
408
I have a directory with 8000 plus photos that are named "Acanthus mollis G IMGP3514.jpg" with many variations between the first word and the image number, as well as the first and last bits. I am trying to build a search path using the directory location, the first and last parts taken from textboxes with a "*" in the middle.
My search string is fixeddir & txtbox1 & "*" & txtbox2 & ".jpg"giving "Acanthus*IMGP3514.jpg". If I do a windows search using that string it returns the correct photo. In access I get an error that is trapped to show a dummy photo.
Where am I going wrong?
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 23:25
Joined
May 7, 2009
Messages
19,169
does your fixeddir folder has a backslash at the end, ie:

c:\fixedFolder\

how do you "search" for the image, using Dir()?
 

jdraw

Super Moderator
Staff member
Local time
Today, 11:25
Joined
Jan 23, 2006
Messages
15,364
What is the error message?
You really do have '8000 plus photos that are named "Acanthus mollis G IMGP3514.jpg"'

What exactly identifies each record uniquely?
Please show the actual code that does the search.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 10:25
Joined
Feb 28, 2001
Messages
26,999
I am going to suggest two options to you.

First, look up and read about the FileSystemObject (FSO), which should give you duplicate abilities from the Windows Explorer in reading folders, searching for files, manipulating files, etc.


This link brings you to the top of the FSO "documentation tree" for the Microsoft online VBA reference. Starting from there and drilling down, you should be able to find all sorts of example code and will see various functions that could be useful.

If I do a windows search using that string it returns the correct photo. In access I get an error that is trapped to show a dummy photo.

If all the files are in the same folder, then the "files" collection in that folder will be searchable via the folder-object.Files collection. Might be clunky due to the need for a loop, but it should be possible.

However, you should also consider a "back-door" method in which you launch a little CMD prompt search via the SHELL command using something that looks like

Code:
DIR Acanthus*IMGP3514.jpg /B  >search-file.txt

Here is an example of the actual output from a command that used relative addressing to get this list from a wildcarded specification:

Code:
C:\Users\Richard>DIR  .\Pictures\202110*.jpg  /b
20211006_105729.jpg
20211006_105738.jpg
20211006_105752.jpg

The /B switch is "bare" meaning, only file names - no header and no trailer. If you add the "> filespec" option to the above, that redirects the output (the list of filenames) to a named file, which you could then open using the VBA OPEN verb and read the filenames via INPUT LINE. When done with it, don't forget to KILL the file so that on your next search, you won't trip over your trail of crumbs from previous searches.

Here is a link to SHELL


Use SHELL to run that DIR command. Read up on the options so you won't get all hung up on it. Also note that SHELL launches a PARALLEL process, so the file might not be instantly available. Somewhere in here might be what you need to find the file names you want.
 

isladogs

MVP / VIP
Local time
Today, 15:25
Joined
Jan 14, 2017
Messages
18,186
See if my free example app is any use for your purpose.
 

John Sh

Member
Local time
Tomorrow, 02:25
Joined
Feb 8, 2021
Messages
408
What is the error message?
You really do have '8000 plus photos that are named "Acanthus mollis G IMGP3514.jpg"'

What exactly identifies each record uniquely?
Please show the actual code that does the search.
NO. They all have different names and the structure of the names varies, vis "Abutilon oxycarpon var oxycarpum DSCF3950.jpg"
 

John Sh

Member
Local time
Tomorrow, 02:25
Joined
Feb 8, 2021
Messages
408
does your fixeddir folder has a backslash at the end, ie:

c:\fixedFolder\

how do you "search" for the image, using Dir()?
A bit more info.
The data files contain the records for a herbarium and the records for a number of outside providers so I am dealing with multiple ways of storing information. Short of renaming all of this particular batch of files, and doing it again when a new batch comes in from this provider, I would like to be able to use the info that changes in structure through a "*".
TB1 has genus data and is always a single word
TB2 has image data and is always a single word
In the middle is TB3 that has species data. this can be anything from one word up to four words with, or without, "." "()" and any combination of these. And that is my dilemma. A different data set always has only one word in TB3
The fixeddir is set at login. I have various fixed directories that hold different photo sets, excel files etc, these are set into global variables and are used by various forms within the overall structure.
If I strip out all but the first word of the second part of the photo name I.e. "Abutilon oxycarpon var oxycarpum DSCF3950.jpg" becomes "Abutilon oxycarpon DSCF3950.jpg" then the code below works but this means renaming 8000 plus files.

Code:
Private Sub Form_Current()
    Dim sdir as string
    Dim target As String
    Dim strPhoto As String
    Dim strGenus As String
    Dim strEpithet As String
    Dim isBlank As Integer
    isBlank = InStr(Me.TxtSpec, " ")
    If isBlank = 0 Then
        strEpithet = Me.TxtSpec
    Else
        strEpithet = Left(Me.TxtSpec, isBlank - 1)
    End If
    strGenus = Trim(Me.cboGenus)
    strPhoto = Me.txtPhotoNum
    sDir = strGenus & " " & strEpithet & " " & strPhoto & ".jpg"
    sDir = pathCol & sDir
    Me.imgPhoto.Picture = sDir
End Sub

The fixedir paths are set as follows

Code:
Public Sub setPathways()
    pathA = "\The University of Newcastle\Herbarium - Documents\Share Drive Storage\HERBARIUM DATABASE\"
    pathB = "\The University of Newcastle\Herbarium - Documents\Share Drive Storage\backup\Backup_" & strLogin & "_"
    pathCol = pathA & "Collier Collection\"
    pathLyn = "\The University of Newcastle\Herbarium - Documents\Share Drive Storage\LYNDA"
    pathJpg = pathA & "DMHN COLLECTION DIGITAL SPECIMEN IMAGES\"
    If Dir("I:\herbarium\back-up", vbDirectory) <> "" Then
        pathA = "n:\"
        pathB = "i:\herbarium\back-up\Main collection backup_" & strLogin & "_"
        pathCol = pathA & "Barry Collier\Collier Collection\"
        pathLyn = pathA & "LYNDA"
        pathJpg = pathA & "Main Collection\All jpgs\"
    ElseIf Dir("D:\main collection", vbDirectory) <> "" Then
        pathCol = "d:\Barry Collier\Collier Collection\"
    End If
End Sub
 

John Sh

Member
Local time
Tomorrow, 02:25
Joined
Feb 8, 2021
Messages
408
I am going to suggest two options to you.

First, look up and read about the FileSystemObject (FSO), which should give you duplicate abilities from the Windows Explorer in reading folders, searching for files, manipulating files, etc.


This link brings you to the top of the FSO "documentation tree" for the Microsoft online VBA reference. Starting from there and drilling down, you should be able to find all sorts of example code and will see various functions that could be useful.



If all the files are in the same folder, then the "files" collection in that folder will be searchable via the folder-object.Files collection. Might be clunky due to the need for a loop, but it should be possible.

However, you should also consider a "back-door" method in which you launch a little CMD prompt search via the SHELL command using something that looks like

Code:
DIR Acanthus*IMGP3514.jpg /B  >search-file.txt

Here is an example of the actual output from a command that used relative addressing to get this list from a wildcarded specification:

Code:
C:\Users\Richard>DIR  .\Pictures\202110*.jpg  /b
20211006_105729.jpg
20211006_105738.jpg
20211006_105752.jpg

The /B switch is "bare" meaning, only file names - no header and no trailer. If you add the "> filespec" option to the above, that redirects the output (the list of filenames) to a named file, which you could then open using the VBA OPEN verb and read the filenames via INPUT LINE. When done with it, don't forget to KILL the file so that on your next search, you won't trip over your trail of crumbs from previous searches.

Here is a link to SHELL


Use SHELL to run that DIR command. Read up on the options so you won't get all hung up on it. Also note that SHELL launches a PARALLEL process, so the file might not be instantly available. Somewhere in here might be what you need to find the file names you want.
Thank you. I'll have a look at this.
I do use FSO in other areas but this requires the user to browse and select a file. In this instance the appropriate image is automatically displayed in an imagebox. See the code in my reply to ArnelGP.
 
Last edited:

John Sh

Member
Local time
Tomorrow, 02:25
Joined
Feb 8, 2021
Messages
408
See if my free example app is any use for your purpose.
Thank you. I will try this.
P.s. I've been looking at your auto resize code and get an error at the two "navigationpane/maximise/minimise" lines. I left a comment after the video.
John
 

isladogs

MVP / VIP
Local time
Today, 15:25
Joined
Jan 14, 2017
Messages
18,186
Hi John
I've just updated the web page to version 3.5 to fix a bug that I'd overlooked.
I thought I had replied to your comment on the automatic form resizing video on YouTube a few days ago but it was missing. I've just replied now

Any questions, get back to me
 

John Sh

Member
Local time
Tomorrow, 02:25
Joined
Feb 8, 2021
Messages
408
Thank you all for the suggestions so far.
I've got to this, using FSO!
In the code below there are three sDir assignments. If I comment out the second the code works.
If I comment out the third, using the code with the "*" ,I get error 53, "file not found" error.
So I still have the problem with "*" not being handled correctly.
Does this come down to renaming 8000 plus files?

Code:
Public Function getImage()
    Dim Source As Object
    Dim Target As String
    Dim retval As Integer
    Dim frm As Form
    Dim sDir As String
    Dim strPhoto As String
    Dim strGenus As String
    Dim strEpithet As String
    Dim isBlank As Integer
    Set frm = Screen.ActiveForm
    On Error GoTo error
    isBlank = InStr(frm.txtSpec, " ")
    If isBlank = 0 Then
        strEpithet = frm.txtSpec
    Else
        strEpithet = left(frm.txtSpec, isblank - 1)
    End If
    strGenus = Trim(frm.cboGenus)
    strPhoto = frm.txtPhotoNum
    retval = 0
    Dim objFSO As Object
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    sDir = strGenus & "*" & strPhoto & ".jpg"  
    sDir = pathCol & sDir                      'This line returns an error
    sDir = pathCol & "NoImage.jpg"    'This line works
    Set Source = objFSO.GetFile(sDir)
    frm.imgPhoto.Picture = Source
    Set objFSO = Nothing
    Exit Function
error:
    MsgBox Err.Number & Err.Description
    frm.imgPhoto.Picture = pathCol & "NoImage.jpg"
End Function
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 10:25
Joined
Feb 28, 2001
Messages
26,999
My other suggestion of using DIR, though a bit clunkier, would handle wildcarding better perhaps. I don't know how to select a named member of a collection via a wildcarded name, which is what you would need.
 

John Sh

Member
Local time
Tomorrow, 02:25
Joined
Feb 8, 2021
Messages
408
My other suggestion of using DIR, though a bit clunkier, would handle wildcarding better perhaps. I don't know how to select a named member of a collection via a wildcarded name, which is what you would need.
running the command prompt it seems that dir has a problem with numbers and spaces.
if I run dir Acacia* it returns all of the files stating with Acacia
if I run dir Acacia*IMGP3514.jpg it returns "file not found". with or without the .jpg
if I run dir Acacia * it returns every file in the directory. This is a documented behaviour of the "*" wildcard.
if I run dir *cia* it returns every file with "cia" in it somewhere
It looks like the best answer might be to create a table with all the photo names in it and do a recordset findfirst to get the full file name and pick it with that name.

Watch this space!
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 10:25
Joined
Feb 28, 2001
Messages
26,999
Try a hybrid. Use the SHELL of the DIR to make a file of the raw file names. Then IMPORT the file names to a table. Then do your findfirst.

As to "having a problem with numbers and spaces" you need to enclose the name in quotes if it is going to have embedded spaces. By numbers, do you mean that some of your embedded numbers have leading spaces in the middle of the name with a variable number of spaces? Don't take it personally, but that is a very short-sighted design. Leading zeros would have completely obviated that problem.
 

John Sh

Member
Local time
Tomorrow, 02:25
Joined
Feb 8, 2021
Messages
408
Try a hybrid. Use the SHELL of the DIR to make a file of the raw file names. Then IMPORT the file names to a table. Then do your findfirst.

As to "having a problem with numbers and spaces" you need to enclose the name in quotes if it is going to have embedded spaces. By numbers, do you mean that some of your embedded numbers have leading spaces in the middle of the name with a variable number of spaces? Don't take it personally, but that is a very short-sighted design. Leading zeros would have completely obviated that problem.
Thanks again for your continued interest.
I made a text file using cmd and dir, cleaned it up in excel then imported into access. When I do a findfirst in the recordset it only ever comes up with the first record if there is a "*" in the search string. I am now comparing file names with the data table and finding more errors than you could poke a stick at. This, in itself, explains why I was having so much trouble getting the appropriate photo to appear next to the data.
I now have day's worth of work to correct the typo's, not my work to start with, and fill in the gaps where images and data do not correspond.
Once this is done I will be able to call the image, using the info in the table, with a high degree of certainty.
Many thanks to all who have responded to my call for help.
John

<Don't take it personally, but that is a very short-sighted design. Leading zeros would have completely obviated that problem.>
At my age, 83, I don't take much of anything personally.
The numbers involved are camera file numbers and have no spaces. They are generally of the form XXXX9999. jpg.
Including numbers such as this ended up in a "File not Found" response when using the dir command.
John
 

John Sh

Member
Local time
Tomorrow, 02:25
Joined
Feb 8, 2021
Messages
408
It turns out the trick is photolocation = fixedpath & Me.cboGenus & " " & Me.cboEpithet & "*.jpg")
with the "*" inside the inverted comma's as "*.jpg" rather than "*" & ".jpg"
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 10:25
Joined
Feb 28, 2001
Messages
26,999
If it works, it works and congratulations on getting it to work for you. However, syntactically, there is no practical difference in the result of "*.jpg" vs. "*" & ".jpg" based on VBA syntax unless there is a non-printing character in there somewhere.
 

Users who are viewing this thread

Top Bottom