Jump to content

FileOpen for Write


Recommended Posts

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.

Link to comment
Share on other sites

  • Moderators

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)

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.

Link to comment
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

Link to comment
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.

Link to comment
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)

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...