Jump to content

Batch Script Env Variables


Recommended Posts

Have a short batch script to install stand alone programs that are zipped.

Install directorys, program name, creation of shortcut to exe are all variables

so I can reuse the script by changing just a few lines at the top.

Problem comes in when I try to run another script like this, it seems like the

cmd shell environment needs to be cleaned up after each call because it

looks to me like the variables are lingering from running the previous

script causing the variables to have incorrect values. I think if I am right

about the problem I need the call to start a new cmd shell each time.

Any suggestions are appreciated. If I run the scripts one at a time no problems.

@set TCLK=tclock2-inst.bat

@set WROL=winroll-inst.bat

@set GV63=gvim63-inst.bat

@set GVRI=gvim63-reg-import.bat

@set CVRC=copy-vimrc-home.bat

@call %TCLK% 2>&1 >> %TEMP%\%TCLK%

@call %WROL% 2>&1 >> %TEMP%\%WROL%

@call %GV63% 2>&1 >> %TEMP%\%GV63%

@call %GVRI% 2>&1 >> %TEMP%\%GVRI%

@call %CVRC% 2>&1 >> %TEMP%\%CVRC%

Here is a sample script --

@cls

@echo ----------------------------------------

@set SCRIPTNAME=tclock2-inst.bat

@rem echo %SCRIPTNAME%

@set DRV=%HOMEDRIVE%

@set INFILE1=tclock2_120.zip

@set TARGDIR=\tclock2_120

@set TARGEXE=tclock2.exe

@set SCLNK=tclock2.lnk

@set M1PTH=\tool

@set M1SD1PTH=%M1PTH%\system

@set DRVM1SD1PTH=%DRV%%M1SD1PTH%

@rem set INSTPTH=%DRVM1SD1PTH%

@set INSTPTH=%DRVM1SD1PTH%%TARGDIR%

@set TARGPTH=%DRVM1SD1PTH%%TARGDIR%

@echo %M1SD1PTH%

@echo %DRVM1SD1PTH%

@echo %TARGPTH%

@rem -- should not have to change anything below this line --

@rem ------------------------------------------------------------

@set smdir=%DRV%\Documents and Settings\All Users\Start Menu

@set smpdir=%DRV%\Documents and Settings\All Users\Start Menu\Programs

@set smpsudir=%smpdir%\Startup

@rem echo %smdir%

@rem echo %smpdir%

@rem echo %smpsudir%

@set M1SD1SCPTH=%smpdir%%M1SD1PTH%

@set SUSCPTH=%smpsudir%

@echo %M1SD1SCPTH%

@echo %SUSCPTH%

@set LCLSC=shortcut.exe

@set LCLUNZIP=unzip.exe

@rem ------------------------------------------------------------

@echo ----------------------------------------

@rem to test only shortcuts, use the next statement

@rem goto :SHORTCUT

@rem ------------------------------------------------------------

@if not exist %LCLSC% (

echo Error: missing file -- %LCLSC%

goto :DOSEXIT )

@if not exist %LCLUNZIP% (

echo Error: missing file -- %LCLUNZIP%

goto :DOSEXIT )

@rem ------------------------------------------------------------

@if not exist %INFILE1% (

echo Error: missing file -- %INFILE1%

goto :DOSEXIT )

@if not exist %INSTPTH% (

mkdir %INSTPTH%

@if ERRORLEVEL 1 (

echo Error: mkdir %INSTPTH% failed

goto :DOSEXIT ) )

@echo copy %INFILE1% %INSTPTH%

@ copy %INFILE1% %INSTPTH%

@if ERRORLEVEL 1 (

echo Error: copy %INFILE1% to %INSTPTH% failed

goto :DOSEXIT )

@echo ----------------------------------------

@echo %LCLUNZIP% -o -d %INSTPTH% %INSTPTH%\%INFILE1%

@ %LCLUNZIP% -o -d %INSTPTH% %INSTPTH%\%INFILE1%

@if ERRORLEVEL 1 (

echo Error: unzip %INFILE1% to %INSTPTH% failed

goto :DOSEXIT )

@rem to test all but shortcuts, use the next statement

@rem goto :DOSEXIT

@rem ------------------------------------------------------------

:SHORTCUT

@echo ----------------------------------------

@echo %LCLSC% /F:"%M1SD1SCPTH%\%SCLNK%" /A:C /T:"%TARGPTH%\%TARGEXE%"

@ %LCLSC% /F:"%M1SD1SCPTH%\%SCLNK%" /A:C /T:"%TARGPTH%\%TARGEXE%"

@if ERRORLEVEL 1 (

echo Error: Shortcut failed %M1SD1SCPTH%\%SCLNK%

goto :DOSEXIT )

@echo ----------------------------------------

@echo %LCLSC% /F:"%SUSCPTH%\%SCLNK%" /A:C /T:"%TARGPTH%\%TARGEXE%"

@ %LCLSC% /F:"%SUSCPTH%\%SCLNK%" /A:C /T:"%TARGPTH%\%TARGEXE%"

@if ERRORLEVEL 1 (

echo Error: Shortcut failed %SUSCPTH%\%SCLNK%

goto :DOSEXIT )

@rem ------------------------------------------------------------

@del %INSTPTH%\%INFILE1%

@if exist %INSTPTH%\%INFILE1% (

echo Error: %INSTPTH\INFILE1% del cmd failed )

