Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Running a batch file from the %temp% folder

34 views
Skip to first unread message

John Gray

unread,
Apr 18, 2018, 2:46:32 AM4/18/18
to
OBJECTIVE: to copy a batch file to the %temp% folder and run it cleanly from there.

EXPLANATION: the batch file is held on a USB Flash Drive and is used to install software on the PC, or copy shortcuts to desktops, or the like.
Copying the batch file to the %temp% folder allows the USB Flash Drive to be removed cleanly (using Uwe Sieber's RemoveDrive.exe) without having to get the user to go through the Safely Remove Hardware manual operation

What follows is the current state of my test batch file. Commands in CAPITALS are for debugging.
Any comments on the method, or suggestions of a better method, would be gratefully received!

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.

@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


John Gray

unread,
Apr 18, 2018, 2:53:43 AM4/18/18
to
### I forgot to warn people that in a _previous_ version of the batch file I managed to create a never-ending succession of Command Prompt windows which required me to reboot my PC! Care in modification is required...

JJ

unread,
Apr 18, 2018, 11:44:19 AM4/18/18
to
On Tue, 17 Apr 2018 23:46:30 -0700 (PDT), John Gray wrote:
>
> The only problem I cannot circumvent is how to close down the STARTed
> child window

CMD instance can not directly interact with each other, so there's no way to
tell the PAUSE command in the other window to continue or cancel itself.

You can close any child batch file window (or any window) using TASKKILL,
but it'll close the window abruptly.

Herbert Kleebauer

unread,
Apr 18, 2018, 3:43:09 PM4/18/18
to
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

John Gray

unread,
Apr 19, 2018, 1:17:10 PM4/19/18
to
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.

Batchman

unread,
Apr 19, 2018, 7:47:18 PM4/19/18
to
John Gray wrote:

> Copying the batch file to the %temp% folder allows the USB Flash Drive to
> be removed cleanly (using Uwe Sieber's RemoveDrive.exe) without having to

First of all thanks for this hint!

> 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.

Perhaps after copying the batch file into the Temp folder...
:: RUN the copy
If exist %temp%~n0.bat %temp%~n0.bat
:: then initial batch file terminates

0 new messages