Jump to content

Write new first line of file - simpler way?


Recommended Posts

My script needs to write new first lines to various different text files (in each case, the line contains a variable that will be used in a batch file or similar file). I use a kludge to accomplish this, and I wonder if there is an easier way.

The kludge looks like this: it FileInstalls a temporary file with all of the text in the eventual text file EXCEPT the first line, then creates another temporary file that contains only the first line of the eventual text file - built from variables - and then uses the COPY command to combine the newly-created temporary file and the FileInstalled temporary file into the file I want to leave on disk.

FileInstall("c:\TestFiles\template.tmp", $mypath, 1)
$handle1 = FileOpen($mypath & "empty.tmp", 2)
FileWriteLine($handle1, "set somevariable =" & Chr(34) & $variabletext & Chr(34))
FileClose($handle1)
RunWait(@ComSpec & " /c " & 'copy empty.tmp + template.tmp target.cmd', $mypath, @SW_HIDE)

It works, but is there an all-AutoIt way to do this that doesn't require @ComSpec and would be more elegant?

Link to comment
Share on other sites

  • Moderators

My script needs to write new first lines to various different text files (in each case, the line contains a variable that will be used in a batch file or similar file). I use a kludge to accomplish this, and I wonder if there is an easier way.

The kludge looks like this: it FileInstalls a temporary file with all of the text in the eventual text file EXCEPT the first line, then creates another temporary file that contains only the first line of the eventual text file - built from variables - and then uses the COPY command to combine the newly-created temporary file and the FileInstalled temporary file into the file I want to leave on disk.

FileInstall("c:\TestFiles\template.tmp", $mypath, 1)
$handle1 = FileOpen($mypath & "empty.tmp", 2)
FileWriteLine($handle1, "set somevariable =" & Chr(34) & $variabletext & Chr(34))
FileClose($handle1)
RunWait(@ComSpec & " /c " & 'copy empty.tmp + template.tmp target.cmd', $mypath, @SW_HIDE)

It works, but is there an all-AutoIt way to do this that doesn't require @ComSpec and would be more elegant?

As long as your using the beta version you could use:

_FileWriteToLine($handle1, 1, "set somevariable =" & Chr(34) & $variabletext & Chr(34), 0)
Edited by big_daddy
Link to comment
Share on other sites

Would it not be easier to use _FileWriteToLine to write to line 1 of the file with the overwrite option set to 0

Edit - Oops too slow again

Edited by BigDod


Time you enjoyed wasting is not wasted time ......T.S. Elliot
Suspense is worse than disappointment................Robert Burns
God help the man who won't help himself, because no-one else will...........My Grandmother

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