Solved

C++ Bat file to self-delete (update)

Posted on 2015-01-16
3
425 Views
Last Modified: 2015-01-17
Hello!

How are you?

I'm trying to do a self-update function.

I already did my copy with other name.

I'm trying to use a bath file when i close my app to delete this one.

void Window::Win::SelfDelete(){

	std::string filename = "bat.bat";

	//my app
	char myApp[MAX_PATH];
	GetModuleFileName(0, myApp, MAX_PATH);
	//bat
	char dir[MAX_PATH];
	GetCurrentDirectory(MAX_PATH, dir);
	std::string finalDir = std::string(dir) + "\\bat.bat";

	std::string bat;
	bat += ":Repeat ";
	bat += "del " + std::string(myApp);
	bat += " if exist " + std::string(myApp) + " goto Repeat ";
	bat += " del " + finalDir;

	std::ofstream file(filename, std::ios::out);
	if (file.is_open()){
		file << bat;
		file.close();
	}

	ShellExecute(NULL, "open", finalDir.c_str(), NULL, NULL, SW_HIDE);
}

Open in new window


I'm not understand i need to do. I'm not good with bath files.

I'm executing it here:

case WM_CLOSE:{
		Window::Win::Inst().SelfDelete();
		DestroyWindow(hWnd);		
		break;
	}

Open in new window


I want to delete the actual program and self-delete the bath after.

Ty for you attention and help.

bye
0
Comment
Question by:Júlio
  • 2
3 Comments
 

Author Comment

by:Júlio
ID: 40553387
I changed to this:

void Window::Win::SelfDelete(){

	std::string filename = "bat.bat";

	//my app
	char myApp[MAX_PATH];
	GetModuleFileName(0, myApp, MAX_PATH);
	//bat
	char dir[MAX_PATH];
	GetCurrentDirectory(MAX_PATH, dir);
	std::string finalDir = std::string(dir) + "\\bat.bat";

	std::ofstream file(filename, std::ios::out);
	if (file.is_open()){
		file << ":Repeat" << std::endl;
		file << "del " + std::string(myApp) << std::endl;
		file << "if exist " + std::string(myApp) + " goto Repeat " << std::endl;
		file << "del " + finalDir << std::endl;
		file.close();
	}

	ShellExecute(NULL, "open", finalDir.c_str(), NULL, NULL, SW_HIDE);
}

Open in new window


The problem is that now the bat file not self-deleting.
i worked with bath files like 15 years ago, i'm reading everything again to try to do it.

the "del %0" is not working for me too.
0
 

Author Comment

by:Júlio
ID: 40553554
Now it's working:

void Window::Win::SelfDelete(){

	std::string filename = "bat.bat";
	//my app
	char myApp[MAX_PATH];
	GetModuleFileName(0, myApp, MAX_PATH);
	std::string::size_type pos = std::string(myApp).find_last_of("\\");
	std::string myAppFileName = std::string(myApp).substr(pos+1, std::string(myApp).length());
	////bat
	char dir[MAX_PATH];
	GetCurrentDirectory(MAX_PATH, dir);
	std::string finalDir = std::string(dir) + "\\bat.bat";

	std::ofstream file(filename, std::ios::out);
	if (file.is_open()){
		file << ":Repeat" << std::endl;
		file << "del " + myAppFileName << std::endl;
		file << "if exist " + myAppFileName + " goto Repeat " << std::endl;
		file << "del ""%0"" ";
		file.close();
	}

	ShellExecute(NULL, "open", finalDir.c_str(), NULL, NULL, SW_HIDE);
}

Open in new window


If someone have a better solution, please tell me.
0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 40553641
Jeffrey Richter wrote a self-deleting executable for MSDN Mag a while ago (without using a batch file) - that's how he did it:

#include <Windows.h>
#include <stdlib.h>
#include <tchar.h>

/////////////////////////////////////////////////////////////////////

int WINAPI WinMain(HINSTANCE h, HINSTANCE h2, LPSTR psz, int n) {

   // Is this the original EXE ther Clone?
   // The original EXE receives one argument,
   // the clone more.
   if (__argc == 1) {

      // Original EXE: Start the Clone.

      // Copy EXE to the temporary directory
      TCHAR szPathOrig[_MAX_PATH], szPathClone[_MAX_PATH];
      GetModuleFileName(NULL, szPathOrig, _MAX_PATH);
      GetTempPath(_MAX_PATH, szPathClone);
      GetTempFileName(szPathClone, __TEXT("Del"), 0, szPathClone); 
      CopyFile(szPathOrig, szPathClone, FALSE);

      // Open the Clone using FILE_FLAG_DELETE_ON_CLOSE
      HANDLE hfile = CreateFile(szPathClone, 0, FILE_SHARE_READ, NULL,
                           OPEN_EXISTING,FILE_FLAG_DELETE_ON_CLOSE);

      // Start the Clone. Pass the PID and the fully qualified path
      // of the original file
      TCHAR szCmdLine[512];
      HANDLE hProcessOrig = OpenProcess(SYNCHRONIZE, TRUE,
                                        GetCurrentProcessId());
      wsprintf(szCmdLine, __TEXT("%s %d \"%s\""), szPathClone, 
               hProcessOrig, szPathOrig);
      STARTUPINFO si;
      ZeroMemory(&si, sizeof(si));
      si.cb = sizeof(si);
      PROCESS_INFORMATION pi;
      CreateProcess(NULL, szCmdLine, NULL, NULL, TRUE, 0, 
                    NULL, NULL, &si, &pi);
      CloseHandle(hProcessOrig);
      CloseHandle(hfile);

      // now the original process may terminate
      } else {

      // Clone-EXE: Delete the  original EXE after termination
      HANDLE hProcessOrig = (HANDLE) _ttoi(__targv[1]);
      WaitForSingleObject(hProcessOrig, INFINITE);
      CloseHandle(hProcessOrig);
      DeleteFile(__targv[2]);
   }
   return(0);
}

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Socket Programming (Unix) 8 118
C++ error cannot convert from std::string to const char * 6 124
Add values of each row in an array 3 50
Path to  STL Map header file 1 41
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

912 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

24 Experts available now in Live!

Get 1:1 Help Now