Solved

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

Posted on 1998-09-28
42
782 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 15
  • 10
  • 9
  • +3
42 Comments
 

Author Comment

by:menorcanet
ID: 1340999
Edited text of question
0
 

Expert Comment

by:mathewthampi
ID: 1341000
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
ID: 1341001
Hi,
Why make it hard, when it's soo easy?:

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

/// John
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Expert Comment

by:viktornet
ID: 1341002
John's right ;->

Regards.
Viktor Ivanov
0
 
LVL 1

Expert Comment

by:BlackDeath
ID: 1341003
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
ID: 1341004
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
ID: 1341005
****. 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
ID: 1341006
Thanks! I will have that in mind. You could also use GetMem(P, MAX_PATH); to allocate memory

//Vik
0
 

Author Comment

by:menorcanet
ID: 1341007
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
ID: 1341008
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
ID: 1341009
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
ID: 1341010
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
ID: 1341011
BD: Sent you the mail you asked for...

/// John
0
 
LVL 1

Expert Comment

by:BlackDeath
ID: 1341012
yo. received. thanx. bye. black. death.
;-)=)

0
 
LVL 4

Expert Comment

by:erajoj
ID: 1341013
BD, Are you into skydiving?

/// John
0
 
LVL 1

Expert Comment

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

Expert Comment

by:BlackDeath
ID: 1341015
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
ID: 1341016
i wonder why the comments scramble up from time to time... (?)

>-< BD
0
 
LVL 4

Expert Comment

by:erajoj
ID: 1341017
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
ID: 1341018
*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
ID: 1341019
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
 
LVL 4

Expert Comment

by:erajoj
ID: 1341020
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
ID: 1341021
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
ID: 1341022
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
ID: 1341023
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
ID: 1341024
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
ID: 1341025
ja, so it seems
0
 

Author Comment

by:menorcanet
ID: 1341026
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
ID: 1341027
hi menorcanet,

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

meikl
0
 

Author Comment

by:menorcanet
ID: 1341028
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
ID: 1341029
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
ID: 1341030
Ok, answer.


0
 

Author Comment

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



0
 
LVL 1

Expert Comment

by:BlackDeath
ID: 1341032
give the other half to vik

rex,
Black Death
0
 
LVL 4

Accepted Solution

by:
erajoj earned 200 total points
ID: 1341033
Magnanimous, BD!

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

/// John
0
 
LVL 1

Expert Comment

by:BlackDeath
ID: 1341034
as you said, john: too many pts for this question.

;-)

Black Death
0
 

Author Comment

by:menorcanet
ID: 1341035
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
ID: 1341036
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
ID: 1341037
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
ID: 1341038
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
ID: 1341039
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
ID: 1341040
Ok, there you have 800 points.

see ya.

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

707 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