0

new to batch file scripting. Need your all help to extract all the values which are in (single quotes) '' from below text file (file.txt) and find that value in another text file, if not exists throw an error.

file.txt
Wed 11/01/2023 - 19:59:23.33 
BIP1138I: Applying overrides using runtime mqsiapplybaroverrides...
BIP1140I: Overriding property FTP_To_Trg#FtpServer with 'ftp.test.com' in 'App_Common_APP/META/broker.xml' ...
BIP1140I: Overriding property FTP_To_Trg#FileOut with 'ftp.test.com' in 'App_Common_APP/META/broker.xml' ...
BIP1140I: Overriding property Email#UDP_SysNm with 'SIT' in 'App_Common_APP/META/broker.xml' ...

I have updated file paths and tried it didn't work, after running the script I don't see anything happening, providing file paths caused an issue?

@ECHO Off
SETLOCAL ENABLEDELAYEDEXPANSION

FOR /f "delims=" %%e IN (C:\Users\k2\file.txt) DO FOR %%y IN (%%e) DO (
 SET "word=%%y"
 IF "!word:~0,1!!word:~-1!"=="''" (
  FIND "!word:~1,-2!" C:\Users\k2\result.txt>nul
  IF ERRORLEVEL 1 (>>C:\Users\k2 ECHO %%y) ELSE (>>C:\Users\k2 ECHO %%y)
 )
)

Tried with file names and yet I don't see anything happening, I have also provided double quotes("") for filePaths, is that issue?

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /f "delims=" %%e IN ("C:\Users\k2\file.txt") DO FOR %%y IN (%%e) DO (
 ECHO %%y
 SET "word=%%y"
 IF "!word:~0,1!!word:~-1!"=="''" (
 ECHO processing %%y
  FIND "!word:~1,-2!" "C:\Users\k2\result.txt">nul
  IF ERRORLEVEL 1 (>>"C:\Users\k2\Failure.txt" ECHO %%y) 
  ELSE (>>"C:\Users\k2\Success.txt" ECHO %%y)
 )
)


3
  • Stackoverflow.com <stackoverflow.com> is not a free script/code writing service. Edit relevant section(s) of what you have tried into your question along with appropriate representative data (use cut/paste) & say what your actual & expected results are. We can try to help with specific problems. This problem can be solved in two lines of code, making presumptions eg does the "other text file" contain the quotes? You should also read How do I ask a good question? <stackoverflow.com/help/how-to-ask>. Requests for code to be written are off-topic & will be closed. Commented Nov 7, 2023 at 15:45
  • I have absolutely tried but mine is just checking the specific text from a file, I need to read 1 by 1 line and find the same value in another file. ``` FINDSTR /I "plp api.com" C:\Users\k2\file.txt >> "C:\Users\k2\result.txt" IF NOT ERRORLEVEL 1 ( ECHO Error ) ELSE ( ECHO "Build Complete" ) PAUSE ``` Commented Nov 7, 2023 at 16:20
  • Please don't post code into comments - use the edit button to edit it into your question. Highlight it and press {} to indented for display. Commented Nov 7, 2023 at 16:45

1 Answer 1

0

You should be able to adapt this:

@ECHO Off&SETLOCAL ENABLEDELAYEDEXPANSION

FOR /f "delims=" %%e IN (q77438851.txt) DO FOR %%y IN (%%e) DO SET "word=%%y"&IF "!word:~0,1!!word:~-1!"=="''" FIND "!word:~1,-2!" q77438851_2.txt>nul&IF ERRORLEVEL 1 (>>u:\miss ECHO %%y) ELSE (>>u:\hit ECHO %%y)

(which just proves it can be done in two lines) - reformatting to a better layout

@ECHO Off
SETLOCAL ENABLEDELAYEDEXPANSION

FOR /f "delims=" %%e IN (q77438851.txt) DO FOR %%y IN (%%e) DO (
 SET "word=%%y"
 IF "!word:~0,1!!word:~-1!"=="''" (
  FIND "!word:~1,-2!" q77438851_2.txt>nul
  IF ERRORLEVEL 1 (>>u:\miss ECHO %%y) ELSE (>>u:\hit ECHO %%y)
 )
)

q77438851.txt is my test file, q77438851_2.txt the other file in which to locate the found-strings.

Read each line from q77438851.txt to %%e, then assign %%y to each space-separated string in turn (note: also uses ,; as separators)

Assign the "word" found to word as you cannot substring a metavariable (%%e %%y)

See whether the first character and the last character of word are both '. and if so...

Locate the substring of word from the second to the second-last character in the second file using find, directing find's output to the bit-bin and setting errorlevel to 0 if found and non-0 if not found.

Write the quoted word found (in %%y) to either the hit or miss file (My U: is a ramdrive)

Obviously, the destination files used would need to be cleared before the run is made - or they could be deleted within the script.

Substring handling - see set /? from the prompt for docco or thousands of examples on SO.

Edit in response to comment - first quoted string only

@ECHO Off
SETLOCAL ENABLEDELAYEDEXPANSION

FOR /f "usebackqdelims=" %%e IN ("q77438851.txt") DO SET "first=y"&FOR %%y IN (%%e) DO (
 SET "word=%%y"
 if defined first IF "!word:~0,1!!word:~-1!"=="''" (
  set "first="
  FIND "!word:~1,-2!" q77438851_2.txt>nul
  IF ERRORLEVEL 1 (>>u:\miss ECHO %%y) ELSE (>>u:\hit ECHO %%y)
 )
)

Set first to a value for each line read.

If first is defined AND it's aquoted word, set first to nothing (which means it becomes undefined - but will be re-defined when the next line is read)

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

9 Comments

I have updated file paths and tried it didn't work, after running the script I don't see anything happening, providing file paths caused an issue? Updated the question.
>>C:\Users\k2 you have used twice on the if errorlevel... line. C:\Users\k2 is a directory. You need to add a filename (...\hit.txt ...\miss.txt). You could also add a line echo %%y before the set "word= line to display the individual words found, and if that makes sense, add echo processing %%y before the FIND "... line.
Tried with file names and yet I don't see anything happening, I have also provided double quotes("") for filePaths, is that an issue? updated the question.
Yes - if the filename in FOR /f "delims=" %%e IN (q77438851.txt) is quoted, you need "usebackqdelims=" otherwise for/f will analyse the quoted name as a simple string. See for /? from the prompt for docco, or the usual thousands of examples on SO
In a line there can more than 1 value with single quotes (' '), I just need to pick the first 1 value.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.