• Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

Batch file programming

Joined
Jun 29, 2006
Messages
230 (0.04/day)
Hello everyone. I've got the following code.

Code:
Loader.exe sumprogram.exe

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

Code:
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

New Member
Joined
Jul 26, 2010
Messages
1,655 (0.33/day)
Location
Philly
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:
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:
Code:
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 :ohwell:). 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.
PHP:
#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) :D

Assuming the aforementioned code was compiled as "waitfor.exe" (and everything was 32-bit), you could include it in your batch like so:
Code:
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? :D

Also, do you have to use "Loader.exe"? If you can just do "start /w sumprogram.exe" this would be a lot easier! :)
 
Last edited:
Joined
Jun 29, 2006
Messages
230 (0.04/day)
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

New Member
Joined
Jul 26, 2010
Messages
1,655 (0.33/day)
Location
Philly
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]
 

Attachments

  • waitfor.exe
    26.8 KB · Views: 472
  • waitfor_src.zip
    1.2 KB · Views: 145
  • sleep.exe
    23.4 KB · Views: 560
  • sleep_src.zip
    443 bytes · Views: 174
Last edited:

PVTCaboose1337

Graphical Hacker
Joined
Feb 1, 2006
Messages
9,501 (1.43/day)
Location
Texas
System Name Whim
Processor Intel Core i5 2500k @ 4.4ghz
Motherboard Asus P8Z77-V LX
Cooling Cooler Master Hyper 212+
Memory 2 x 4GB G.Skill Ripjaws @ 1600mhz
Video Card(s) Gigabyte GTX 670 2gb
Storage Samsung 840 Pro 256gb, WD 2TB Black
Display(s) Shimian QH270 (1440p), Asus VE228 (1080p)
Case Cooler Master 430 Elite
Audio Device(s) Onboard > PA2V2 Amp > Senn 595's
Power Supply Corsair 750w
Software Windows 8.1 (Tweaked)
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.
 
Top