While trying to create a batch file that runs a SQL script by calling out to sqlcmd.exe, I'm trying to get the batch file to detect for the presence of the exe and add it to %PATH% automatically before executing. I can guarantee that it will be somewhere on the host but not where.
Looking at the many examples on SO, this suggests that the most robust way of detecting if string A is in string B is to echo out and pipe to findstr to account for case insensitivity, etc.
With this in mind, I have the following in my batch file;
@echo off
REM Ensure we can find the sql interpreter
echo Checking for SqlCmd.exe, please wait...
setlocal EnableDelayedExpansion
for /f "delims=" %%F in ('dir "%programfiles%\sqlcmd.exe" /s/b') do (
set filepath=%%~dpF
echo "Found SQL interpreter in: !filepath!"
REM is it part of the PATH already?
echo %path% | findstr /c:"!filepath!" 1>nul
if errorlevel 1 (
echo "SqlCmd is not part of the current ENVAR PATH, setting..."
set path=%path%;!filepath!
)
)
And has the following output;
Checking for SqlCmd.exe, please wait...
\Microsoft was unexpected at this time.
Despite the text Found SQL interpreter in: not appearing in the output, the actual issue seems to be in the next executable line (tested by removing it and running):
echo %path% | findstr /c:"!filepath!" 1>nul
It seems to be echoing out the whole line and executing rather than echoing out the %path% and piping to findstr. I've tried adding quotes to no effect.
I'm almost there but missing an encoding step/tweak to get this to work.
Update 1
The PATH variable contents on my test machine is;
C:\Program Files\Common Files\Oracle\Java\javapath;C:\Program Files\Microsoft MPI\Bin\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files (x86)\Microsoft SQL Server\150\Tools\Binn\;C:\Program Files\Microsoft SQL Server\150\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server\150\DTS\Binn\;C:\Program Files\Microsoft SQL Server\150\DTS\Binn\;C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\170\Tools\Binn\;C:\Program Files\Microsoft SQL Server\130\Tools\Binn\;C:\Program Files\dotnet\;C:\ProgramData\chocolatey\bin;C:\Program Files (x86)\Windows Kits\10\Windows Performance Toolkit\;C:\Program Files\Git\cmd;C:\Users\User\.dotnet\tools;C:\Program Files\Java\jdk-17.0.3.1\bin;C:\Users\User\AppData\Local\Programs\Microsoft VS Code\bin;C:\Program Files\Azure Data Studio\bin
Microsoft-containing directorynames from yourpathso we can apply practice in place of theory. Some problems with your code:filepathwill have a terminal backslash.Pathelements will usually be of the formd:\dirname;ord:\dirname\;& the last element may not be terminated by;.Echoing%path%;would seem a good idea, as would /c:"!filepath!;" /c:"!filepath:~0,-1!;" in the 'findstr`.set "var=value"for setting string values - this avoids problems caused by trailing spaces. Don't assign"or a terminal backslash or Space. Build pathnames from the elements - counterintuitively, it is likely to make the process easier.setlocal EnableDelayedExpansionor modified tosetlocal EnableExtensions DisableDelayedExpansionas delayed variable expansion is not really needed here on removing nextset filepath=%%~dpFand use everywhere instead of!filepath!%%~dpF. There is a space missing in thedircommand line between/sand/b.cmd.exedetects that syntax error after several file system accesses and automatically corrects the error, but better is using the correct syntax with a space between each argument passed to command DIR.echo "%path%" | %SystemRoot%\System32\findstr.exe /I /C:"%%~dpF" 1>nulbecause of%path%expands to a string containing)which is interpreted outside of a double quoted argument string as FOR command block closing and not)at end of the posted batch file which is the cause for the error message.echo "%path%"is in real no solution onPATHcontains a folder path enclosed in"because of containing a semicolon. That is fortunately very rare. Better would bepath | %SystemRoot%\System32\findstr.exe /I /L /C:"%%~dpF" 1>nulaspathis also a command and not only a variable.