Solved fileCopy error (1 Viewer)

conception_native_0123

Well-known member
Local time
Today, 14:44
Joined
Mar 13, 2021
Messages
1,826
do not know if this has been answered before but i found this one
but it not quantify my issue. i am running test code to copy text files from one directory to other. i keep getting path not found error. but source file exists. what i doing wrong? thank you. here is my code

Code:
Public Sub copyTXTs()
On Error Resume Next
Dim sFile As String
Dim tFile As String
Const dirSource As String = "C:\finance\"
Const dirTarget As String = "C:\finance\new\"
sFile = "celtic.txt"
tFile = "celtic.txt"
  FileCopy dirSource & sFile, dirTarget & tFile
Exit Sub
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 03:44
Joined
May 7, 2009
Messages
19,169
you need to check if folder "new" already exists.
 

Minty

AWF VIP
Local time
Today, 19:44
Joined
Jul 26, 2013
Messages
10,355
Does the new folder already exist?
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 03:44
Joined
May 7, 2009
Messages
19,169
you can create a function that will "force" create the "new" folder.
and call the function first before copying:

public function fnForceMKDir(byval sPath as string) as string
dim i as integer, v as variant, s as string
fnForceMKDir = sPath
v = split(spath, "\")
on error resume next
for i = 0 to ubound(v)
s = s & v(i)
vba.mkdir s
s = s & "\"
next
end function
 

conception_native_0123

Well-known member
Local time
Today, 14:44
Joined
Mar 13, 2021
Messages
1,826
i even started and restarted the program many times. so memory should be destroyed many times too.
 

Minty

AWF VIP
Local time
Today, 19:44
Joined
Jul 26, 2013
Messages
10,355
Remove the On Error Resume Next - you need to see where the error is occurring.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 14:44
Joined
Feb 28, 2001
Messages
27,001
I have occasionally had issues with using constants in the way you are using them. Change your code to look like this:

Code:
Public Sub copyTXTs()
On Error Resume Next
Dim sFile As String
Dim tFile As String
Const dirSource As String = "C:\finance\"
Const dirTarget As String = "C:\finance\new\"
sFile = dirSource & "celtic.txt"       'concatenate directory to file
tFile = dirTarget & "celtic.txt"        'concatenate directory to file
  FileCopy  sFile,  tFile                 ' use concatenated strings to do the work
Exit Sub

If this fixes it, great! If not, then I don't see another obvious source of errors here. But string constants are sometimes odd in their behavior and I never figured out why.
 

Minty

AWF VIP
Local time
Today, 19:44
Joined
Jul 26, 2013
Messages
10,355
Having error handling active when you are developing, and particularly when debugging is really unhelpful, as it often hides an issue.

There is nothing obviously wrong with your code other than the strange use of the Const for the target path strings.
 

conception_native_0123

Well-known member
Local time
Today, 14:44
Joined
Mar 13, 2021
Messages
1,826
Code:
Public Sub copyTXTs()
On Error Resume Next
Dim sFile As String
Dim tFile As String
Const dirSource As String = "C:\finance\"
Const dirTarget As String = "C:\finance\new\"
sFile = dirSource & "celtic.txt"       'concatenate directory to file
tFile = dirTarget & "celtic.txt"        'concatenate directory to file
  FileCopy  sFile,  tFile                 ' use concatenated strings to do the work
Exit Sub

that did trick! i do not know why it was an error. thank you doc man and everyone elsee. problem solved! :)
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 14:44
Joined
Feb 28, 2001
Messages
27,001
Great. I have no specifics about why they don't work right, but CONST strings just don't act like ordinary variables. For example,


According to the person who filed this report, the strings act as though they are empty strings when referenced as though they were variables. If that is what was happening to you, then that would certainly explain what you say.

It PROBABLY has to do with the fact that the constant strings are not declared in the same memory area as ordinary DIM, PUBLIC, or PRIVATE variables. They are defined at compile time and are the same as other literal constants - stored in an area where modification is not allowed.
 

conception_native_0123

Well-known member
Local time
Today, 14:44
Joined
Mar 13, 2021
Messages
1,826
According to the person who filed this report, the strings act as though they are empty strings when referenced as though they were variables.

yes but CAN they be referenced as variables? as in, USED as variables? I always use them this way
 

Minty

AWF VIP
Local time
Today, 19:44
Joined
Jul 26, 2013
Messages
10,355
I'm guessing that the clue is in the name, they aren't variables - they are Constants.

In your code example, there is no reason at all to declare them as a constant, they are only used in this module and not referenced by anything else. I Use a Const for things like a default SQL String used in a forms rowsource, and set them up on the top of a form module.

I agree there seems no obvious cause that they should fail, but they did.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 14:44
Joined
Feb 28, 2001
Messages
27,001
yes but CAN they be referenced as variables? as in, USED as variables? I always use them this way

The specific question is whether they can be used as actual arguments in sub/function usage. Obviously, in the modification I suggested to you, they are indeed used as variables and they work great, as you reported. But in the case where the constants appeared in the line of the FileCopy, I'm not sure what is going on there.
 

Users who are viewing this thread

Top Bottom