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?

SRel
  • 383
  • 1
  • 11
  • 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"`! – urbanSoft Dec 02 '19 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 – jeb Dec 02 '19 at 10:28
  • You can use [tag:find] or [tag:findstr] to determine if a string contains a substring. – Compo Dec 02 '19 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 – SRel Dec 02 '19 at 11:11

2 Answers2

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.

urbanSoft
  • 686
  • 6
  • 14
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
Gerhard
  • 22,678
  • 7
  • 27
  • 43