1. ## FileCopy Doesnt Copy

Hello All,

Ran into an interesting occurrence today. FileCopy does not seem to work, but the CopyFile using fso does. What gives? What are the circumstances when FileCopy does not copy. There is no error thrown, it just does not copy the file. When I go to the target folder, it is blank.

Thanks

Code:
Function CopyFile(str_Source, str_Target)

Dim str_Source As String
Dim str_Target As String

str_Source = App.Path & "\My Data Files\My Projects\specialFiles\" & strFile & ".txt"
str_Target = strTargetFolderPath & "\" & strFile & ".txt"

Dim fso As Object

Set fso = CreateObject("Scripting.FileSystemObject")
fso.CopyFile str_Source, str_Target
Set fso = Nothing

'Why doesn't "this", down below in red, work? Doing a copy via fso as above works but not this.
'FileCopy str_Source, str_Target

End Function

2. ## Re: FileCopy Doesnt Copy

Originally Posted by clickman
Hello All,

Ran into an interesting occurrence today. FileCopy does not seem to work, but the CopyFile using fso does. What gives? What are the circumstances when FileCopy does not copy. There is no error thrown, it just does not copy the file. When I go to the target folder, it is blank.

Thanks

Code:
Function CopyFile(str_Source, str_Target)

Dim str_Source As String
Dim str_Target As String

str_Source = App.Path & "\My Data Files\My Projects\specialFiles\" & strFile & ".txt"
str_Target = strTargetFolderPath & "\" & strFile & ".txt"

Dim fso As Object

Set fso = CreateObject("Scripting.FileSystemObject")
fso.CopyFile str_Source, str_Target
Set fso = Nothing

'Why doesn't "this", down below in red, work? Doing a copy via fso as above works but not this.
'FileCopy str_Source, str_Target

End Function

comment out these lines:

Code:
Dim str_Source As StringDim str_Target As String

str_Source = App.Path & "\My Data Files\My Projects\specialFiles\" & strFile & ".txt"
str_Target = strTargetFolderPath & "\" & strFile & ".txt"

Dim fso As Object

Set fso = CreateObject("Scripting.FileSystemObject")
fso.CopyFile str_Source, str_Target     Set fso = Nothing
CALL the function with str_source and str_Target as arguments...that will work

3. ## Re: FileCopy Doesnt Copy

Yeah, your function makes no sense. str_Source and str_Target are function parameters, and then you try to create new variables with the same names, and use some unknown variable of strFile.

Out of curiosity, I tested out what happens when you "reuse" a parameter variable name with a Dim statement inside of the called function and I got a "duplicate declaration in current scope" error, so I'm not even sure how the code that you posted is even able to run.

4. ## Re: FileCopy Doesnt Copy

Ok -- I see I should have structured my question a different way.

This WORKS

Code:
Function CopyFile()

Dim str_Source As String
Dim str_Target As String
Dim strFile as String

strFile = "MyString"

str_Source = App.Path & "\My Data Files\My Projects\specialFiles\" & strFile & ".txt"
str_Target = strTargetFolderPath & "\" & strFile & ".txt"

Dim fso As Object

Set fso = CreateObject("Scripting.FileSystemObject")
fso.CopyFile str_Source, str_Target
Set fso = Nothing

End Function
***********
This Below Does Not Work No Errors thrown but Source file (it does exist) does not get copied to the Target. Also, str_Source and str_Target are created before the Call and are the same as in the above CopyFile(). I checked and checked and checked these strings by debug.print and they are same in both.

Code:
Function CopyFile(str_Source, str_Target)

Dim str_Source As String
Dim str_Target As String

FileCopy str_Source, str_Target

End Function
So then, are there instances where FileCopy will not "work". For example, my understanding is that if the source is open, it will fail, and that on some Win7 machines?? it will not copy the file. But are there other instances, other than coding errors like I likely had, where it does not work? You know, like if it was coded on a Monday it will work, but if coded on a Tuesday it will not . LOL

I guess nobody has ever run into this behavior?

Im WIN 10 Pro

5. ## Re: FileCopy Doesnt Copy

Originally Posted by OptionBase1
Yeah, your function makes no sense. I'm not even sure how the code that you posted is even able to run.
Me Neither ...

6. ## Re: FileCopy Doesnt Copy

Originally Posted by clickman
This Below Does Not Work No Errors thrown but Source file (it does exist) does not get copied to the Target. Also, str_Source and str_Target are created before the Call and are the same as in the above CopyFile(). I checked and checked and checked these strings by debug.print and they are same in both.

Code:
Function CopyFile(str_Source, str_Target)

Dim str_Source As String
Dim str_Target As String

FileCopy str_Source, str_Target

End Function
Why are you still attempting to create new variables with the same name as the parameter variables? Also, are you actually using VB6 or is this some sort of VBA code inside of another program?

People probably haven't ran in to this problem with FileCopy because every indication is that this has nothing to do with the FileCopy command and everything to do with other significant problems with your code.

7. ## Re: FileCopy Doesnt Copy

This is VB6.

Those Dim statements were not attempting to do anything but were remnants from some debugging I was doing. [Like the actual hard setting that was done in the Function CopyFile() ].. But WAIT ..OMG - That might actually have something to do with what is happening. Let me check if this:

Code:
Function CopyFile(str_Source, str_Target)

Dim str_Source As String
Dim str_Target As String

FileCopy str_Source, str_Target

End Function
Results in something different than this:

Code:
Function CopyFile(str_Source, str_Target)

FileCopy str_Source, str_Target

End Function

8. ## Re: FileCopy Doesnt Copy

Let me check some more and get back tommorrow..

9. ## Re: FileCopy Doesnt Copy

That's why you should to specify the variable type in the call:
Code:
Function CopyFile(str_Source As String, str_Target As String)

FileCopy str_Source, str_Target

End Function

10. ## Re: FileCopy Doesnt Copy

OK I messed up in trying to condense and simplify from the original code into the forum code blocks. Here are 3 blocks for working the issue.

Block #1 - the actual code block that works. This is as it exists in the project. This block includes my "*** is going on" comment and the commented out FileCopy down at the end of the function:

Code:
Function CopyExportFileToProjectsMostRecentFolder(strTargetFolderPath_MostRecentFolder, strExportFile)

'dim the strings that constitute the complete source and target file paths
Dim str_Source As String
Dim str_Target As String

'build the source and target strings for the fso.CopyFile or the FileCopy operation
'using the root stings that have been passed to the function
str_Source = App.Path & "\xGel Data Files\xGEL Exported Projects\gINT Format\" & strExportFile & ".gpj"
str_Target = strTargetFolderPath_MostRecentFolder & "\" & strExportFile & ".gpj"

'*****************************************************************************
'check the incoming parameter strings and the concatenated strings
'used in the actual fso.CopyFile or the FileCopy
'*****************************************************************************
'parameters passed to  function
Debug.Print "strTargetFolderPath_MostRecentFolder ="; strTargetFolderPath_MostRecentFolder
Debug.Print "strExportFile ="; strExportFile

'concatenated source and targets
Debug.Print "str_Source ="; str_Source
Debug.Print "str_Target ="; str_Target

'execute the copy
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")

fso.CopyFile str_Source, str_Target

Set fso = Nothing

'Why doesn't this work?
'FileCopy str_Source, str_Target

End Function

Forum Block #2 - the block extracted from Block #1 that works using the fso.CopyFile
Code:
Function CopyExportFileToProjectsMostRecentFolder(strTargetFolderPath_MostRecentFolder, strExportFile)

'dim the strings that constitute the complete source and target file paths
Dim str_Source As String
Dim str_Target As String

