File should delete itself
-
Hi all, I have made a dialog based application from which i am trying to delete various other files.. my problem is it deletes all the file from the folder but leaves itself and the folder there only.... how can i delete it. Thanks in advance
-
Hi all, I have made a dialog based application from which i am trying to delete various other files.. my problem is it deletes all the file from the folder but leaves itself and the folder there only.... how can i delete it. Thanks in advance
An executable cannot delete "itself". You may consider a script, or a batch file which will have all the files of your interest deleted. Here's a batch file, which works for me:
@echo off
sleep 50000
del /q "D:\files\file1.txt"
del /q "D:\files\file2.txt"
del /q "D:\files\MyDialog.exe"
del /q "D:\files\mybatch.bat"
exitWhereas
mybatch.bat
is the batch file itself. Finish the stuff with your dialog and call this batch file with a ShellExecute()[^]. I've added a delay at the beginning of the batch file to make sure that the dialog gets enough time to quit.Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP
-
Hi all, I have made a dialog based application from which i am trying to delete various other files.. my problem is it deletes all the file from the folder but leaves itself and the folder there only.... how can i delete it. Thanks in advance
Hi Friend, You should try this trick : There are several tricks for making programs that are in one way or another able to delete themselves, without leaving any traces in the system. One of the most wide-spread techniques, and also the one which is supposed to be one of the most platform independent (between Windows versions) is the following:
Start the program you want to delete (EXE1).
From this program, drop a second exe file (EXE2).
Open a file handle to EXE2 from EXE1, using CreateFile, with the flag "FILE_FLAG_DELETE_ON_CLOSE".
Execute EXE2 from EXE1 (e.g. with CreateProcess), causing the operating system to open another file handle to EXE2.
Let EXE1 exit and terminate (which will implicitly cause its filehandle to EXE2 to close, leaving only the operating system's filehandle to EXE2 left open).
EXE2 waits for EXE1 to terminate, and as soon as it detects this, it deletes EXE1 from disk.
After successfully deleting EXE1, the job of EXE2 is complete, and it exits and terminates, causing the operating system's filehandle to it to close.
The general concept and idea is now that EXE2 should immediately be deleted upon this closing of the last open file handle to it. This is also a seemingly correct assumption, especially when you read the entry for the "FILE_FLAG_DELETE_ON_CLOSE" flag of the CreateFile API in the Win32API reference
Or you can visit the following link : http://www.woodmann.com/forum/archive/index.php/t-4542.html Have a Good Luck...:rose:
Jagdish Bhimbha S/W Developer
-
Hi all, I have made a dialog based application from which i am trying to delete various other files.. my problem is it deletes all the file from the folder but leaves itself and the folder there only.... how can i delete it. Thanks in advance
Hi, If you want to delete the folder after deleting the files. You this syntax. _wrmdir(dirname );
Thanks and Regards. SANTHOSH V
-
Hi all, I have made a dialog based application from which i am trying to delete various other files.. my problem is it deletes all the file from the folder but leaves itself and the folder there only.... how can i delete it. Thanks in advance
VCProgrammer wrote:
my problem is it deletes all the file from the folder but leaves itself and the folder there only.... how can i delete it.
Since your exe is running, it could not be deleted. Better mark it for deletion after next reboot. You can use the api
MoveFileEx()
with flagMOVEFILE_DELAY_UNTIL_REBOOT
. Check here for the code snippet - http://weseetips.com/2008/05/19/how-to-mark-your-file-for-deletion-after-next-reboot/[^] Regards, Jijo._____________________________________________________ http://weseetips.com[^] Visual C++ tips and tricks. Updated daily.
-
An executable cannot delete "itself". You may consider a script, or a batch file which will have all the files of your interest deleted. Here's a batch file, which works for me:
@echo off
sleep 50000
del /q "D:\files\file1.txt"
del /q "D:\files\file2.txt"
del /q "D:\files\MyDialog.exe"
del /q "D:\files\mybatch.bat"
exitWhereas
mybatch.bat
is the batch file itself. Finish the stuff with your dialog and call this batch file with a ShellExecute()[^]. I've added a delay at the beginning of the batch file to make sure that the dialog gets enough time to quit.Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP
Hi Rajesh like yesterday?why you told good answer! :laugh:
-
Hi Rajesh like yesterday?why you told good answer! :laugh:
Ah! I never knew that posting a good answer would get me a 1 vote. I wish the poor 1-voting creature all the happiness he could get. He'll need it to live with his lowly attitude. :) BTW, Thanks for the vote.
Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP
-
Ah! I never knew that posting a good answer would get me a 1 vote. I wish the poor 1-voting creature all the happiness he could get. He'll need it to live with his lowly attitude. :) BTW, Thanks for the vote.
Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP
Rajesh R Subramanian wrote:
Ah! I never knew that posting a good answer would get me a 1 vote
Its not only for you its for some members. :-D
-
Rajesh R Subramanian wrote:
Ah! I never knew that posting a good answer would get me a 1 vote
Its not only for you its for some members. :-D
Like - I belong to some kind of a special members group? :-D
Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP
-
Hi Friend, You should try this trick : There are several tricks for making programs that are in one way or another able to delete themselves, without leaving any traces in the system. One of the most wide-spread techniques, and also the one which is supposed to be one of the most platform independent (between Windows versions) is the following:
Start the program you want to delete (EXE1).
From this program, drop a second exe file (EXE2).
Open a file handle to EXE2 from EXE1, using CreateFile, with the flag "FILE_FLAG_DELETE_ON_CLOSE".
Execute EXE2 from EXE1 (e.g. with CreateProcess), causing the operating system to open another file handle to EXE2.
Let EXE1 exit and terminate (which will implicitly cause its filehandle to EXE2 to close, leaving only the operating system's filehandle to EXE2 left open).
EXE2 waits for EXE1 to terminate, and as soon as it detects this, it deletes EXE1 from disk.
After successfully deleting EXE1, the job of EXE2 is complete, and it exits and terminates, causing the operating system's filehandle to it to close.
The general concept and idea is now that EXE2 should immediately be deleted upon this closing of the last open file handle to it. This is also a seemingly correct assumption, especially when you read the entry for the "FILE_FLAG_DELETE_ON_CLOSE" flag of the CreateFile API in the Win32API reference
Or you can visit the following link : http://www.woodmann.com/forum/archive/index.php/t-4542.html Have a Good Luck...:rose:
Jagdish Bhimbha S/W Developer
Jagdish V. Bhimbha wrote:
Open a file handle to EXE2 from EXE1, using CreateFile, with the flag "FILE_FLAG_DELETE_ON_CLOSE".
This does not work on all platforms. Sometimes
CreateFile()
will returnINVALID_HANDLE_VALUE
andGetLastError()
will returnERROR_ACCESS_DENIED
. This is because the OS supports the ability to share a file with delete access."Love people and use things, not love things and use people." - Unknown
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne