Solved

VBScript that sees if a file exists in a folder?

Posted on 2004-10-13
2
6,357 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Excel object stays open 19 78
VB error "Type mismatch" 2 59
VBA open file from excel cell 4 44
VBA to copy paste columns form one file to other 20 90
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

809 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