Sign in to follow this  
Followers 0
emendelson

Write new first line of file - simpler way?

4 posts in this topic

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?

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

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


Get Beta versions Here Get latest SciTE editor Here AutoIt 1-2-3 by Valuater - A great starting point.

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

Share this post


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

Exactly what I needed; thank you.!

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