Jump to content

Random fileread


Recommended Posts

Hey! I tried to make a script that chooses a random line from a .txt, and shows it in a msgbox. This is what i've come up with so far, but i can't figure out what i've done wrong. There's no errors, but it still doesn't do what i just said. I also want it to be able to write the file by adding a word.

#include <File.au3>
#include <GUIConstantsEx.au3>
HotKeySet("{ESC}", "Terminate")
$linecount=0
$gui=GUICreate("Nothing to do?", 250, 180)
$input1=GUICtrlCreateInput("", 20, 20, 200, 22)
$button1=GUICtrlCreateButton("Add", 20, 50, 200, 22)
$button2=GUICtrlCreateButton("Go!", 20, 80, 200, 22)
$button3=GUICtrlCreateButton("New File", 20, 110, 200, 22)
$Button4=GUICtrlCreateButton("Old File", 20, 140, 200, 22)
GUISetState(@SW_SHOW)
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
  Case $GUI_EVENT_CLOSE
   Exit
  Case $Button4
   Local $FilePath = FileOpenDialog("Chose File", @ScriptDir, "(*.txt)")
   $file=FileOpen($FilePath)
  Case $button3
    $FilePath = InputBox("Filename?", " ", "")
    FileWrite(@ScriptDir & "\" & $FilePath & ".txt", "")
    $file=FileOpen(@ScriptDir & "\" & $FilePath & ".txt", 1)
  Case $button1
   $read1=GUICtrlRead($input1)
   FileWrite($file, $read1 & @CRLF)
   GUICtrlSetData($input1, "")
   Sleep(50)
  Case $button2
   GUIDelete($gui)
   FileClose($file)
   $file=FileOpen(@ScriptDir & "\" & $FilePath & ".txt", 0)
   While 1
     $fileread1=FileReadLine($file, 1)
     If $fileread1="" Then
      FileClose($file)
      Sleep(100)
      MsgBox(0, $linecount, $linecount)
      ExitLoop
     EndIf
      $linecount=$linecount+1
   WEnd
   While 1
    $file=FileOpen($FilePath)
    $fileread1=FileReadLine($file, 1)

    $Random=Random(1, $linecount, 1)
    If $Random=1 Then
     MsgBox(0, "You're going to:", $fileread1)
     FileClose($file)
     ExitLoop
    EndIf
    FileClose($file)
   WEnd
EndSwitch
WEnd
FUnc Terminate()
Exit
EndFunc
Link to comment
Share on other sites

Well I have Explained in Brief What You Wanted To do

Herez The Code

#cs  Well You Can Add THe Gui
and Rest Of The Stuff I guess...................
Herez A Simple Code to Get a Random Line
From a File And Show it in a Msgbox/Splash........
#ce
Global $sFile_Name='C:UsersabhishekDesktopPresentationAbhishekSCriptsPhoenix XL.txt'
Global $sFile_Handle=FileOpen($sFile_Name)
Global $sLine_Count=1
#cs
The Following Loop
is For Getting the Number of Lines
Present in the File............
Note That @error is set to -1 when
end of file is reached........
#ce
While 1
$sRead=FileReadLine($sFile_Handle,$sLine_Count)
;ConsoleWrite('Read:'&$sRead&@TAB&'LineCount:'&$sLine_Count&@CRLF)
Switch @error
  Case -1
   ;ConsoleWrite('Encountered Error:'&@error&@TAB&'LineCount:'&$sLine_Count&@CRLF)
   $sLine_Count-=1
   ExitLoop
  Case 0
   ;ConsoleWrite('Encountered Error:'&@error&@TAB&'LineCount:'&$sLine_Count&@CRLF)
   Sleep(10)
  Case Else
   ;ConsoleWrite('Encountered Error:'&@error&@TAB&'LineCount:'&$sLine_Count&@CRLF)
   MsgBox(16,'Error','A Error Ocurred'&@CRLF&'Sorry For The Inconveniance')
EndSwitch
$sLine_Count+=1
WEnd
#cs
MsgBox Part .............
Will Show a Random Text.........
#ce
ConsoleWrite(_ShowRandomText($sFile_Handle,$sLine_Count)&@CRLF)
Func _ShowRandomText($sHandle,$sLine_Number)
Local $sRead=FileReadLine($sFile_Handle,Random(1,$sLine_Count,1))
If $sRead='' Then Return _ShowRandomText($sHandle,$sLine_Number)
Return MsgBox(64,'Random Text',$sRead)
EndFunc
#cs Debugging Lines Have Been Commented Out Using a Semi-Colon
Remove The Semi-Colon to get to know Whats Happening...... :)
#ce

;ConsoleWrite($sLine_Count&@CRLF)
;ConsoleWrite($sFile_Handle&@CRLF)
;ConsoleWrite($sFile_Name&@CRLF)

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...