Sign in to follow this  
Followers 0
homemade

Auto selecting text and copying

6 posts in this topic

Hey guys, I'm completely new to using autoit and I'm kinda confused trying to figure out how to do things using the help file and forum search.

Anyways, I want to write a script that will open up a specific text file, automatically select the first few lines or so, copy it, delete what was selected, then paste into a new text file and save both.

I tried using the FileOpen function but it doesnt seem to open up the file

$file = FileOpen("test.txt", 0)

; Check if file opened for reading OK

If $file = -1 Then

MsgBox(0, "Error", "Unable to open file.")

Exit

EndIf

FileClose($file)

I replaced test.txt with the specific directory and file name but that doesnt open up the file

If you guys can point me in the right direction that would be great. Thanks.

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

FileOpen simply opens the file for reading and/or writing - you won't actually see the file open in Notepad or anything. You'll need to perform a FileOpen, then do your modifications, and finally perform a FileClose. Here is an example of opening an existing file, writing a line, and then closing the file:

$file = FileOpen("filename.txt",1);Opens the file in Appendable Write mode - does not overwrite contents
 FileWriteLine($file, "This line was injected into the file by my script.")
 FileClose($file)
Edited by Airwolf

Certifications: A+, Network+, Security+, Linux+, LPIC-1, MCSA | Languages: AutoIt, C, SQL, .NETBooks: AutoIt v3: Your Quick Guide - $7.99 - O'Reilly Media - September 2007-------->[u]AutoIt v3 Development - newbie to g33k[/u] - Coming Soon - Fate Publishing - Spring 2013UDF Libraries: SkypeCOM UDF Library | ADUC Computers OU Cleanup | Find PixelChecksumExamples: Skype COM Examples - Skype4COMLib Examples converted from VBS to AutoIt

Share this post


Link to post
Share on other sites

is there a better way to auto select text from a txt file than to have a mouse drag a defined section?

Share this post


Link to post
Share on other sites

is there a better way to auto select text from a txt file than to have a mouse drag a defined section?

You should try using _FileReadToArray to dump the contents of the file into an array, and then you can search through the array for the lines you want to manipulate. Once you find the lines, you can delete them and inject new lines or do whatever it is you'd like to do. Read up on _FileReadToArray and _FileWriteToLine in the help file. If you have specific questions, feel free to ask. Also, you can post any code you come up with whether it is partially working or not and we can help you out. Here is a basic/crude example of what I think you are trying to do:

#Include <File.au3>
Dim $array

$file = FileOpen("filename.txt",1)
$search = "Contents of line to find"

_FileReadToArray($file,$array) ;writes all lines in the file to an array, each entry in the array is a line
For $x = 1 To $array[0]
     If $array[$x] = $search Then
          _FileWriteToLine($file,$x,"",1) ;deletes the line from the original file
          $newfile = FileOpen("NewFile.txt",1) ;creates new file
          FileWriteLine($newfile,$array[$x]) ;writes the found search string into the new file
          FileClose($newfile) ;closes the new file
     EndIf
Next

FileClose($file)

Certifications: A+, Network+, Security+, Linux+, LPIC-1, MCSA | Languages: AutoIt, C, SQL, .NETBooks: AutoIt v3: Your Quick Guide - $7.99 - O'Reilly Media - September 2007-------->[u]AutoIt v3 Development - newbie to g33k[/u] - Coming Soon - Fate Publishing - Spring 2013UDF Libraries: SkypeCOM UDF Library | ADUC Computers OU Cleanup | Find PixelChecksumExamples: Skype COM Examples - Skype4COMLib Examples converted from VBS to AutoIt

Share this post


Link to post
Share on other sites

What I'm trying to do isn't complex enough to require an array.

I just need to select the first five lines of a file and copy it.

Share this post


Link to post
Share on other sites

What I'm trying to do isn't complex enough to require an array.

I just need to select the first five lines of a file and copy it.

Could do something like:
$lines = ""

;read the entire contents of the file into a variable
$originalFile = FileRead("test.txt")

; Read in lines of text until the number of lines specified or EOF is reached
$file = FileOpen("test.txt", 0)

; Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

For $i = 1 to 5
    $lines &= FileReadLine($file,$i) & @CRLF
        If @error = -1 Then ExitLoop
Next

FileClose($file)

MsgBox(0, $i-1 &  " lines read",$lines)

;create a new file for writing the read lines to, write the lines, close the file
$newFile = FileOpen("newFile.txt",2)
FileWrite($newFile,$lines)
FileClose($newFile)

;replace the lines read in $originalFile with nothing, overwrite the original file 
$originalFile = StringReplace($originalFile, $lines, "")
$file = FileOpen("test.txt",2)
FileWrite($file, $originalFile)
FileClose($file)

;empty the variables to free up memory
$lines = ""
$originalFile = ""

Share this post


Link to post
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
Sign in to follow this  
Followers 0