VBScript that sees if a file exists in a folder?


I have put together a script that when launched will move a specific file from user machine to a server. The script works. However, I would like to add some logic that looks for any file with an extension of .csv in a folder and if found then move that file.
Currently, it is hard coded (strFilePath) to move a specific filename. Some of my code is below:

strFilePath = "C:\MyProjects\EPIBS7AA.csv"
Set objFSO = CreateObject("Scripting.FileSystemObject")

      ' Delete file if it already exists in the destination(server).

If (objFSO.FileExists("Z:\EPIBS7AA.csv")) then

      WshShell.PopUp "File deleted."
End If

If (objFSO.FileExists(strFilePath)) then
      ' Move the file to destination

Set objFileCopy = objFSO.GetFile(strFilePath)
objFileCopy.Move ("Z:\")
WSCript.Echo "Moved to " & RemotePath


WshShell.PopUp "File Not Found in " & strFilePath

End if

Any help would be greatly appreciated. Thanks
Who is Participating?
Mike TomlinsonConnect With a Mentor Middle School Assistant TeacherCommented:
' Source and Destination folders
strSourcePath = "C:\MyProjects\"
strDestinationPath = "Z:\"

' Create FileSystemOject, get reference to Source folder and collection of source files
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strSourcePath)
Set colFiles = objFolder.Files

' Process each file in Source folder
For Each objFile in colFiles
      strFileName = objFile.Name
      ext = lcase(objFSO.GetExtensionName(objfile))
      If ext = "csv" Then         
            ' Delete the file if it already exists at target
            If objFSO.FileExists(strDestinationPath & strFileName) Then
                  objFSO.DeleteFile strDestinationPath & strFileName
                  MsgBox strFileName, vbInformation, "File Deleted"
            End If

            ' Move the file to the target folder
            objFSO.MoveFile strSourcePath & strFileName, strDestinationPath & strFileName
            MsgBox strFileName, vbInformation, "File Moved"
      End If
GrahamSkanConnect With a Mentor RetiredCommented:
You can use the Dir function in VBScript
strFileName = Dir(strFilePath & \*.csv)
do until strFileName = ""
 ... move code...
    strFileName = Dir(strFilePath & \*.csv)
All Courses

From novice to tech pro — start learning today.