Reputation: 1746
I've created a bath file to add firewall rules for sql server... here is a part of the script
set PROGRAM="%ProgramFiles%\Microsoft SQL Server\MSSQL11.DBSERVER\MSSQL\Binn\sqlservr.exe"
set RULE_NAME="SQL Server"
echo Checking firewall rule %RULE_NAME%.
netsh advfirewall firewall show rule name=%RULE_NAME% >nul
if not ERRORLEVEL 1 (
echo Rule %RULENAME% already exist.
) else (
echo Rule %RULENAME% not exist. Creating...
netsh advfirewall firewall add rule name=%RULE_NAME% dir=in action=allow program=%PROGRAM%
netsh advfirewall firewall add rule name=%RULE_NAME% dir=out action=allow program=%PROGRAM%
)
I am talking about the first line
set PROGRAM="%ProgramFiles%\Microsoft SQL Server\MSSQL11.DBSERVER\MSSQL\Binn\sqlservr.exe"
Is there any way I can locate the file without typing the path? as The file sqlservr.exe is located differently in different OS and dependent upon the instance name...
Upvotes: 0
Views: 265
Reputation: 30113
A bit tricky:
@ECHO OFF >NUL
SETLOCAL EnableExtensions
rem EnableDelayedExpansion
set "ss=sqlservr.exe"
rem set "ss=GoogleUpdate.exe" my testing value
for /F "tokens=2 delims==" %%G in ('
wmic Service where "pathname like '%%%ss%%%'" get PathName/value^|find /I "%ss%"
') do (
@rem echo debugging G "%%~G"
for /F delims^=^" %%x in ("%%G") do (
set "_program=%%~x"
@rem echo debugging x "%%~x"
)
)
rem next two lines merely for debugging to check up results
set _program
echo "%_program%"
Here the for
loops are
%%G
to retrieve the PathName
value;%%x
to remove the ending carriage return in the value returned (wmic
behaviour: each output line ends with 0x0D0D0A
instead of common 0x0D0A
).See Dave Benham's WMIC
and FOR /F
: A fix for the trailing <CR>
problem
Note the delims^=^"
trick to treat a double quote character as a delimiter.
Upvotes: 1