'build the source and target strings for the fso.CopyFile or the FileCopy operation
'using the root stings that have been passed to the function
str_Source = App.Path & "\xGel Data Files\xGEL Exported Projects\gINT Format\" & strExportFile & ".gpj"
str_Target = strTargetFolderPath_MostRecentFolder & "\" & strExportFile & ".gpj"

'*****************************************************************************
'check the incoming parameter strings and the concatenated strings
'used in the actual fso.CopyFile or the FileCopy
'*****************************************************************************
'parameters passed to  function
Debug.Print "strTargetFolderPath_MostRecentFolder ="; strTargetFolderPath_MostRecentFolder
Debug.Print "strExportFile ="; strExportFile

'concatenated source and targets
Debug.Print "str_Source ="; str_Source
Debug.Print "str_Target ="; str_Target

'execute the copy
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")

fso.CopyFile str_Source, str_Target

Set fso = Nothing

End Function
Forum Block #3 - the block extracted from Block #1 that does not work using the FileCopy:
Code:
Function CopyExportFileToProjectsMostRecentFolder(strTargetFolderPath_MostRecentFolder, strExportFile)

'dim the strings that constitute the complete source and target file paths
Dim str_Source As String
Dim str_Target As String

'build the source and target strings for the fso.CopyFile or the FileCopy operation
'using the root stings that have been passed to the function
str_Source = App.Path & "\xGel Data Files\xGEL Exported Projects\gINT Format\" & strExportFile & ".gpj"
str_Target = strTargetFolderPath_MostRecentFolder & "\" & strExportFile & ".gpj"

'*****************************************************************************
'check the incoming parameter strings and the concatenated strings
'used in the actual fso.CopyFile or the FileCopy
'*****************************************************************************
'parameters passed to  function
Debug.Print "strTargetFolderPath_MostRecentFolder ="; strTargetFolderPath_MostRecentFolder
Debug.Print "strExportFile ="; strExportFile

'concatenated source and targets
Debug.Print "str_Source ="; str_Source
Debug.Print "str_Target ="; str_Target

'execute the copy

FileCopy str_Source, str_Target

End Function

11. ## Re: FileCopy Doesnt Copy

What error does FileCopy give?
Can you give an example of the content of str_Source and str_Target?

12. ## Re: FileCopy Doesnt Copy

Originally Posted by Arnoutdv
What error does FileCopy give?
Can you give an example of the content of str_Source and str_Target?
It gives no error at all - it is just that there is nothing in the target folder if done by the FileCopy, but there IS if done via the fso.CopyFile. This of course, is what lead me to put all those debug statements in there, to check character by characte,r in the Immediate window if maybe my target folder was some folder in some strange place. The strings were spot on.. Indeed, observe that BOTH the fso.CopyFile and the offending FileCopy use the SAME concatenated strings.

The content of the file is a database file ... If you are meaning the strings then here they are. Again both the fso.CopyFile AND the FileCopy use the same strings.

strTargetFolderPath_MostRecentFolder =C:\Users\Art K\Desktop\xgel Desktop Project Archives\VB Project\LabMate 2020\xGel Data Files\xGEL Lab Data PDFs\New Residence\Most Recent
strExportFile =New Residence_Arts CF 39_ExportNum_11

str_Source =C:\Users\Art K\Desktop\xgel Desktop Project Archives\VB Project\LabMate 2020\xGel Data Files\xGEL Exported Projects\gINT Format\New Residence_Arts CF 39_ExportNum_11.gpj
str_Target =C:\Users\Art K\Desktop\xgel Desktop Project Archives\VB Project\LabMate 2020\xGel Data Files\xGEL Lab Data PDFs\New Residence\Most Recent\New Residence_Arts CF 39_ExportNum_11.gpj
Also just ran across this post that sounds like my issue, although I am on a Win10 machine

13. ## Re: FileCopy Doesnt Copy

Okay, this is very, very confusing...

First off...what does your call to this function look like? (The actual call, please).
If you are passing in the correct information, you should not need any declaration of new paths or files. Pass that information when you call the function.

