开发者

Search file in folder based on input from user, copy that file into a new folder

I have a folder that contains a lot of pictures. I need to copy the pi开发者_Python百科ctures in that folder based on the input by the user and copy it into a new folder:

  1. User enters input.
  2. The code needs to search for the pictures in the folder based on the input.
  3. If found, the pictures is than move to a new folder/another folder.

How do I do this?


This is an example of how to do it. I don't know what your "user input" is, so I just made an assumption. Rectify as appropriate.

Sub CopySomeFiles()
    Dim FSO, sourceFolder, currentFile, filesInSourceFolder
    Dim strSourceFolderPath As String
    Dim strDestinationFolderPath As String
    Dim strUserInput As String
    Set FSO = CreateObject("Scripting.FileSystemObject")

    ' Figure out which file to copy from where to where
    strUserInput = InputBox("Please enter name of file to copy.")
    strSourceFolderPath = "C:\MySourceFolder"
    strDestinationFolderPath = "C:\MyDestinationFolder"

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

    ' Look at all files in source folder. If name matches,
    ' copy to destination folder.
    For Each currentFile In filesInSourceFolder
        If currentFile.Name = strUserInput Then
            currentFile.Copy (FSO.BuildPath(strDestinationFolderPath, _
                currentFile.Name))
        End If
    Next

End Sub
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