Solved

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

Posted on 2015-01-16
3
476 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 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

Industry Leaders: 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!

Question has a verified solution.

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

Suggested Solutions

For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

726 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