14. ## Re: FileCopy Doesnt Copy

Could this be that FSO is set to overwrite by default. Try adding ,False to the FSO calls which will make then like VB's Filecopy.

15. ## Re: FileCopy Doesnt Copy

Code:
FileCopy Source, Destination
can't be easier.

or

Code:
Private Declare Function CopyFile Lib "kernel32" Alias "CopyFileA" (ByVal lpExistingFileName As String, ByVal lpNewFileName As String, ByVal bFailIfExists As Long) As Long

CopyFile Source, Destination, 0

16. ## Re: FileCopy Doesnt Copy

Originally Posted by Steve Grant
Could this be that FSO is set to overwrite by default. Try adding ,False to the FSO calls which will make then like VB's Filecopy.
Good approach to examine.. Ill let you know. However, the fso works just fine it is the FileCopy that does not.

17. ## Re: FileCopy Doesnt Copy

yes, like baka said.....

Simple

(Also like his second option)

18. ## Re: FileCopy Doesnt Copy

usually when we work with filecopying we also check for fileexists. its quite mandatory we do that.
also, another issue is folders that contains illegal characters or too long.

Code:
Private Declare Function GetModuleFileName Lib "kernel32.dll" Alias "GetModuleFileNameW" (ByVal hModule As Long, ByVal lpFileName As Long, ByVal nSize As Long) As Long
Private Declare Function GetShortPathName Lib "kernel32.dll" Alias "GetShortPathNameW" (ByVal lpszLongPath As Long, ByVal lpszShortPath As Long, ByVal cchBuffer As Long) As Long

