Solved

VBScript that sees if a file exists in a folder?

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…

929 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

14 Experts available now in Live!

Get 1:1 Help Now