Batch file: Find if substring is in string (not in a file) Part 2 - Using Variables

29,370

Solution 1

try one:

set "var=abcdefg"
set "search=bcd"
CALL set "test=%%var:%search%=%%"
if "%test%"=="%var%" (echo %search% is not in %var%) else echo %search% in %var% found


set "var=abcdefg"
set "search=bcd"
echo %var%|findstr /lic:"%search%" >nul && echo %search% found || echo %search% not found

Solution 2

The solution is to use FindStr and the NULL redirect, >nul.

SET var=%1
SET searchVal=Tomcat
SET var|FINDSTR /b "var="|FINDSTR /i %searchVal% >nul
IF ERRORLEVEL 1 (echo It does't contain Tomcat) ELSE (echo It contains Tomcat)

Save as test.bat and execute with the parameter to be searched, as follows: test Tomcat7

C:\>test Tomcat9
It contains Tomcat
Share:
29,370
MAbraham1
Author by

MAbraham1

Software/Web Developer with SHR Consulting Group of Springfield, VA for DoD/DISA website projects. Past projects include: Architected & developed the Aircraft Status Board, using HTML5, JavaScript, JQuery, AlpacaJS, and Java Servlets for back-end communications. This was developed in an Agile SDLC in < 60 days, and became the JASDF home page. Awarded funds and briefed NGTS President results of HMMVW-Augmentation Project • Developed a web app maintenance suite to manage data and component software/firmware versions for Navy E-2D Advanced Hawkeye aircraft under NAVAIR (PMA-231). Client uses AJAX/HTML5, JQuery/JSF/RichFaces/Tomcat. Developed a faults-to-publications maint. tool using a Java client front-end and SQL Server 2012 database back-end. Developed aircraft fault aggregation & prognostics BIT DB for Navy squadrons. Migrated to PostgreSQL and Derby. Developed web-based configuration-management report using JAXB, XML, XSLT, XPath, and Apache Tomcat. Developed data-access layers (Java, JDBC, XML, Eclipse) Complex GUI/data interface (ASP.NET/C#, LINQ, Oracle, Visual Studio) Planning data architecture using Enterprise Architect, Visio, and Excel Patent for Diesel Engine after treatment User Interface (Algorithm Developer; US7793492) Technologies: Java/JSF/J2EE, Apache Tomcat, MySQL, MS SQL Server, Oracle, H2, Derby DB, PostgreSQL, Sybase, and of course MS Access. This amazing photo shows Erik and me atop Skeleton Point on the South Kaibab Trail, overlooking God's grand creation, including the mighty Colorado River! We backpacked on the bottom of the Canyon's main corridor for 4 days and 3 nights last May 2017 My wife and family live in Indianapolis, Indiana, USA. In my spare time, I am involved in an active family, church leadership, Boy Scouts and a pianist/composer.

Updated on July 09, 2022

Comments

  • MAbraham1
    MAbraham1 almost 2 years

    In a Windows batch file, I have a string 'abcdefg'. I want to check if 'bcd' is in the string, but I also want each to be in a variable, or pass in a parameter for the string.

    This solution comes close, but uses constants rather than variables. Batch file: Find if substring is in string (not in a file)

  • Chef Pharaoh
    Chef Pharaoh about 9 years
    The second answer here helped me out, I had square brackets in the string and managed to get things working with this answer.