• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 849
  • Last Modified:

<---- Copying files in Delphi ( Pchar,String..) ---->

I need to copy a file in Delphi.
But the COPYFILE routine needs 2 Pchar variables.
I can do this: Pcharvar:='file1'
but I need to do this: Pcharvar:=stringvar+'file1'
HOW DO I add an string to the Pchar Variable ?
How do I get the WinDir and put it in the Pchar ?

My final idea is to do this:
Pcharvar:= WinDiw+string+'file'

thanx

0
menorcanet
Asked:
menorcanet
  • 15
  • 10
  • 9
  • +3
1 Solution
 
menorcanetAuthor Commented:
Edited text of question
0
 
mathewthampiCommented:
Here is a little function you can add to your application. It works exactly like copyfile except that it takes normal strings instead of PChars. Now you can build up your string using pascal strings and just call this function. Hope this helps
Cheers
Mat.

function copyFile2(originalFilename, copiedFilename: string, bFailifExists: Boolean ): LongBool;
var
   myPchar1, myPchar2: PChar;
   myString: string;
begin
     myPChar1 := strAlloc(length(string1));
     myPChar2 := strAlloc(length(string2));
     strPCopy(myPChar1, originalFilename);
     strPCopy(myPChar2, copiedFilename);
     copyFile2 := copyFile(myPchar1, myPChar2, bfailIfExists);
     strDispose(myPChar1);
     strDispose(myPChar2);

end;


0
 
erajojCommented:
Hi,
Why make it hard, when it's soo easy?:

CopyFile( PChar( StringVar + OriginalFileName ), PChar( StringVar + DestinationFileName ), False );

/// John
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
viktornetCommented:
John's right ;->

Regards.
Viktor Ivanov
0
 
BlackDeathCommented:
for windir use api-call getwindowsdirectory:

var
  p: PChar;

function strGet_WinDir: string;
begin
  p := StrAlloc(Succ(MAX_PATH));
  GetWindowsDirectory(p, MAX_PATH);
  Result := StrPas(p);
  StrDispose(p);
end;

Black Death.
0
 
viktornetCommented:
or else use this....
function Get_WinDir: string;
var
   p : array[0..MAX_PATH] of char;
begin
  GetWindowsDirectory(p);
  Result := StrPas(p);  
end;

Regards,
Viktor Ivanov
0
 
BlackDeathCommented:
****. my last comment lead to system crash & was lost. so, once again.

hi, vik.

you forgot the second parameter specifying the size of the directory buffer.

the reason why i prefer to handle allocation and disposition manually is:

in former times i've done lots in turbo/borland pascal on machines with only a few millibytes of ram ;-).
i had to watch the stack usage heavily especially in recursions. that's why i used mainly 4 bytes for pchar to be put on stack instead of declaring the actual variable with much more space. it might look a little bit nasty, but you know: old habits die hard.

anyway, have a nice day,

Black Death.
0
 
viktornetCommented:
Thanks! I will have that in mind. You could also use GetMem(P, MAX_PATH); to allocate memory

//Vik
0
 
menorcanetAuthor Commented:
The answer from JOHN (EREJOJ) was the best, thanx a lot..

CopyFile( PChar( StringVar + OriginalFileName ), PChar( StringVar + DestinationFileName ), False );

AND BLACKDEATH's function for the WinDir is great Too, THANX BOTH

var
  p: PChar;

function strGet_WinDir: string;
begin
  p := StrAlloc(Succ(MAX_PATH));
  GetWindowsDirectory(p, MAX_PATH);
  Result := StrPas(p);
  StrDispose(p);
end;

0
 
BlackDeathCommented:
so, menorca:

this wasn't worth 200 pts.

what do you think of deleting this question?
if you wanna give us points, post one q for each of us (erajoj & me) rated 1 point D?

have a nice weekend,

Black Death.
0
 
erajojCommented:
No need for me. Take the points, BD! My answer wasn't worth more than a nickel anyway.

/// John
0
 
BlackDeathCommented:
so was mine, john.

menorca: i will answer this one if you promise that you answer the 199 points-question i'm going to post for you as soon as i have 199 points again so it doesn't have to be deleted in order to let others have a look at this (if necessary).

whaddaya think of this?

Black Death.


0
 
erajojCommented:
BD: Sent you the mail you asked for...

/// John
0
 
BlackDeathCommented:
yo. received. thanx. bye. black. death.
;-)=)

0
 
erajojCommented:
BD, Are you into skydiving?

/// John
0
 
BlackDeathCommented:
this ;-)=) ain't a parasuite if you mean that.
0
 
BlackDeathCommented:
to answer your question:
nope, but actually i've made one jump with a buddy of mine (who's a teacher in paragliding)
but never stand-alone.
and with another one a trip in a glider. it was a lucky day: 4 1/2 hours (with 2 people on board!!)
what i'm still missing is hang gliding. and that's what i'm interested in at most.
beleive it or not: a third lad (all of these work at siemens, like me), who's to south africa for 4 weeks now, actually is into hang gliding. but we've never had the chance to meet for a trip. but i think i'm gonna get this one as well...

so far,

bye

Black Death.

and i ain't got no para.
0
 
BlackDeathCommented:
i wonder why the comments scramble up from time to time... (?)

>-< BD
0
 
erajojCommented:
Man, I'm disappointed! I've been online since the mid eighties (BBS's and then the internet), and I've seen all the smileys I can cope.

Of course, it's not a parachute! A parachute would look something like this: (|>¤<  (if it would be VERY windy that day...)

No, I meant "www.blackdeath.com"! :-)

