Discussion:
CopyFile
(too old to reply)
Richard H.
2010-10-13 19:25:35 UTC
Permalink
How can I copy from a folder into the root. It's running from a memory
card so I cannot put the disk letter in the full path as it will
change depending which computer it's on.

This is what I have, but it doesn't work -

CopyFile(PChar('\myfiles\'+file_from),PChar(file_to),False);





Thanks..
Jamie
2010-10-13 23:47:26 UTC
Permalink
Post by Richard H.
How can I copy from a folder into the root. It's running from a memory
card so I cannot put the disk letter in the full path as it will
change depending which computer it's on.
This is what I have, but it doesn't work -
CopyFile(PChar('\myfiles\'+file_from),PChar(file_to),False);
Thanks..
"GetWindowsDirectory" Will most likely do what you want.. From that,
you can extract the drive path..

P.S.
I think you need to be in Admin mode before you can do that with
these new fangle dangle OS'es these days! ;)
Maarten Wiltink
2010-10-14 07:19:04 UTC
Permalink
Post by Richard H.
How can I copy from a folder into the root. It's running from a memory
card so I cannot put the disk letter in the full path as it will
change depending which computer it's on.
This is what I have, but it doesn't work -
CopyFile(PChar('\myfiles\'+file_from),PChar(file_to),False);
The path for the root directory is '\'. You might try putting that
in front of file_to in the same way as you're putting a path
in front of file_from.

I think Jamie meant GetCurrentDirectory instead of GetWindowsDirectory.
(It may have a slightly different name, but the idea is just that: to
get the current working directory.)

The above code will work relative to the current working directory
already, of course, because you're not specifying complete paths.
That's the whole idea of a current working directory. Note that there
is no guarantee that it is what you want. Programs can be started
with a different one than the directory the executable is in, for one.

Groetjes,
Maarten Wiltink
Richard H.
2010-10-14 08:11:26 UTC
Permalink
I had no luck with that way, so I did -

for index := 0 to Files.Count -1 do
begin

dir := GetCurrentDir;

Files.Directory := dir+'\myfiles';
file_from := Files.Items[index];

Files.Directory := dir+'\';
file_to := 'copy_'+file_from;

CopyFile(PChar(file_from),PChar(file_to),False);
end;


This still doesn't work. I think it's something to do with it being in
a loop.
Korben Dallas
2010-10-14 10:12:33 UTC
Permalink
Post by Richard H.
I had no luck with that way, so I did -
for index := 0 to Files.Count -1 do
begin
dir := GetCurrentDir;
Files.Directory := dir+'\myfiles';
file_from := Files.Items[index];
Files.Directory := dir+'\';
file_to := 'copy_'+file_from;
CopyFile(PChar(file_from),PChar(file_to),False);
end;
This still doesn't work. I think it's something to do with it being in
a loop.
i did not undertand what you want to copy and where but i think the
problem is file_to var.
file_from contains the complete path so you cant copy to
'copy_'+path+filename

i think you have to write something like

file_from := CompletePath+filenames[index];
file_to := '\'+filenames[index];

where completepath could be Getcurrentdir+'\myfiles\'

Kd
Maarten Wiltink
2010-10-14 10:31:03 UTC
Permalink
Post by Richard H.
I had no luck with that way, so I did -
What way? We're not psychics.
Post by Richard H.
for index := 0 to Files.Count -1 do
begin
dir := GetCurrentDir;
Files.Directory := dir+'\myfiles';
file_from := Files.Items[index];
Files.Directory := dir+'\';
file_to := 'copy_'+file_from;
CopyFile(PChar(file_from),PChar(file_to),False);
end;
This still doesn't work. I think it's something to do with it being in
a loop.
It has nothing to do with being in a loop. The problem is in what you
do with the value from GetCurrentDir. You said you didn't know where to
get the current drive from, so someone told you. You weren't told that
that function returns more than just the drive, but a very minor amount
of intelligence and experimentation might have turned up both the
problem and the solution.

There's also another problem to do with caching.

Step through your code and inspect the values of your variables. If it
isn't obvious then, go into management.

Groetjes,
Maarten Wiltink
a***@aol.com
2010-10-14 13:55:09 UTC
Permalink
On 14 Oct, 11:31, "Maarten Wiltink" <***@kittensandcats.net>
wrote:
<snip>
Post by Maarten Wiltink
Step through your code and inspect the values of your variables. If it
isn't obvious then, go into management.
Ho ho ho - or into government <g>.

Alan Lloyd
a***@aol.com
2010-10-14 14:01:39 UTC
Permalink
Post by Richard H.
How can I copy from a folder into the root. It's running from a memory
card so I cannot put the disk letter in the full path as it will
change depending which computer it's on.
If the executable is on the memory card (as you say) then . . .

ExtractFileDrive(ParamStr(0))

. . . will give you the drive for the card.

OTOH your program could always run through the drive looking for a
file or folder which you _know_ is on the memory card.

Alan Lloyd
Richard H.
2010-10-14 14:36:12 UTC
Permalink
Thanks, I have it working correctly now.

Maarten, thanks for the free insult :)
Maarten Wiltink
2010-10-15 08:08:26 UTC
Permalink
Post by Richard H.
Thanks, I have it working correctly now.
Good! I'm glad to hear that. No sarcasm this time.
Post by Richard H.
Maarten, thanks for the free insult :)
Always pleased to be of service.

At the risk of overstaying my welcome, I'd like to expound a bit
on the development of your code as we could see it from our side.

You had a first version. It didn't work. You identified, mostly
correctly, the problem with it. You asked for advice. You got a
little more than you bargained for.

The next step is where it got problematic, and where I got curt.
You tried to use the advice you were given, but you did not spend
enough time checking the advice itself, and what the functions
mentioned did exactly, and which part of that you needed, and how
to integrate it with your existing solution to make it start to
work.

Instead, you applied what has been described as the Jackson Pollack
approach to programming - throw code at the system, trying to see
what works.

While it's undeniably creative and occasionally makes for beautifully
psychedelic code, it's actually quite rare that you stumble upon the
correct solution using _only_ that method. Thinking carefully about
what you have, what you want, and how to get from one to the other,
generally works faster and better.

There is actually a bit of real science in there. 'What you have' is
called the precondition. 'What you want' is the postcondition. The
prescription how to transform the precondition into the postcondition
is called an algorithm. Note that to design an algorithm (which can
be very simple!), you need to know where you come from and where you
want to go. Only then can you think clearly about the steps to take
on the way.

Hope that helps.

Groetjes,
Maarten Wiltink

Loading...