?
Solved

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

Posted on 2015-01-16
3
Medium Priority
?
552 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
[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
  • 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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

800 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