0

i've been googling the last couple of days for a solution to my problem. There seems to be a ton of questions asked on this topic but somehow they are not working for me or (most likely) i'll be doing something wrong.

Const OverwriteExisting = True
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0

Dim FSO, MyFile, FileName, strFileName, strCopyTo

Set FSO = CreateObject("Scripting.FileSystemObject")

strFileName = "C:\......SourceFolder\SourceFile.html"
strCopyTo = "T:\....DestinationFolder\"
FileName = "T:\........\autorun.inf"

If FSO.FileExists(strFileName) Then         
            FileSystemObject.CopyFile strFileName, strCopyTo, True
            'FSO.CopyFile strFileName, strCopyTo, OverwriteExisting
        Else
            For i = 1 to 3

                If FSO.FileExists(strFileName) Then 
                        FileSystemObject.CopyFile strFileName, strCopyTo, True
                    else
                    WScript.sleep 1000 '1000 milliseconds
                End if


                    Set MyFile = FSO.OpenTextFile(FileName, ForAppending, True, TristateTrue)

                    ' Write to the file.
                    MyFile.WriteLine "Attempt " & i & " - " & Now
                    MyFile.Close
            Next
End If

What i'm trying to do is copying a file from the source folder located on C:\ to a shared network drive called T:. So i started the script with checking if the file exist, if it exists than it will have to copy it to the destination folder. If not i would like to try 3 more times with each at least 1 second a part from each other. for that i'm using the FOR method and WScript.sleep function. And if it didn't exist i'm writing it to a file.

Everything is working fine, except for the most important thing, the file even though it does exist it does not get copied.

I've checked the attributes on the origin and destination file, as according to google, the read-only attribute would prevent the file from being copied even when in the copy function "overwrite" is set to TRUE.

So i do not know where to look right now and would greatly appreciate any help.

Thanks Philippe

1
  • 2
    I don't know if this is the actual problem, but in the lines where you're trying to do the copying, you reference the object FileSystemObject. Problem is, when you created your object, you didn't call it FileSystemObject; you called it FSO. Trying turning on Option Explicit to avoid these sorts of problems. Commented Aug 28, 2015 at 15:02

1 Answer 1

1

I'm going to go ahead and post this as an answer instead of a comment, because I'm pretty sure it's what's causing your problem.

In the lines where you're trying to do the copying, you have

FileSystemObject.CopyFile [...]

However, at no point do you define a variable, let alone an object, named FileSystemObject. Your file system object is actually defined as

Set FSO = CreateObject("Scripting.FileSystemObject")

That's FSO, not FileSystemObject. Try changing your CopyFile lines to:

FSO.CopyFile strFileName, strCopyTo, True

If you started your code with <%Option Explicit%>, the interpreter ("compiler") would catch these sorts of errors for you.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Martha: easy fix, but simply couldn't see it, probably looked too much at the code and tried to many things that were not solving them. (couldn't see the woods through the threes; dutch saying...
@Philippe: "couldn't see the forest for the trees" is an English saying as well. :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.