Solved

VBScript that sees if a file exists in a folder?

Posted on 2004-10-13
2
6,424 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 86

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to measure sizes and angles in scanned images ? 3 100
Excel Automation VBA 19 116
Get the Y value of a line at X in a chart in VBA 28 74
Modify Text File with Excel Macro 13 51
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

752 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