0

I read each line in the file1.txt file and parse it against the = character. The problem is that the extracted word has a space at the end and findstr doesn't find the searched word in file2.txt because of this. I tried to use: set g=%g: =% to remove the space, but it doesn't work. Can someone help me please?

for /f "tokens=*" %%a in (file1.txt) do (
  for /f "tokens=1,2 delims==" %%g in ("%%a") do (
    if [%%h] == [] (
        echo %%g 
    ) else (
        FOR /F "tokens=*" %%i IN ('findstr /c:"%%g" file2.txt') do (echo %%i)
    )
  ) 
)
IGP
  • 37
  • 7
  • 1
    You need to invoke delayedexpansion [hundreds of SO articles about that - use the search feature] in order to display or use the run-time value of any variable that's changed within a parenthesised series of instructions (aka "code block"). [Stephan's DELAYEDEXPANSION link](https://stackoverflow.com/a/30284028/2128947) hence `set g=%g: =%` will use the value that `g` had at the time the `for` keyword is encountered. Note that `g` and `%%g` are not the same `g` variable. You need to use `set "g=%%g"` then `SET "g=!g: =!"` then use `!g!`, not `%g%` to for your purpose. – Magoo Dec 05 '22 at 10:40
  • Use `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. In comparisons, use `if "thing1" == "thing2" ...` to avoid problems caused by spaces in `thing1/2`. – Magoo Dec 05 '22 at 10:46
  • You have not shown us the problematic format of entries in `file1` and haven't told us what you want to do (if anything) with the other parts of the mystery line. More data=>more assistance. – Magoo Dec 05 '22 at 10:48
  • This doesn't work. I used :set "g=%%g" then SET "g=!g: =!". When I echo !g! is displayed: !g! (not his value). findstr doesn't find anything now. – IGP Dec 07 '22 at 15:08
  • I forgot to set the variable EnableDelayedExpansion... it goes with setlocal EnableDelayedExpansion – IGP Dec 07 '22 at 15:30

0 Answers0