# Batch file programming



## boogerlad (Feb 26, 2011)

Hello everyone. I've got the following code.


```
Loader.exe sumprogram.exe
```

but after that, I want to delete the files that it makes after exiting. Would this work?


```
start /wait Loader.exe sumprogram.exe
```

Does anything after the first program (loader.exe) follow the switches of the first program or the start command?


----------



## streetfighter 2 (Feb 26, 2011)

When using _start /w_ it'll only wait for the program in the first argument to quit.  Consequently if your "Loader.exe" quits the second it starts "sumprogram.exe" then the _start /w_ will finish even if "sumprogram.exe" is still running.

So in answer to your question:


boogerlad said:


> Would this work?


No.  I do not believe it would.

If you wanted to use *nearly* pure batch you could poll "sumprogram.exe" until it ends:

```
Loader.exe sumprogram.exe
:again
sleep 500
tasklist /FI "IMAGENAME eq sumprogram.exe" | find "sumprogram.exe"
IF %ERRORLEVEL%==0 GOTO again
<delete crap here>
```
You'd need to obtain a sleep.exe file (mine uses millisecond but the linked one uses seconds and I've never tested it ).  There is a ping method that is similar to sleep.exe, but it's a pile of crap if you ask me.

It would also be fairly simple to write a program in C that you could use in a batch file.

```
#include <windows.h> 
#include <psapi.h> 
#include <stdio.h>

int PrintProcessNameAndID( DWORD processID, const char *name ) 
{ 
     TCHAR szProcessName[MAX_PATH] = TEXT("<unknown>"); 

     // Get a handle to the process. 
     HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION | 
                                 PROCESS_VM_READ, 
                                 FALSE, processID ); 

     // Get the process name. 
     if (NULL != hProcess ) 
     { 
          HMODULE hMod; 
          DWORD cbNeeded; 

          if ( EnumProcessModules( hProcess, &hMod, sizeof(hMod),  
                &cbNeeded) ) 
          { 
               GetModuleBaseName( hProcess, hMod, szProcessName,  
                                      sizeof(szProcessName)/sizeof(TCHAR) ); 
          } 
     } 

     if(strcmp(szProcessName, name) == 0) // right process 
     { 
          CloseHandle(hProcess); 
          return 1; 
     } 

     // Release the handle to the process. 
     CloseHandle( hProcess ); 
     return 0; 
} 

int find(const char *name) 
{ 
     // Get the list of process identifiers. 
     DWORD aProcesses[1024], cbNeeded, cProcesses; 
     unsigned int i; 

     if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) ) 
     { 
          return -1; 
     } 

     // Calculate how many process identifiers were returned. 
     cProcesses = cbNeeded / sizeof(DWORD); 

     // Print the name and process identifier for each process. 
     for ( i = 0; i < cProcesses; i++ ) 
     { 
          if( aProcesses[i] != 0 ) 
          { 
               if(PrintProcessNameAndID( aProcesses[i], name )) 
               { 
                    //found it 
                    return aProcesses[i]; 
               } 
          } 
     }
	 return -1;
} 

int main(int argc, char **argv) 
{ 
     if(argc < 2){ /* Check if we have two parameters on the command line, including the program name */ 
          printf("Usage: Process name required \n");
          exit(1); 
     } 

	 int pid = find(argv[1]);
     if (pid!=-1) 
     { 
          HANDLE hProcess = OpenProcess(STANDARD_RIGHTS_REQUIRED | SYNCHRONIZE, FALSE, (DWORD)pid); 
          if (NULL != hProcess ) 
          { 
               DWORD dwWaitResult = WaitForSingleObject (hProcess, INFINITE);
			   CloseHandle( hProcess );
			   return 0;
          }  
     } 
     return 1; 
}
```
 (some code shamelessly stolen from here) 

Assuming the aforementioned code was compiled as "waitfor.exe" (and everything was 32-bit), you could include it in your batch like so:

```
Loader.exe sumprogram.exe
waitfor.exe sumprogram.exe
<delete crap here>
```
I'm probably over engineering this, but that's what I'm supposed to do right? 

Also, do you have to use "Loader.exe"?  If you can just do "start /w sumprogram.exe" this would be a lot easier!


----------



## boogerlad (Feb 26, 2011)

Wow, thanks for the response. I only know java, but that c code would help me learn it... As for now, I'm using the batch solution. I'll let you know if it works after I get back from work.


----------



## streetfighter 2 (Feb 26, 2011)

Since I wasn't doing anything particularly important I decided to compile the program that I wrote/stole a minute ago.  I had to make a few minor alterations but it's working swell now.  I've included the source with a batch file for compiling with mingw.

Feel free to try it out.

EDIT:  I also included my own personal copy of sleep.exe (which uses milliseconds) and the source to it.

EDIT 2: [highlight]I just realized that Windows 7 has a tool called "waitfor.exe" that's in system path and does something quite different from my "waitfor.exe".  Consequently, if you want to add my "waitfor.exe" to "%path%" you'll need to change it's name on Windows 7.[/highlight]


----------



## PVTCaboose1337 (Mar 4, 2011)

saritayadav said:


> java is better language between c, c++ , you make a good soft were using by java



Really?  Between C and C++?  Lolno.  Better because of a virtual machine?  Maybe.  

Hey, also back on topic, a paradox that computer scientists asked early one was "Can a program tell when it has terminated?"  The answer is no.  You have to use an outside program.  You want to delete a file after exit?  Have to use an external program (which can be made by the original program) in order to delete that file.  Best to use Windows functions to do all that though.


----------

