Sign in to follow this  
Followers 0
SenseEye

writing to txt file using _FileWriteToLine

7 posts in this topic

Hello All, 

I am having some issues with the below script. I want to take a value from an input box and write it to a text file (stored in the same location named Config.txt).

 

Results: script runs however the text file does not get populated with the result from the input. Any help would be greatly appreciated. 

#include <File.au3>

Global $sIniFile = @ScriptDir & "\" & "Config.txt"
MsgBox(1,"",$sIniFile) ;Test to see if $sIniFile is working - Comes back as expected 

Global $script1 = InputBox("Window Info", "Enter the name of your program identified using Auto Window Info")
_FileWriteToLine($sIniFile, 1, $script1, 1)

 

Share this post


Link to post
Share on other sites



What error is returned by the _FileWriteToLine?


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

I'm not getting any errors on the console the script runs and finishes but does not add any info to the txt file. (I am new to this do I need to be running in a debug mode or is there a location where errors would be logged that is not set to the console window?)

 

Share this post


Link to post
Share on other sites

And you made sure the file is not empty? Look in the help file for _FileWriteToLine, it must have at least as many lines as $iLine or it will fail. Try something like this:

#include <File.au3>

Global $sIniFile = @ScriptDir & "\" & "Config.txt"
MsgBox(1,"",$sIniFile) ;Test to see if $sIniFile is working - Comes back as expected

Global $script1 = InputBox("Window Info", "Enter the name of your program identified using Auto Window Info")
_FileWriteToLine($sIniFile, 1, $script1, 1)
    If @error Then ConsoleWrite(@error & @CRLF)

Then correlate the @error number with the help file.

1 person likes this

When you're dead, you don't know you're dead - it's only difficult for those that know you. It's the same way when you're stupid...

My Scripts: SCCM UDFInclude Source with Compiled Script, Windows Firewall UDF

Share this post


Link to post
Share on other sites

