0

I have tried to use the answer mentioned from here: [Find Substring in String] (Batch file: Find if substring is in string (not in a file))

I try to adapt the solution mentiones in the commands, so that I have my SearchVal saved inside a variable so this can be changed during runtime.

Minimal example:

set searchVal="cde"
set str1="abcdef"
setlocal enabledelayedexpansion
if not "x!str1:%searchVal%=!"=="x%str1%" echo It contains my subs
endlocal
pause

In my opinion this little batch should display that the strings contains my subs, however nothing is shown and I do not know why as I directly make use of the solution that should be working.

EDIT

Thanks to the commands I found my mistake. In my current situation I look at files inside a folder and save the filename inside an array while doing a for-loop:

for /f "tokens=1 delims=" %%G in ('pathToFolder\*.properties /b') do (
if not "%%~G:%searchVal%=!"=="%%~G" echo It contains my subs !ID_Properties!
set filename[!ID_Properties!]=%%~G
set /a ID_Properties+=1
)

... where ID_properties is just a counter and searchVal my string I am looking for. Does anyone know how I can use the %%G inside the loop in the correct way so the search works as before?

4
  • 3
    You're super close! Remove the " in your set var otherwise those will be part of you resulting searchVal variable. And str1 does not contain "cde"! Commented Dec 2, 2019 at 10:22
  • 2
    Or even better use the extended set-syntax set "searchVal=cde". The first quote is before the variable name. This ensures that no trailing spaces are part of the variable. And forget about "x..." == "x..." it's superfluous, use "!str1:%searchval%=!"=="!str1!" instead Commented Dec 2, 2019 at 10:28
  • You can use find or findstr to determine if a string contains a substring. Commented Dec 2, 2019 at 10:40
  • I added my question as I did not get it to work while being in a loop. Thanks @urbanSoft and jeb outside my loop this is working Commented Dec 2, 2019 at 11:11

2 Answers 2

3

Your for-loop syntax is not correct it seems like a mixture between executing a dir command and looping through files. I'll stick with the dir command option and using usebackq.

@echo off
setlocal EnableDelayedExpansion

set searchVal=cde
set ID_Properties=0
for /f "usebackq tokens=1 delims=" %%G in (`dir pathToFolder\*.properties /b`) do (
    set file=%%G
    if not "!file:%searchVal%=!"=="!file!" (
      echo It contains my subs !ID_Properties!
      set filename[!ID_Properties!]=!file!
      set /a ID_Properties+=1
    )
)

Filling the array is only done if the file contains your searchVal; don't know if this was/is you intention.

Sign up to request clarification or add additional context in comments.

Comments

0

a For loop might be a bit much, depending on what you plan to do with the output. findstr is maybe a shorter option:

findstr /im "cde" *.properties && echo - found || echo not found

and add /s if you require recursive search through subdirectories:

findstr /ims "cde" *.properties && echo - found || echo not found

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.