Private Function CurrentFolder$() Dim sBuffer As String Dim lBuffer As String If CBool(App.LogMode = 0) Then CurrentFolder = App.Path Else sBuffer = Space$(259)
lBuffer = sBuffer
GetModuleFileName 0&, StrPtr(sBuffer), 260
GetShortPathName StrPtr(sBuffer), StrPtr(lBuffer), 260
CurrentFolder = Left$(lBuffer, InStrRev(lBuffer, "\") - 1) End If End Function this will get the current folder of the application, if app.path fails. or we use this to get the shortpath: Code: Private Function GetShortPath$(ByVal Source$) Dim lBuffer As String Dim nSize As Long lBuffer = Space$(259)
nSize = GetShortPathName(StrPtr(Source), StrPtr(lBuffer), 260)
GetShortPath = Left$(lBuffer, nSize) End Function another one that we need is fileexists. Code: Private Declare Function lOpen Lib "kernel32" Alias "_lopen" (ByVal lpPathName As String, ByVal iReadWrite As Long) As Long Private Declare Function lclose Lib "kernel32" Alias "_lclose" (ByVal hFile As Long) As Long Private Function FileExists(FileName$) As Boolean
Dim hFile&
hFile = lOpen(FileName, 0)
If hFile <> -1 Then lclose hFile: FileExists = True
End Function

so, what we do is.
if I want to copy a file from my folder, lets say:

c:\myfolder\data\thisfile.dat to d:\anotherfolder\

I do:

CurrentFolder or GetShortPath of that folder I want to copy from
and the same we do with the Destination folder.
what we get is
Source = "C:\MYFOLD~1\thisfile.dat"
Destination "D:\ANOTHE~1\thisfile.dat"

now we check if file exists using the FileExists on both.
that will tell us if theres a source to copy from and if the destination already have the file.
this to prevent errors, if theres no source and to ask the user to overwrite if file already exists, or rename or abort or whatever.

if we want to overwrite, we need to kill it, we can use the kill command
we also do a error checking if the kill worked, this will tell if the file is locked or not. if locked we know we can not do any copying until the file is unlocked.

19. ## Re: FileCopy Doesnt Copy

Originally Posted by baka
.... , another issue is folders that contains illegal characters or too long.
YUP.. I am going to check that too!. I was suspicious because I recall in just moving filed from one drive to another using the file manager, I ran into that very thing.

20. ## Re: FileCopy Doesnt Copy

The canonical way to check for file existence is to use GetFileAttributes

https://devblogs.microsoft.com/oldne...23-00/?p=24713

I use something like this

Code:
Public Function FileExists(sFile As String) As Boolean
If GetFileAttributes(sFile) = -1 Then ' INVALID_FILE_ATTRIBUTES
FileExists = (Err.LastDllError = 32) ' ERROR_SHARING_VIOLATION
Else
FileExists = True
End If
End Function
cheers,
</wqw>

21. ## Re: FileCopy Doesnt Copy

UPDATE.

My friends, I ran across a clue about fso.CopyFile being able to copy an open file, but that FileCopy is not able to copy an open file, so I added this to IsFileOpen check to see if the source file is open.

Yup- Somehow my source file is open. This is good news.

Code:
Function IsFileOpen(filename As String)
Dim filenum As Integer, errnum As Integer

On Error Resume Next   ' Turn error checking off.
filenum = FreeFile()   ' Get a free file number.
' Attempt to open the file and lock it.
Open filename For Input Lock Read As #filenum
Close filenum          ' Close the file.
errnum = Err           ' Save the error number that occurred.
On Error GoTo 0        ' Turn error checking back on.

' Check to see which error occurred.
Select Case errnum

' No error occurred.
' File is NOT already open by another user.
Case 0
IsFileOpen = False

' Error number for "Permission Denied."
' File is already opened by another user.
Case 70
IsFileOpen = True

MsgBox "The source file is OPEN so I can't do a FileCopy." & _
vbCrLf & _
vbCrLf & _
"Memory Tip: Cant copy open files." & _
"", vbInformation, "Friendly Reminder - Only Closed Files Can Be Copied With the VB6 FileCopy Statement "

' Another error occurred.
Case Else
Error errnum
End Select

End Function

22. ## Re: FileCopy Doesnt Copy

In the code above I can see that in my project the IsFileOpen = True for the file I am working with (.mdb file not a txt file). Now the question is how to close that file? I looked into fso, but do not see a way to close the file.

I also took a look at Baku's

Code:
Private Declare Function lOpen Lib "kernel32" Alias "_lopen" (ByVal lpPathName As String, ByVal iReadWrite As Long) As Long
Private Declare Function lclose Lib "kernel32" Alias "_lclose" (ByVal hFile As Long) As Long

Private Function FileExists(FileName\$) As Boolean
Dim hFile&
hFile = lOpen(FileName, 0)
If hFile <> -1 Then lclose hFile: FileExists = True
End Function
\to try and discern a pathwat to closing this file that is apparently open, but cant seem to work a way to close the file. Ideas??

23. ## Re: FileCopy Doesnt Copy

You can't just close an open file, which is obviously in use.
Is the file in use by your application or an external application?
Otherwise if FSO does copy an open file, then just the FSO

24. ## Re: FileCopy Doesnt Copy

Originally Posted by Arnoutdv
You can't just close an open file, which is obviously in use.
Is the file in use by your application or an external application?
Otherwise if FSO does copy an open file, then just the FSO
That is what to me is the strange thing. The file itself is created by my client application, and it is created using a fso operation. It is not used anywhere and immediately after the file is created, this created file is then to be copied to another location. With FileCopy it is No Joy, but with fso.CopyFile it IS copied.

Yes, going forward, I will perform the operation via fsoCopyFile, but inquiring minds just want to know what is going on and the "why" the FileCopy statement way does not copy the file, and if the file is "really" open somehow was a result iof it being created by fso.CopyFile.

Question, fso.CopyFile can copy an open file. Does the copy itself inherit the "open"?? Is the resulting file also "open" ??

Here is the code that makes the copy of the file: (App.Path & "\My Data Files\My Exported Projects\Gpj Format" & strExportFile & ".gpj")

Code:
Dim fso As Object

Set fso = CreateObject("Scripting.FileSystemObject")

Dim ExportFile As Object

'clone the gpj file and assign the cloned copy's  file extension as .mdb
Set ExportFile = fso.GetFile(App.Path & "\My Data Files\My Exported Projects\Gpj Format\" & strExportFile & ".gpj")

ExportFile.Copy (App.Path & "\My Data Files\My Exported Projects\Access Format\" & strExportFile & ".mdb")

Set ExportFile = Nothing
Set fso = Nothing

'Call the vb6FileCopy

25. ## Re: FileCopy Doesnt Copy

not sure how fso works. but u need to close the operation so that the file is not locked.
so, look into your own code how u "copy" the file and make sure u close the operation, as long is open it will be locked.

26. ## Re: FileCopy Doesnt Copy

Originally Posted by baka
not sure how fso works. but u need to close the operation so that the file is not locked.
so, look into your own code how u "copy" the file and make sure u close the operation, as long is open it will be locked.
I had earlier posed the following ..

Question, fso.CopyFile can copy an open file. Does the copy itself inherit the "open"?? Is the resulting file also "open" ??
It appears that there is no inheritance based on checking with your IsFileOpen() function.

Now, about the file being open ... It was indeed, a connection "GConn" to the the file that resulted in the "openness" because of some work we were doing previously. Here is how that state got created...

Code:
        Set GConn = New ADODB.Connection

strGConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data " & _
"Source=" & App.Path & "\My Data Files\My Exported Projects\GPJ Format\" & strExpFile & ".gpj" & ";Mode=ReadWrite;"

'open connection to the newly created export file
GConn.Open strGConn
"
Does this make sense to you. I am familiar with closing recordsets and closing connections to a database, but connections to a DB, where you use the "Source =..." is a new one on me in this project written by others long ago, and I'll put that in my toolbox.

I went and moved my FileCopy code to be after the GConn = Nothing, and all is fine if I want to use the FileCopy. (I went ahead and used fso.CopyFile in the end, but left in the CopyFile statement with comments to remind me of what is happening.) I like to leave maintenance record of my mistakes at the place of my major accidents to remind me. LOL

BTW - In several thousand lines of code, where we are opening and reading and writing and closing multiple databases every milisecond, this is the ONLY place where this "Source=" & App.Path ...." occurs.

Thanks so much for your help, and the help of others that gave me the breadcrumb trail.

27. ## Re: FileCopy Doesnt Copy

" & App.Path ...." - The path to the folder where the application is located.

28. ## Re: FileCopy Doesnt Copy

Originally Posted by Argus19
" & App.Path ...." - The path to the folder where the application is located.
I think you were asking for confirmation concerning what the "&" and the "App.Path" meant.

When working in the IDE, where you are doing your coding, for example from a folder on your desktop, App.Path will point to that desktop development folder. Then, when you build the exe, and install it with your setup program in the folder, then that App.Path (a string) string will change to reflect where your setup actually installed your program.

The "&" is a concatenation string to marry the "Source =" to the result returned by App.Path. Note that there is another & after that App.Path which marries the remainder of the string to what came before and into a full string representing the full path to the file that you are using as the Source.

The App.Path is a convenient way to build the string. Sure, you could write it out longhand as:

"Source= C:\LabTest\Labviewer2020\My Data Files\My Exported Projects\GPJ Format\mydatatfile.txt"

But the App.Path is a more flexible, and easier way, because that App.Path automatically "fills in" that "C:\LabTest\Labviewer2020" portion if that is the installation location, and will auto fill it with another thing when you are executing out of your IDE folder.

For example, if your IDE is in a folder on your desktop say, "C:\Users\BillyBob\Desktop", then App.Path will be equal to that at run time from the IDE

If you actual installation is located somewhere else, say at C:\LabTest\Labviewer2020\, then App.Path with be equal to that at run time.

Set up your code and do a Debug.Print of the App.Path to see what it returns. That will be informative as to what is going on.

Of course the VB6 Help can explain things more completely.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•