Thank you jLogan3o13 thats fixed it I was trying to an empty file (opened the Config.txt added some content and it now works

Share this post


Link to post
Share on other sites

Do you need it to be on a specific line? If you're just looking to append to the end of an existing file, try FileWriteLine instead.

1 person likes this

When you're dead, you don't know you're dead - it's only difficult for those that know you. It's the same way when you're stupid...

My Scripts: SCCM UDFInclude Source with Compiled Script, Windows Firewall UDF

Share this post


Link to post
Share on other sites

I just need to write and read to the line at the moment but that's great for advice for future projects :) thank you so much for your help

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

  • Similar Content

    • SpinningCone
      Problem with _FileWriteToLine ? help needed
      By SpinningCone
      FOr a while now i've been running a script that uses a flat text file to keep track of users to perform actions on.
       
      I have some logic in the block that adds new users with some comma delimited fields with FileWriteLine and then updates that line with _FileWriteToLine

      this has been running fine for years but suddenly i'm having problems with the script crashing because when the very last line of the file is losing it's CRLF so the next appended line concatenates with the last line and the parser fails to parse the last line.
      this has been driving me nuts for a while so i finally took apart all my code and have discovered that for some reason _FileWriteToLine deletes CRLF off the bottom of the file after use. i don't know why this started suddenly, i even copied an old file.au3 library and it still does it (maybe a change to shell32.dll? )

      I was able to replicate the bug by making a new file with each line having CRLF at the end then  with the following code:
       
      #Region ;**** Directives created by AutoIt3Wrapper_GUI **** #AutoIt3Wrapper_UseX64=y #AutoIt3Wrapper_Res_requestedExecutionLevel=asInvoker #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI **** _FileWriteToLine($FilePath,12,"Test rewrite",1) After running the code and refreshing the file i can see that the last blank line disappears (no final CRLF)

      Is there a way to prevent this behavior? (without a huge logic rewrite to use a DB or another write method)

      EDIT: actually it seems that replacing the old au3 is working, or at least working some of the time. not sure if it's a *sometimes* bug with the older library.
    • monter
      [Beta bug?] _FileWriteToLine damages the file (3.3.9.20+)
      By monter
      Hello, I just discovered bad thing. If I used _FileWriteToLine function in AutoIt3 beta since 3.3.9.20 and newer, after launching the file it's partially damaged itself. Older versions work correctly and there is no file damaging.
      Look at the example and test on different versions - older and newer.
       
      (Added): I think the problem is in IncludeFile.au3
      v.3.3.9.20, line 677:
      Local $aArray = StringRegExp(@CRLF & $sFileRead & @CRLF, "(*BSR_ANYCRLF)([^\R]*)(?:\R)(?:\R$)?", 3) v.3.3.9.19, line 683:
      Local $aArray = StringRegExp(@CRLF & $sFileRead & @CRLF, "([^\r\n]*)(?:\r\n|\n|\r)(?:[\r\n]$)?", 3) Maybe the pipe "|" I used is the reason?
      #AutoIt3Wrapper_Res_Field=Release date|18.10.2013 partypooper, MrCreatoR?
       
      P.S. Excuse me for strange characters in line number 5.
      It's an online artifact, there is no such characters in my code.
       
      #region ;**** Directives created by AutoIt3Wrapper_GUI **** #AutoIt3Wrapper_Compression=4 #AutoIt3Wrapper_Res_Comment=http://monter.FM #AutoIt3Wrapper_Res_Description=Example file showing beta bug since AutoIt3.3.9.20. #AutoIt3Wrapper_Res_Field=Release date|18.10.2013 #AutoIt3Wrapper_Res_LegalCopyright=monter.FM #endregion ;**** Directives created by AutoIt3Wrapper_GUI **** #include <Date.au3> #include <File.au3> Global $msg, $flg, $sec $title = 'AutoIt3 - version ' & @AutoItVersion FileChangeDir(@ScriptDir) If StringReplace(@AutoItVersion, '.', '') > 33919 Then VerBug() Else VerOK() EndIf FileInfo() MsgBox($flg, $title, $msg, $sec) If $flg = 48 Then Run('notepad.exe ' & @ScriptName) Func VerBug() $msg = 'This version has just damaged this file.' & @CRLF & 'Fortunately I made the *.bak copy :)' & @CRLF & 'Look at this damaged script now.' $flg = 48 $sec = 6 FileCopy(@ScriptName, @ScriptName & '.bak') EndFunc ;==>VerBug Func VerOK() $msg = "This version works fine and shouldn't damage this file." $flg = 64 $sec = 3 EndFunc ;==>VerOK Func FileInfo() Opt('TrayIconDebug', 1) $strRes = '#AutoIt3Wrapper_' For $ln = 1 To 10 $srchRes = StringInStr(FileReadLine(@ScriptFullPath, $ln), $strRes) If $srchRes > 0 Then $lr = $ln ExitLoop EndIf Next $strRes = '#AutoIt3Wrapper_Res_Field=Release date|' For $ln = $lr To $lr + 16 $srchRes = StringInStr(FileReadLine(@ScriptFullPath, $ln), $strRes) If $srchRes > 0 Then $lnRes = $ln ExitLoop EndIf Next $rd = FileGetTime(@ScriptFullPath, 0, 0) _FileWriteToLine(@ScriptFullPath, $lnRes, '#AutoIt3Wrapper_Res_Field=Release date|' & $rd[2] & '.' & $rd[1] & '.' & $rd[0], 1) Global $dateRlse = $rd[2] & '.' & $rd[1] & '.' & $rd[0] FileSetTime(@ScriptFullPath, $rd[0] & $rd[1] & $rd[2] & $rd[3] & $rd[4] & $rd[5], 0) EndFunc ;==>FileInfo FileWriteToLine_bug_Au3.3.9.20+.au3
    • guinness
      Insert a line into a file (like _FileWriteToLine) but with no memory limitation.
      By guinness
      I'm trying to create/see if it's possible for an alternative to _FileWriteToLine with no memory limitation.
      For example: >jchd's -_FileCountLines