Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate

Help with finding Line number of a string in a text file. Please :)

  • Please log in to reply
3 replies to this topic
  • Members
  • 8 posts
  • Last active: Aug 22 2014 07:04 PM
  • Joined: 14 Jan 2014
Hi! I am new here and was wondering if anyone could help me with my script. I am trying to find the line of a specific text entry in a text file.
InputBox, Lot, Enter Lot Name, Enter Lot Name, 300,300 ;Enter Lot number ( location for text file)
MsgBox %Lot% ;Verify Lot name
FileRead, Contents, %A_Desktop%\%Lot%\%Lot%\C0000001.txt ;Read Text file into "Contents" variable
Loop, Parse, Contents, ;Read through "Contents" variable
If (A_loopfield ~= "Ball      CO") ;Hopefully Search for text string "Ball      CO" with spaces.
LineNum := A_Index ;Pull Line number from looping index
MsgBox %LineNum%  %A_Desktop%\%Lot%\%Lot% ;output line number and location of file as a check
MsgBox %Contents% ;output contents variable to ensure file was imported

  • Moderators
  • 8510 posts
  • Last active: Sep 10 2015 05:50 AM
  • Joined: 08 Apr 2009
Add this line at the top of your script, it only needs to be se once, unless the variable is modified
string :="Ball      CO"    ; <---- preserve the spaces between words
Change the one line of your script to this format
If A_loopfield contains %string%  ; <---- the 'contains' verb requires text not variable names, so %% are added

  • Members
  • 2475 posts
  • Last active: Feb 18 2015 04:57 PM
  • Joined: 01 Dec 2013

Change also this line :

Loop, Parse, Contents, ;Read through "Contents" variable

because the Loop, Parse, written like this, processes "Contents" character by character.

You have to give a delimter, like `r or `n or both.


to this

Loop, Parse, Contents, `r`n ;Read through "Contents" variable

Si ton labeur est dur et que tes résultats sont minces, souviens toi du grand chêne qui avant n'était qu'un gland....comme toi ! (anonyme) ;)

L'art de lire, c'est l'art de penser avec un peu d'aide. (É. Faguet)

Windows 3.1. Collector's Edition.     (www.avaaz.org)

  • Members
  • 8 posts
  • Last active: Aug 22 2014 07:04 PM
  • Joined: 14 Jan 2014

Thank you both for your help, I have it working well now :) I implemented your suggestions and also changed some of the text file it was searching.