/// John
0
 
BlackDeathCommented:
*whoops* - i'm sorry...

1st for spelling para wrong
(i'm native german, you know...not my fault! - blame my parents...but you won't succeed - daddy's guilty (for my language) and dead (for 12 years)...and mom's the best...(partly 'cos she's the only significant alive - she became 50 last month!) so f*** what.

2nd for embarrassing you (regarding the smiley)... -nevermind!

und:
nope, Black Death is one of my nicknames here.

(i almost only wear black and especially on monday mornings i look extremely sinister...
apart from that i've smoked that cigarette brand once)

well - i hope you forgive me for being impertinent.

anyway. i'm gonna knock off soon - weekend's calling... booze'n'dope'n'r&r...

und don't let the chicks get on my nerves this w.e.: i wanna be left alone these dayz -
this week at work was a *_BIG PILE OF **** !!!_*

now. that was it. as cool as a cucumber again.

thanx for listening.

pax vobiscum (et cum spiritu tuo, john)

Black Death.
0
 
BlackDeathCommented:
ach:

with "is one of my nicknames here" i didn't mean "here at xx" but "here at home" which is braunschweig in germany...

cheers,

BD
0
 
erajojCommented:
BD,
Thanks for sharing! No need to be sorry.
I'm swedish, but I am not sorry about that.
I'm working my lardy ass off and I am not sorry about that.
I spent the weekend with a woman friend of mine and her sons
and parents at their summerhouse, deep in the swedish forest.
We ate, drank, picked chanterelles, chopped wood and just enjoyed
each others company, it was delightful, and soo easy to do.

Peace be with you too (and with thy spirit)!

This phrase you can use: "Cogito, ergo doleo", since it seems useful in your situation. However, don't stay in that situation! I didn't.

/// John

0
 
BlackDeathCommented:
hey, john!

thank you for your effort to cheer me up - it works a little bit.
i'll remember your words. in fact i'm heading for a fundamental change, you know.

this weekend wasn't so bad after all. a chum of mine came along and we've had a couple of very relaxing hours when sittin' in the pub & playing billards and darts, talking about our plans regarding future business. (that's what i've mentioned above - the change)

i'm really looking forward to it.

so, that's it for now, c ya,

Black Death.

btw: wasn't this schopenhauer?



0
 
kretzschmarCommented:
Try This
for string<->PChar

procedure TForm1.Button1Click(Sender: TObject);
var
  ok : boolean;
  s, ss, sss : string;
begin
  sss := 'bat'
  s := 'c:\autoexec.'+sss+#0;
  ss := 'c:\tmp\autoexec.Bla'+#0;
  copyfile(@s[1],@ss[1],ok);
end;


Meikl
0
 
erajojCommented:
I didn't know it was leech-season, did you? ;-)

This code adds an superfluous zero terminator to the string (unless shortstring is used):
       s := 'c:\autoexec.'+sss+#0;
       ss := 'c:\tmp\autoexec.Bla'+#0;

This code uses a nonrecommended type convertion:
       copyfile(@s[1],@ss[1],ok);

Otherwise it was almost as good as the provious comments on the subject. :-)

/// John

0
 
kretzschmarCommented:
Hello John,

it was only an idea (but it works by no using shortstring and stringlenth <=254 Chars), and by reading the other comments it seems the question is anwered.

meikl
0
 
BlackDeathCommented:
ja, so it seems
0
 
menorcanetAuthor Commented:
Hey, sorry there, I was out for some days.

Well, what should we do ?  
Answers from all of you are alright..


0
 
kretzschmarCommented:
hi menorcanet,

reject my answer, i think erajoj had the best answer(comment) given.

meikl
0
 
menorcanetAuthor Commented:
ok,

so half of the points (100x4) are for erajoj, for the COPY answer,
and the other half for BD or Viktor, for the GETWINDIR.

so, now what ?


0
 
erajojCommented:
Hi,
1. Reduce the number of points for this question to 50.
2. Post a new question for BD or Viktor for 50 pts.

/// John

0
 
menorcanetAuthor Commented:
Ok, answer.


0
 
menorcanetAuthor Commented:
??, how Do I reduce the points ?
(anyway, it's alright, I'll give 100 for each one)



0
 
BlackDeathCommented:
give the other half to vik

rex,
Black Death
0
 
erajojCommented:
Magnanimous, BD!

Thanks, menorcanet! Too many pts for this question. Hope to repay you someday.

/// John
0
 
BlackDeathCommented:
as you said, john: too many pts for this question.

;-)

Black Death
0
 
menorcanetAuthor Commented:
I'll give 100 to each one then, which will be 400. (x4)

but..
How do I "split" the points??




0
 
erajojCommented:
Hi,
Reject my answer, then give the points to Viktor, he's had a tough time in school, so he needs some cheering up. Don't care about splitting the points.

(Don't know what I was thinking when I answered, maybe I'm beginning to get senile?!)

/// John
0
 
viktornetCommented:
Don;t worry guys... If you need the points just keep 'em... It's enough the knowledge I get as I answer questions =)

Cheers,
Viktor
0
 
BlackDeathCommented:
seems like nobody wanna have those points, eh?
menorca - why don't you delete this q and post the essence as a q for free if you feel that it is of interest for others?

cheerio,

Black Death.
0
 
menorcanetAuthor Commented:
I think I can't delete it once it's been asnwered.
Anyway, I already have 75 points now for not asking in several days.


0
 
menorcanetAuthor Commented:
Ok, there you have 800 points.

see ya.

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 15
  • 10
  • 9
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now