Solved

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

Posted on 1998-09-28
42
746 Views
Last Modified: 2010-04-06
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
Comment
Question by:menorcanet
  • 15
  • 10
  • 9
  • +3
42 Comments
 

Author Comment

by:menorcanet
Comment Utility
Edited text of question
0
 

Expert Comment

by:mathewthampi
Comment Utility
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
 
LVL 4

Expert Comment

by:erajoj
Comment Utility
Hi,
Why make it hard, when it's soo easy?:

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

/// John
0
 
LVL 10

Expert Comment

by:viktornet
Comment Utility
John's right ;->

Regards.
Viktor Ivanov
0
 
LVL 1

Expert Comment

by:BlackDeath
Comment Utility
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
 
LVL 10

Expert Comment

by:viktornet
Comment Utility
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
 
LVL 1

Expert Comment

by:BlackDeath
Comment Utility
****. 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
 
LVL 10

Expert Comment

by:viktornet
Comment Utility
Thanks! I will have that in mind. You could also use GetMem(P, MAX_PATH); to allocate memory

//Vik
0
 

Author Comment

by:menorcanet
Comment Utility
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
 
LVL 1

Expert Comment

by:BlackDeath
Comment Utility
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
 
LVL 4

Expert Comment

by:erajoj
Comment Utility
No need for me. Take the points, BD! My answer wasn't worth more than a nickel anyway.

/// John
0
 
LVL 1

Expert Comment

by:BlackDeath
Comment Utility
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
 
LVL 4

Expert Comment

by:erajoj
Comment Utility
BD: Sent you the mail you asked for...

/// John
0
 
LVL 1

Expert Comment

by:BlackDeath
Comment Utility
yo. received. thanx. bye. black. death.
;-)=)

0
 
LVL 4

Expert Comment

by:erajoj
Comment Utility
BD, Are you into skydiving?

/// John
0
 
LVL 1

Expert Comment

by:BlackDeath
Comment Utility
this ;-)=) ain't a parasuite if you mean that.
0
 
LVL 1

Expert Comment

by:BlackDeath
Comment Utility
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
 
LVL 1

Expert Comment

by:BlackDeath
Comment Utility
i wonder why the comments scramble up from time to time... (?)

>-< BD
0
 
LVL 4

Expert Comment

by:erajoj
Comment Utility
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
 
LVL 1

Expert Comment

by:BlackDeath
Comment Utility
*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
 
LVL 1

Expert Comment

by:BlackDeath
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 4

Expert Comment

by:erajoj
Comment Utility
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
 
LVL 1

Expert Comment

by:BlackDeath
Comment Utility
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
 
LVL 27

Expert Comment

by:kretzschmar
Comment Utility
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
 
LVL 4

Expert Comment

by:erajoj
Comment Utility
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
 
LVL 27

Expert Comment

by:kretzschmar
Comment Utility
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
 
LVL 1

Expert Comment

by:BlackDeath
Comment Utility
ja, so it seems
0
 

Author Comment

by:menorcanet
Comment Utility
Hey, sorry there, I was out for some days.

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


0
 
LVL 27

Expert Comment

by:kretzschmar
Comment Utility
hi menorcanet,

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

meikl
0
 

Author Comment

by:menorcanet
Comment Utility
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
 
LVL 4

Expert Comment

by:erajoj
Comment Utility
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
 

Author Comment

by:menorcanet
Comment Utility
Ok, answer.


0
 

Author Comment

by:menorcanet
Comment Utility
??, how Do I reduce the points ?
(anyway, it's alright, I'll give 100 for each one)



0
 
LVL 1

Expert Comment

by:BlackDeath
Comment Utility
give the other half to vik

rex,
Black Death
0
 
LVL 4

Accepted Solution

by:
erajoj earned 200 total points
Comment Utility
Magnanimous, BD!

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

/// John
0
 
LVL 1

Expert Comment

by:BlackDeath
Comment Utility
as you said, john: too many pts for this question.

;-)

Black Death
0
 

Author Comment

by:menorcanet
Comment Utility
I'll give 100 to each one then, which will be 400. (x4)

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




0
 
LVL 4

Expert Comment

by:erajoj
Comment Utility
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
 
LVL 10

Expert Comment

by:viktornet
Comment Utility
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
 
LVL 1

Expert Comment

by:BlackDeath
Comment Utility
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
 

Author Comment

by:menorcanet
Comment Utility
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
 

Author Comment

by:menorcanet
Comment Utility
Ok, there you have 800 points.

see ya.

0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now