Solved

VBScript that sees if a file exists in a folder?

Posted on 2004-10-13
2
6,237 Views
Last Modified: 2008-01-09
Hello,

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

      objFSO.DeleteFile("Z:\EPIBS7AA.csv")
      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

Else

WshShell.PopUp "File Not Found in " & strFilePath

End if
      
Wscript.Quit

Any help would be greatly appreciated. Thanks
Michele
0
Comment
Question by:mmemon
2 Comments
 
LVL 76

Assisted Solution

by:GrahamSkan
GrahamSkan earned 100 total points
ID: 12299215
You can use the Dir function in VBScript
strFileName = Dir(strFilePath & \*.csv)
do until strFileName = ""
 ... move code...
    strFileName = Dir(strFilePath & \*.csv)
Loop
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 100 total points
ID: 12299360
' 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
Next
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now