Saved Bookmarks
| 1. |
Solve : for /f findstr problem with spaces? |
|
Answer» HELLO, guys. I did search but I couldn't find any proper SOLUTION to my problem. Here is what I've been trying to do: I have a file, let's name it vars.h where I define a few paths and then I try to find a specific define from that file. Example: vars.h: Code: [Select]#define MSYSPATH "C:\MSYS" test.bat: Code: [Select]FOR /F "tokens=3,3 delims= " %%A IN ('FINDSTR /I /L /C:"define MSYSPATH " "vars.h"') DO SET "MSYSPATH=%%~A" ECHO:"%MSYSPATH%" Which works fine except for the case where the path has spaces in it. So if I use #define MSYSPATH "C:\MSYS NEW" it will still output "C:\MSYS". THANKS a LOT in advance. You can create an IMPLICIT FOR variable, which is all of the line past the final explicit variable. This is created by an asterisk appended to the final explicit token number in the tokens/delims block, so to capture into one FOR variable everything past the first 2 tokens, use tokens=1-2* then if you use %%A to start, %%B is token 2 and the implicit variable %%C (because of the asterisk) is the rest of the line however many spaces it contains. token 1 = #define token 2 = MSYSPATH token 3 = everything after the 2nd delimiter Code: [Select]#define MSYSPATH "C:\MSYS NEW" <-%%A-> <-%%B--> <--- %%C ---> Code: [Select]#define MSYSPATH "C:\MSYS NEW VERY LONG FOLDER NAME\WITH SUBFOLDER" <-%%A-> <-%%B--> <---------------- %%C ---------------------------> Code: [Select]FOR /F "tokens=1-2* delims= " %%A IN ('FINDSTR /I /L /C:"define MSYSPATH " "vars.h"') DO set "MSYSPATH=%%~C" You are the man! It works perfectly, and thank you very much for explaining it. |
|