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

Batch file programming

Discussion in 'Programming & Webmastering' started by boogerlad, Feb 26, 2011.

  1. boogerlad

    Joined:
    Jun 29, 2006
    Messages:
    219 (0.07/day)
    Thanks Received:
    12
    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?
     
  2. streetfighter 2

    streetfighter 2 New Member

    Joined:
    Jul 26, 2010
    Messages:
    1,658 (1.07/day)
    Thanks Received:
    732
    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:
    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 PrintProcessNameAndIDDWORD processID, const char *name 

         
    TCHAR szProcessName[MAX_PATH] = TEXT("<unknown>"); 

         
    // Get a handle to the process. 
         
    HANDLE hProcess OpenProcessPROCESS_QUERY_INFORMATION 
                                     
    PROCESS_VM_READ
                                     
    FALSEprocessID ); 

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

              if ( 
    EnumProcessModuleshProcess, &hModsizeof(hMod),  
                    &
    cbNeeded) ) 
              { 
                   
    GetModuleBaseNamehProcesshModszProcessName,  
                                          
    sizeof(szProcessName)/sizeof(TCHAR) ); 
              } 
         } 

         if(
    strcmp(szProcessNamename) == 0// right process 
         

              
    CloseHandle(hProcess); 
              return 
    1
         } 

         
    // Release the handle to the process. 
         
    CloseHandlehProcess ); 
         return 
    0


    int find(const char *name

         
    // Get the list of process identifiers. 
         
    DWORD aProcesses[1024], cbNeededcProcesses
         
    unsigned int i

         if ( !
    EnumProcessesaProcessessizeof(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 ( 0cProcessesi++ ) 
         { 
              if( 
    aProcesses[i] != 
              { 
                   if(
    PrintProcessNameAndIDaProcesses[i], name )) 
                   { 
                        
    //found it 
                        
    return aProcesses[i]; 
                   } 
              } 
         }
         return -
    1;


    int main(int argcchar **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 SYNCHRONIZEFALSE, (DWORD)pid); 
              if (
    NULL != hProcess 
              { 
                   
    DWORD dwWaitResult WaitForSingleObject (hProcessINFINITE);
                   
    CloseHandlehProcess );
                   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: Feb 26, 2011
  3. boogerlad

    Joined:
    Jun 29, 2006
    Messages:
    219 (0.07/day)
    Thanks Received:
    12
    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.
     
  4. streetfighter 2

    streetfighter 2 New Member

    Joined:
    Jul 26, 2010
    Messages:
    1,658 (1.07/day)
    Thanks Received:
    732
    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]
     

    Attached Files:

    Last edited: Feb 27, 2011
    boogerlad says thanks.
  5. PVTCaboose1337

    PVTCaboose1337 Graphical Hacker

    Joined:
    Feb 1, 2006
    Messages:
    9,512 (2.99/day)
    Thanks Received:
    1,143
    Location:
    San Antonio, Texas
    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.
     
    streetfighter 2 says thanks.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guest)

Share This Page