I'd first like to thank everyone, particularly fox, Steffen, Doug, Squash, and Antonio for all the help as of late. I've learned an incredible amount with all of your suggestions and advice. Thank you!
With that said, I'm just in the final stages with one of my scripts and I was wondering the best way to capture some preliminary path and variable existence checks.
The first portion of my script sets certain paths and script names as variables.
However, I've added some checks in there that will spool to a text file (DNE.txt) if it doesn' exist.
Here it is:
Code: Select all
:BEGIN
echo \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
echo \\ \\
echo \\ WELCOME TO SQL MGMT ! \\
echo \\ \\
echo \\ DOS TIPS \\
echo \\ \\
echo \\ 0 0 0 0 0 \\
echo \\ \/!\/ (=!!=) \/!\/\/!\/ \\
echo \\ / \ /\ /\ / \ / \ \\
echo \\ \\
echo \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
TIMEOUT /T 5 >nul
CLS
echo *********************************************
echo This batch can perform the following actions:
echo ---------------------------------------------
echo.
echo [1] Backup SQL Server Database
echo.
echo [2] Backup local SQL DB and restore locally or remote
echo.
echo [3] Compare ^& synchronize a single SQL Table
echo.
echo [4] Compare ^& synchronize all SQL Tables
echo.
echo [5] Restore SQL DB from *.BAK file
echo.
echo ***************************************************
echo Which of the above options do you want to perform?
echo ---------------------------------------------------
echo.
CHOICE /N /C 12345 /M "Option: "
IF ERRORLEVEL 5 SET INT_OPT=5
IF ERRORLEVEL 4 SET INT_OPT=4
IF ERRORLEVEL 3 SET INT_OPT=3
IF ERRORLEVEL 2 SET INT_OPT=2
IF ERRORLEVEL 1 SET INT_OPT=1
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: I N I T I A L I Z E SQL V A R I A B L E S ::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
CLS
color CF
echo :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
echo.:: ::
echo.::-- Setting Microsoft SQL Server Installation path --:: ::
echo.:: ::
echo ::-- Setting SQL Server Utility paths --:: ::
echo.:: ::
echo ::-- Setting CMD ^& SQL script variables --:: ::
echo :: ::
echo ::-- Please allow 15-20 seconds to complete --:: ::
echo :: ::
echo.:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::-- SET SQLCMD output file --::
::-- Used for all command lines to direct output --::
::-- Option [1][2][3][4][5] --::
SET SQL_OUTPUT=SQL_SERVER_OP.txt
::-- SET SQL Ghosted Users Report output file --::
::-- Option [1][2][3][4][5] --::
SET SQL_GHOST_RPT=SQL_GHOST_RPT.txt
::-- Set SQLCMD SQL script name --::
::-- Option [1] --::
SET SQL_KS=KILLSQLSESSION.sql
::-- Set SQLCMD SQL script name --::
::-- Option [2] --::
SET RSTR_SQL_DB=RSTR_SQL_DB.sql
::-- Set SQLCMD SQL script name --::
::-- Option [3] --::
SET SQLS=EXEC_SQL_SYNC.sql
::-- Set SQLCMD SQL ^& CMD script name --::
::-- Option [4] --::
SET EXEC_TBCA=EXEC_TBLDIFF_CMD_ALL.cmd
SET SQLS2=EXEC_SQL_SYNC_ALL.sql
SET BTCA=BUILD_TBLDIFF_CMD_ALL.sql
::-- SQL Path verification section --::
If exist "%CD%DNE.txt" DEL "%CD%DNE.txt"
::-- Return MSSQL path --::
set "sql="
for %%a in (c d e f g h i j k l m n o p q r s t u v w x y z) do if not defined sql (pushd "%%a:\" & for /d /r %%b in (MSSQL*.MSSQLSERVER) do set "sql=%%b"&set DL=%%a & popd)
if not defined sql (
echo ------------------------------------------------
echo Microsoft SQL Server installation path not found
echo ------------------------------------------------
)>> "%CD%DNE.txt"
if exist "%sql%\MSSQL\" SET MSSQL_PATH=%sql%\MSSQL\
::-- Return fully qualified path for SQLCMD --::
for %%D in (c d e f g h i j k l m n o p q r s t u v w x y z) do if exist %%D: (
for /f "delims=" %%A in ('dir/b/s %%D:\sqlcmd.exe 2^>nul') do (
SET SQL_CMD_PATH=%%A
)
)
If not defined SQL_CMD_PATH echo SQLCMD.exe utility path not found >>"%CD%DNE.txt"
::-- Return fully qualified path for tablediff.exe --::
for %%D in (c d e f g h i j k l m n o p q r s t u v w x y z) do if exist %%D: (
for /f "delims=" %%A in ('dir/b/s %%D:\tablediff.exe 2^>nul') do (
SET TD_CMD_PATH=%%A
)
)
If not defined TD_CMD_PATH (
echo ------------------------------------
echo tablediff.exe utility path not found
echo ------------------------------------
) >>"%CD%DNE.txt"
::-- Check for files before proceeding --::
set DNE=
for %%F in (
%RSTR_SQL_DB%
%SQL_KS%
%BTCA%
) do if not exist %%F (
echo --------------------------------------------------------
echo %%F : Does not exist in current directory
echo --------------------------------------------------------
set DNE=1
)>>"%CD%DNE.txt"
::-- If exists - Display DNE.txt output and exit batch session
If exist "%CD%DNE.txt" start "" notepad.exe "%CD%DNE.txt" & EXIT
::-- Reset color --::
color 0f
CLS
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: E N D SQL I N I T I A L I Z A T I O N ::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
Is there anyway to capture the path checks and script name checks in one fell-swoop? As you can see just a few lines above, I'm already checking for the existence of the three script names, but was hoping to add the PATH checks in there as well.
SQL_CMD_PATH
TD_CMD_PATH
%RSTR_SQL_DB%
%SQL_KS%
%BTCA%
Thank you!