Solved

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

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
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 viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

747 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

18 Experts available now in Live!

Get 1:1 Help Now