@rem ------------------------------------------------------------

@echo off

@rem these must be the final lines in the script

:DOSEXIT

@echo ----------------------------------------

@echo if Error: recheck -- %SCRIPTNAME%

Link to comment
Share on other sites


Be nice put use the code tags for you posts please.

2 things.

1. @echo off - will prevent the lines from being displayed, saves putting an @ in froint of each line. Echo lines will displayed their associated text. edit:(dang un4given1 beat me to it)

2. SetLocal & Endlocal - added to the beginning andend of the called script will make all variables in the script local to itself.

@echo off

set TCLK=tclock2-inst.bat
set WROL=winroll-inst.bat
set GV63=gvim63-inst.bat
set GVRI=gvim63-reg-import.bat
set CVRC=copy-vimrc-home.bat

call %TCLK% 2>&1 >> %TEMP%\%TCLK%
call %WROL% 2>&1 >> %TEMP%\%WROL%
call %GV63% 2>&1 >> %TEMP%\%GV63%
call %GVRI% 2>&1 >> %TEMP%\%GVRI%
call %CVRC% 2>&1 >> %TEMP%\%CVRC%

Here is a sample script --

@echo off
Setlocal
cls
echo ----------------------------------------
set SCRIPTNAME=tclock2-inst.bat

echo %SCRIPTNAME%
set DRV=%HOMEDRIVE%
set INFILE1=tclock2_120.zip

set TARGDIR=\tclock2_120
set TARGEXE=tclock2.exe
set SCLNK=tclock2.lnk

set M1PTH=\tool
set M1SD1PTH=%M1PTH%\system

set DRVM1SD1PTH=%DRV%%M1SD1PTH%
rem set INSTPTH=%DRVM1SD1PTH%
set INSTPTH=%DRVM1SD1PTH%%TARGDIR%
set TARGPTH=%DRVM1SD1PTH%%TARGDIR%

echo %M1SD1PTH%
echo %DRVM1SD1PTH%
echo %TARGPTH%

rem -- should not have to change anything below this line --
rem ------------------------------------------------------------
set smdir=%DRV%\Documents and Settings\All Users\Start Menu
set smpdir=%DRV%\Documents and Settings\All Users\Start Menu\Programs
set smpsudir=%smpdir%\Startup
rem echo %smdir%
rem echo %smpdir%
rem echo %smpsudir%

set M1SD1SCPTH=%smpdir%%M1SD1PTH%
set SUSCPTH=%smpsudir%
echo %M1SD1SCPTH%
echo %SUSCPTH%

set LCLSC=shortcut.exe
set LCLUNZIP=unzip.exe

rem ------------------------------------------------------------

echo ----------------------------------------
rem to test only shortcuts, use the next statement
rem goto :SHORTCUT


rem ------------------------------------------------------------
if not exist %LCLSC% (
echo Error: missing file -- %LCLSC%
goto :DOSEXIT )

if not exist %LCLUNZIP% (
echo Error: missing file -- %LCLUNZIP%
goto :DOSEXIT )

rem ------------------------------------------------------------
if not exist %INFILE1% (
echo Error: missing file -- %INFILE1%
goto :DOSEXIT )

if not exist %INSTPTH% (
mkdir %INSTPTH%
if ERRORLEVEL 1 (
echo Error: mkdir %INSTPTH% failed
goto :DOSEXIT ) )

echo copy %INFILE1% %INSTPTH%
copy %INFILE1% %INSTPTH%
if ERRORLEVEL 1 (
echo Error: copy %INFILE1% to %INSTPTH% failed
goto :DOSEXIT )

echo ----------------------------------------
echo %LCLUNZIP% -o -d %INSTPTH% %INSTPTH%\%INFILE1%
%LCLUNZIP% -o -d %INSTPTH% %INSTPTH%\%INFILE1%
if ERRORLEVEL 1 (
echo Error: unzip %INFILE1% to %INSTPTH% failed
goto :DOSEXIT )

rem to test all but shortcuts, use the next statement
rem goto :DOSEXIT

rem ------------------------------------------------------------
:SHORTCUT

echo ----------------------------------------
echo %LCLSC% /F:"%M1SD1SCPTH%\%SCLNK%" /A:C /T:"%TARGPTH%\%TARGEXE%"
%LCLSC% /F:"%M1SD1SCPTH%\%SCLNK%" /A:C /T:"%TARGPTH%\%TARGEXE%"
if ERRORLEVEL 1 (
echo Error: Shortcut failed %M1SD1SCPTH%\%SCLNK%
goto :DOSEXIT )

echo ----------------------------------------
echo %LCLSC% /F:"%SUSCPTH%\%SCLNK%" /A:C /T:"%TARGPTH%\%TARGEXE%"
%LCLSC% /F:"%SUSCPTH%\%SCLNK%" /A:C /T:"%TARGPTH%\%TARGEXE%"
if ERRORLEVEL 1 (
echo Error: Shortcut failed %SUSCPTH%\%SCLNK%
goto :DOSEXIT )

rem ------------------------------------------------------------
del %INSTPTH%\%INFILE1%
if exist %INSTPTH%\%INFILE1% (
echo Error: %INSTPTH\INFILE1% del cmd failed )

rem ------------------------------------------------------------
echo off
rem these must be the final lines in the script
:DOSEXIT
echo ----------------------------------------
echo if Error: recheck -- %SCRIPTNAME%
endlocal

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...