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:
- User enters input.
- The code needs to search for the pictures in the folder based on the input.
- 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
精彩评论