View Full Version : Copying files only if they exist


Emmanuel
05-06-2009, 06:11 AM
He guys,

I have a quick question regarding a code I wrote. I need to copy some files from one server to another every day, but some times they script starts running before the files are available and that is causing the script to fail. Could someone help me troubleshoot this code to enable it to check if the files are available before it tries to copy them over?

This is the code as it is today:

dim objFSO, strSourceFolder, strDestFolder
Const OverwriteExisting = TRUE
strSourceFolder = "\\tbstpgw3\D\ftp1\*.FLG"
strDestFolder = "C:\Documents and Settings\Me\Desktop\ftp1\"
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFile strSourceFolder, strDestFolder, OverwriteExisting


Thank you in advance for your help.

WebManny

Darth Vodka
05-07-2009, 12:42 AM
i have this if you want it (or use the "dir" bit)


Public Function FileExists(strFile As String) As Boolean

If Dir(strFile) <> "" Then
FileExists = True
Else
FileExists = False
End If

End Function

Emmanuel
05-07-2009, 04:44 AM
Tried it, but for some reason it does not work. Maybe I'm not incorporating it right. This is the code I have.

dim objFSO, strSourceFolder, strDestFolder

'Constants
Const OverwriteExisting = TRUE
Set objFSO = CreateObject("Scripting.FileSystemObject")

'Global variables

Public Function FileExists(strFile As String) As Boolean
strFile = "\\tbstpgw3\D\ftp1\*.flg"
strDestFolder = "C:\Documents and Settings\me\Desktop\ftp1\"

If Dir(strFile) <> "" Then
objFSO.CopyFile strSourceFolder, strDestFolder, OverwriteExisting
Else
FileExists = False
End If

End Function


When I run it, I get Error: Expected ')' in line 17. I have no idea what is missing.

Emmanuel
05-07-2009, 12:43 PM
Thanks guys. I found the answer. I got it to work by doing this:

dim objFSO, f1, f, fc, strSourceFolder, strDestFolder, strlOOKFile, strlOOKFolder

'Constants
Const OverwriteExisting = TRUE

'Global variables
strSourceFolder = "\\tbstpgw3\D\ftp1\*.FLG"
strlOOKFolder = "\\tbstpgw3\D\ftp1\"
strDestFolder = "C:\Documents and Settings\Me\Desktop\ftp1\"


Set objFSO = CreateObject("Scripting.FileSystemObject")
Set f = objFSO.GetFolder(strlOOKFolder)

Set fc = f.Files
For Each f1 in fc

If objFSO.GetExtensionName(lcase(f1.name)) = "flg" Then
objFSO.CopyFile strSourceFolder , strDestFolder , OverwriteExisting
Else
End If

Next