Forums before death by AOL, social media and spammers... "We can't have nice things"
|    alt.msdos.batch.nt    |    Fun with Windows NT batch files    |    68,980 messages    |
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
|    Message 67,187 of 68,980    |
|    John Gray to Herbert Kleebauer    |
|    Re: Running a batch file from the %temp%    |
|    19 Apr 18 10:17:09    |
   
   From: jgqued@gmail.com   
      
   On Wednesday, 18 April 2018 20:43:09 UTC+1, Herbert Kleebauer wrote:   
   > On 18.04.2018 08:46, John Gray wrote:   
   >   
   > > The only problem I cannot circumvent is how to close down the STARTed   
   > > child window - the final line of the batch file does not cause this to   
   happen.   
   > > commands in CAPITALS are for debugging.   
   >   
   > Instead of   
   >   
   > cd /d %temp% & start "" %~n0.bat   
   >   
   > use:   
   >   
   > cd /d %temp% & start "" cmd /c %~n0.bat   
   >   
   > or:   
   >   
   > cd /d %temp% & %~n0.bat   
   >   
   >   
   > In the second case you have to avoid the () in the   
   > if statement:   
   >   
   > if /i "%cd%"=="%temp%" goto :temp   
   > REM we are running from original location %cd%   
   > copy %~n0.bat %temp%   
   > REM copy RemoveDrive.exe %temp%   
   > cd /d %temp% & %~n0.bat   
   > :: ECHO Finishing the original BATch file...   
   > :: PING -n 21 127.0.0.1 > nul   
   > :: endlocal & exit /b 0   
   >   
   > :temp   
   > :: we reach here if running the copied BATch file   
   >   
   > > @echo off   
   > > :: this BATch file called, arbitrarily, $RunOnTemp.bat   
   > > :: is copied to %temp% and run from there   
   > > setlocal   
   > > ECHO Hello from the top of the BATch file %~n0.bat!   
   > > ECHO %%0 is %0   
   > > ECHO %%cd%% is %cd%   
   > > ECHO %%temp%% is %temp%   
   > > PAUSE   
   > > :: determine whether we are running in   
   > > :: * the original BATch file, or   
   > > :: * its copy in the %%temp%% folder   
   > > if /i not "%cd%"=="%temp%" (   
   > > REM we are running from original location %cd%   
   > > copy %~n0.bat %temp%   
   > > REM copy RemoveDrive.exe %temp%   
   > > cd /d %temp% & start "" %~n0.bat   
   > > ECHO Finishing the original BATch file...   
   > > PING -n 21 127.0.0.1 > nul   
   > > endlocal & exit /b 0   
   > > )   
   > > :: we reach here if running the copied BATch file   
   > > ECHO the required BATch file work is done below   
   > > setlocal enabledelayedexpansion   
   > > ECHO.   
   > > ECHO etc, etc...   
   > > ECHO.   
   > > ECHO Hello at end of BATch file %~n0.bat   
   > > PAUSE   
   > > endlocal   
   > > :: this EXIT command does not cause the child   
   > > :: Command Prompt window to terminate.   
   > > :: Neither does goto :eof WHY?   
   > > exit /b 0   
   > >   
   > >   
      
   Thanks to both of you for your suggestions!   
      
   I was working on a TASKLIST + TASKKILL method of terminating the started task,   
   and ended up with:   
    :: the title can be any arbitrary string, which then appears in the list of   
   tasks   
    title %~n0   
    set forcmds=tasklist /v /nh /fi "imagename eq cmd.exe" ^^^| findstr /i   
   /c:"%~n0"   
    :: the PID appears as the second tasklist output field   
    for /f "tokens=2" %%a in ('%forcmds%') do taskkill /pid %%a   
      
   This works quite well, but Herbert's suggestion of   
    cd /d %temp% & start "" cmd /c "%~n0.bat"   
   means that I can use   
    exit /b 0   
   or   
    goto :eof   
   as the final statement in the batch file, and have this close the started task.   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   
|
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
(c) 1994, bbs@darkrealms.ca