Sign in to follow this  
Followers 0
Bullitt00692

FileOpen for Write

7 posts in this topic

I need to read the SERVICES file on a PC and append a line to the bottom, only if the line is not already there.

My questions is when I use "FileOpen(test.txt, 0)" I am able to read the file one line at a time with the "FileReadLine" command. When when I use "FileOpen(test.txt, 1) and execute the "FileReadline" command, I am unable to read in a line from the file.

Why can't I read the file when I open it with the '1' option?

I really do not want to read one file and then write to another. it would be great if I can open the SERVICES file for Write access and ready thru and append if needed.

Share this post


Link to post
Share on other sites



I need to read the SERVICES file on a PC and append a line to the bottom, only if the line is not already there.

My questions is when I use "FileOpen(test.txt, 0)" I am able to read the file one line at a time with the "FileReadLine" command. When when I use "FileOpen(test.txt, 1) and execute the "FileReadline" command, I am unable to read in a line from the file.

Why can't I read the file when I open it with the '1' option?

I really do not want to read one file and then write to another. it would be great if I can open the SERVICES file for Write access and ready thru and append if needed.

You may find it easiest to do something like:

$hFile = 'PathToFile\FileName.Extension'
$sLastLine = FileReadLine($hFile, UBound(StringSplit(StringStripCR(FileRead($hFile)), @LF)) - 1);Or look at _FileCountLines() in help
MsgBox(64, 'Info', 'Last Line = ' & @CRLF & $sLastLine)

[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

Remarks

Up to 64 files can be open simultaneously by one AutoIt script. Exceeding this limit throws a run-time error.

A file can only be in either read or write mode; it cannot be in both.

When opening a file in write mode, the file will be created if it does not exist.

When finished working with a file, call the FileClose function to close the file. Autoit normally closes all files upon termination, but explicitly calling FileClose is still a good idea.

http://www.autoitscript.com/autoit3/docs/f...ns/FileOpen.htm


Start here if you are new Valuater's AutoIT 1-2-3Looking for an UDF - Look hereDo you need to do it twice - Autoit

Share this post


Link to post
Share on other sites

I have the fileopen command, but the issue is a want to open the file for Write access and when I use this command "FileOpen(Text.txt, 1)" and then issue the FileReadLine command, the line I read in is spaces or blank. When I open the file with "FileOpen(Text.txt, 0)" I get the data, but can not write to the file.

Share this post


Link to post
Share on other sites

FileOpen(Text.txt, 0)

Read data

FileClose

FileOpen(Text.txt, 1)

Write Data

FileClose

Tada :P

As I wrote before you cant have a file open in read and write mode at the same time :whistle:


Start here if you are new Valuater's AutoIT 1-2-3Looking for an UDF - Look hereDo you need to do it twice - Autoit

Share this post


Link to post
Share on other sites

Here is an example of my script:

$file = FileOpen("c:\Windows\system32\drivers\etc\services", 0)

If $file = -1 Then

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

Exit

EndIf

;

$line = "Hold"

$i = 1

;

While $i <= 10

$line = FileReadLine($file)

If @error = -1 Then ExitLoop

If StringInStr($line, "sapmsQR1") > 0 Then

MsgBox(0, "Technical Services", "String Found", 20)

Exit

EndIf

MsgBox(0, "Technical Services", $line, 1)

Wend

;

FileWrite($file, "sapmsQR1 3601/tcp" & @CRLF)

FileClose($file)

;

MsgBox(0, "Technical Services", "String Not Found", 20)

Share this post


Link to post
Share on other sites

I'm a little slow tonight - I got it - Thanks!!!

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