copying a file

Is it possible to duplicate itself.... for example

if fileexists c:\myprog.exe  then copy it c:\windows then delete c:\myprog.exe so i dont have to make a seperate program as an installation it can be an "all in one" program.... depending on where the exe is,it becomes an instalation program, other wise it just does it task....
LVL 2
craig_capelAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

craig_capelAuthor Commented:
well ok i will not be able to delete myexe prog untill the file has been moved, and then the file that has been moved deletes the exe, so yeah i understand that you can not do that in one go..... i just want to save on space.
0
AttarSoftwareCommented:
I guess your program could perform a CopyFile, and then CreateProcess this new file, with a parameter telling it that it has to wait for the first program to finish and then delete it, and the handle of the first program.  Thus, the second executable watches the first until it terminates, and then deletes it...

Heh, complex no?

Have Fun...

Tim

PS:  Look at the following functions...

CreateProcess
ParamCount
ParamStr
WaitForSingleObjectEx
DeleteFile
CopyFile
0
men xinCEOCommented:
Can you use reboot?

;-)

menxin
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

GwenaCommented:
Hi craig :)

I'm not exactly sure what you are asking.... but if what you need to do is have your running exe make a copy of itself then here is some code that does that....


 CopyFile(pchar(Application.ExeName),pchar('c:\windows\desktop\'+ExtractFileName(Application.ExeName)),false);

this line places a copy of itself into \windows\desktop
the copy has the same name....

This allows a program to copy itself..... you would need to run the new program....then stop the original...then have the now running copy delete the first file.... tricky but do-able... this lets you alter an exe file without a reboot :)


If this helps you and is an acceptable answer to your question please give madshi the points because he is the one that wrote the line of code :-)
0
AttarSoftwareCommented:
Surely that line of code is the easiest part of this problem...

As I said before, just pass the process handle of the calling EXE as a parameter into the second copied EXE, and then you can either terminate the process or wait for the process to finish, and then delete the original file...

So just add the function "GetCurrentProcess" to the list of functions above, and you should have enough to go on...

Tim.
0
AttarSoftwareCommented:
Surely that line of code is the easiest part of this problem...

As I said before, just pass the process handle of the calling EXE as a parameter into the second copied EXE, and then you can either terminate the process or wait for the process to finish, and then delete the original file...

So just add the function "GetCurrentProcess" to the list of functions above, and you should have enough to go on...

Tim.
0
AttarSoftwareCommented:
Or if you can reboot (as menxin says), then you can place a string registry value in:

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\RunOnce

to the effect of "C:\NewDir\NewProg.exe -DC:\oldDir\oldExe.exe"

and then when the program runs, look for the -D and delete any filename that follows it...

The runOnce registry entry will be removed by Windows once it has been executed...

Good luck again,

TIm.
0
craig_capelAuthor Commented:
Gwena, its fairly simple the idea is.... I can easily copy a file other than the exe that is doing the copying....

BUT, if i copy the exe that sending the command it gives me nice errors, sharing violation, but yeah to reboot it would be the run command, or i can get the first program to run the second program and the first would then terminate leaving the second one running.......

But yes if this does not give an error when copying itself, gladly point out who deserves the points.....
0
craig_capelAuthor Commented:
OMG - Gwena. i have never been able to get something like that to work, without read error addressing problems.... impressed!!!!!!

who wants the points? post it
0
AttarSoftwareCommented:
Me!

Heh, Tim.
0
AttarSoftwareCommented:
Sorry about that Gwenna, I got greedy ;O(

Cheers,

Tim.
0
craig_capelAuthor Commented:
CopyFile(pchar(Application.ExeName),pchar('c:\windows\desktop\'+ExtractFileName(Application.ExeName)),false);

this is what i needed, that was all, the rest is how you put it.... childsplay!

Gwena, or Madashi Need the points.... i have not even used you r code... sorry
0
AttarSoftwareCommented:
*boggle* ;O)
0
craig_capelAuthor Commented:
ok i will give you the points, if you can tell me in what was you deserve them?
0
AttarSoftwareCommented:
Soz, I thought you were looking for different ways to perform a move operation on a running executable, not just a way to copy a file...

My mistake...

Madshi, or Gwenna step up and claim your prize...

Tim.
0
GwenaCommented:
Hi Craig :)

This problem also had me stumped!  Madshi is the one to give
the points to.... he was the ONLY one that was able to solve this
problem when I asked the question... I gave him 300 points for that line..... I'm sure he will be happy to take your 25.... just
post a 0 point question for madshi... and tell him to come here
and claim his 25 points :)



...Gwen..
0
GwenaCommented:
OK Craig I posted a note to Madshi telling him to come and claim the points... so he should soon show up to do so :)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MadshiCommented:
Hi friends!!

Gwena, it's really fair from you to offer these points to me, but hey, such fairness should be rewarded!! So please take these points...   :-)

craig_capel, you can directly accept one of Gwena's comments as the answer...

Regards, Madshi.
0
craig_capelAuthor Commented:
Uh how?
0
craig_capelAuthor Commented:
Ah ok.... did not see that :)
0
craig_capelAuthor Commented:
I bet your feeling guilty :)
0
GwenaCommented:
WoW!

Thanx all...
Now I no longer have 0 expert points :)
Better watch out Madshi... I'm catching up
to you... I only have 173,624 points to go ;-)
0
MadshiCommented:
:-)
0
craig_capelAuthor Commented:
in all my time i have been here, i have been able to only answer one question :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.