-1

I'm trying to code a Batch script to find txt files in a folder (and recursively in subfolders) that contain these two consecutive lines (no spaces between the lines):

Code "5898"

Price "50"

I tried with this:

Findstr -m /S /C:"Code ""5898""\r\n Price ""50"" *.txt" >> output.txt

but I don't know how to manage the carriage return and the newline. If I try to find the string without using Price ""50"" it works fine while no good results if I try to look for the two lines I need.

qwe
  • 25
  • 5
  • Several edits later and still your example code does not show a folder, a recursive search, the line containing the string `Code "5898"` or any attempt at looking for a line containing `Price "50"`. I think you'd be better off trying to explain your requirements more fully. – Compo Oct 13 '16 at 15:06
  • You are right. I fixed many of the points: folder is not very important. I'll run the command from the folder where I want to start my search. – qwe Oct 13 '16 at 15:13
  • This will help you in the future using FINDSTR. http://stackoverflow.com/questions/8844868/what-are-the-undocumented-features-and-limitations-of-the-windows-findstr-comman – Squashman Oct 13 '16 at 16:04

1 Answers1

0

Please do not alter this file, doing so would probably cause it not to work.

@Echo Off
SetLocal EnableDelayedExpansion
For /F %%A In ('Copy /Z "%~dpf0" Nul') Do Set "CR=%%A"
Set LF=^


Dir/B/S/A-D|Findstr/RNF:"/" /C:"Code \"5898\"!CR!*!LF!Price \"50\"">"%tmp%\$_.tmp"
>Output.txt (   For /F "UseBackTokens=1-3 Delims=:" %%A In ("%tmp%\$_.tmp") Do (
    Echo("%%A:%%B"))
Del "%tmp%\$_.tmp"

To output only the filename, (odd when the search is recursive):

@Echo Off
SetLocal EnableDelayedExpansion
For /F %%A In ('Copy /Z "%~dpf0" Nul') Do Set "CR=%%A"
Set LF=^


Dir/B/S/A-D|Findstr/RNF:"/" /C:"Code \"5898\"!CR!*!LF!Price \"50\"">"%tmp%\$_.tmp"
>Output.txt (   For /F "UseBackTokens=1-3 Delims=:" %%A In ("%tmp%\$_.tmp") Do (
    Echo("%%~nxB"))
Del "%tmp%\$_.tmp"

You could technically change the Tokens to 1-2 now but I'll leave it as is so it is easier to re-implement the line number check again, If %%C==1 before the echo on the second last line if ever necessary.

Compo
  • 36,585
  • 5
  • 27
  • 39
  • It works, thank you!!! I redirect the output in a file adding a >> output.txt after the Echo. What about if I would print in my file only the filename without path? – qwe Oct 13 '16 at 16:04
  • Trying better if I have a file with Price "5" this script works while it should not... – qwe Oct 13 '16 at 16:08
  • I have updated the answer to incorporate your output file and file nmae only requirements. As to your issue with `Price "5"` that would probably require that I spend a long time debugging, without having true access to sufficient resources for testing. – Compo Oct 13 '16 at 16:13
  • I understand. But for others that read I have to say that it doesn't work fine because with every string in Price the file is returned. – qwe Oct 13 '16 at 16:28
  • One option for the `Price "5"` issue may be to ignore the FindStr **/C** option. I have incorporated that into the answers. – Compo Oct 13 '16 at 16:28
  • It doesn't work. With another file: Code "5898" Price "45243" It works the same. So the second line is never considered – qwe Oct 13 '16 at 16:36
  • I have tested the code and found that the issue was being caused within the parentheses of the For command. I have fixed this by using a temporary file. Hope it has helped you! – Compo Oct 13 '16 at 17:56
  • Your solution now works with the problem about the second line. But if the file doesn't start with the line Code "5898" it still doesn't work. Thank you for your help Compo, it's very important for me! – qwe Oct 14 '16 at 07:26
  • Your request was specific you wanted first line to contain `Code "5898"` and the second to contain `Price "50"`, that's what my code does. On the second last line the number 1 specifies the first line. I tested it with the same strings on lines 3 to 4 and 118 to 119 it worked perfectly outputting only the documents where it appeared on only lines 1 and 2. – Compo Oct 14 '16 at 08:37
  • I'm sorry if my request seemed to ask for files that contain the two lines in the first and the second line. In reality these lines can be in any position of a txt file and I can't predict where they can be. – qwe Oct 14 '16 at 08:41
  • That's fine, there were several edits by more than one person in quick succession and I went with one without checking if it had changed. I have updated the scripts accordingly. – Compo Oct 14 '16 at 08:51