Search folder for file using the user input, then create copy of file

I am trying to create a simplistic program that will search for an image using the users input, in a specific folder, if the image exists, then creates a copy of that image to another folder. The user may not have the whole name, for example, the image name could be iStock-454565767, The user would be able to just input the numbers without the istock.

We use thousands of istock photos that have been purchased and are housing them in one file, which makes searching for specific images difficult, especially when we may need several at one time.

I have this code:

Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
    Dim FSO, sourceFolder, currentFile, filesInSourceFolder
    Dim strSourceFolderPath As String
    Dim strDestinationFolderPath As String
    Dim strUserInput As String
    FSO = CreateObject("Scripting.FileSystemObject")

    strUserInput = txtSearch.Text
    strSourceFolderPath = "C:UsersD41071023DesktopPhoto Database"
    strDestinationFolderPath = "C:UsersD41071023Desktoptest"

    sourceFolder = FSO.GetFolder(strSourceFolderPath)
    filesInSourceFolder = sourceFolder.Files

    For Each currentFile In filesInSourceFolder
        If currentFile.Name = strUserInput Then
            currentFile.Copy(FSO.BuildPath(strDestinationFolderPath,
            currentFile.Name))
        End If
    Next
End Sub

When I click “Search” nothing happens. No file is created, there’s not even an error. Also, I want to add a message box that pops up when the file has been created, or a message that lets the user know that the image was not found.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Upto my understanding,

Dim sourceFolderPath = "source folder path here" ' Folderpath without spaces
Dim fileNumber = "file number here"

Dim sourceFilePath = Directory.GetFiles(sourceFolderPath, $"iStock-{fileNumber}.png").FirstOrDefault()

If Not IsNothing(sourceFilePath) Then
    Dim destinationFolderPath = "destination folder path here" 'Folderpath without spaces
    Dim sourceFileName = Path.GetFileName(sourceFilePath)
    Dim destinationFilePath = Path.Combine(destinationFolderPath, sourceFileName)

    File.Copy(sourceFilePath, destinationFilePath)
    MsgBox("File " & "iStock-{fileNumber}.png" & " found.", MsgBoxStyle.OkOnly)
else 
    MsgBox("File not found  .  .  .  ", MsgBoxStyle.OkOnly + MsgBoxStyle.Information)
End If

Method 2

If I’m understanding you correctly, this is the sort of thing that you should have:

Dim sourceFolderPath = "source folder path here"
Dim fileNumber = "file number here"

Dim sourceFilePath = Directory.GetFiles(sourceFolderPath, $"iStock-{fileNumber}.png").SingleOrDefault()

If sourceFilePath IsNot Nothing Then
    Dim destinationFolderPath = "destination folder path here"
    Dim sourceFileName = Path.GetFileName(sourceFilePath)
    Dim destinationFilePath = Path.Combine(destinationFolderPath, sourceFileName)

    File.Copy(sourceFilePath, destinationFilePath)
End If


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x