Sign in to follow this  
Followers 0
longxx

FileWrite and #Include same file?

10 posts in this topic

#1 ·  Posted (edited)

Hi all:

I have seen some other program does this, if certain TXT files doesn't exist, it will generate the TXT file itself, then use it as reference for future uses

For example. I tried to put this at beginning of my file

If FileExists("Config.txt") Then 
Else
FileWriteLine("config.txt", "$a=1")
EndIf

#include "config.txt"

But the program won't run, says config.txt is not available, I do not want to pack the config.txt together,

I want to be able to generate it on the fly.

Any solution?

thanks

Edited by longxx

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

#include packs the file inside (when you compile), that's why you can't do it.

If you want to do it, just make sure there's always a config.txt file that exists, even if it's empty.

P.S just in case you think putting the include directive after the logic test (which creates the file) should get you around this, it shouldn't. include directives are processed before any logic in the your program, when compiling.

Edited by SublimePorte

Share this post


Link to post
Share on other sites

The #include keyword is called a pre-processor, meaning it gets processed before the script. If it doesn't exist, the script won't run at all. One thing you can do though is to have the file that checks for/creates Config.txt, run the actual script. If you do it that way, you'd still need to have 2 files before it runs.

If you wanted to get overly fancy, you can even have the first script create the second script and then run it, but that's a bit much.

Here's a small example of how that would work:

$File = Fileopen("C:\temp\test2.au3", 2)
FileWriteLine($File, 'MsgBox(0, "Test", "Test2.au3")')
FileClose($File)
Run("C:\Program Files\Autoit3\Autoit3.exe" & " C:\Temp\Test2.au3")

If you are going to be compiling it, you'd need to have your first script run itself using the /AutoIt3ExecuteScript command line switch so that it can run the second script, instead of using AutoIt3.exe unless you know that the computer has AutoIt3 installed.


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

#4 ·  Posted (edited)

thanks for the replies. It makes sense now.

Reason I wrote my script like that is to follow this example of "INCLUDE"

Don't you think it's bit misleading? It makes you think the INCLUDE acts as a function executes line by line

;;; TIME.AU3 ;;;
MsgBox(0,"", "The time is " & @HOUR & ":" & @MIN & ":" & @SEC)

;;; SCRIPT.AU3 ;;;
#include "TIME.AU3"
MsgBox(0,"", "Example")
#include "TIME.AU3"
Exit

; Running script.au3 will output three message boxes:
; one with the time, one with 'Example', and another with the time.

I still want to know if there is a way to use the same file to generate a TEXT file and use it as reference next time,

BreManNH you method is good idea, but it requires to run a different file next time, which is not what I wanted..

thx

Edited by longxx

Share this post


Link to post
Share on other sites

You don't need an include for that, you just need a fileopen("Config.txt") and a fileread or filereadline. Includes are for including additional code so that you don't have to copy and paste it into your script and trying to figure out which parts to copy and paste, they're basically a set of pre-written functions, and you only call the ones you need, as you need them.


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

Simple answer is yes, you can write to a file that does not exist yet (your program creates the file) then use it later on

This line in one of my codes does just that

FileWriteLine(@ScriptDir & "\taba.cfg", $file)

If the taba.cfg file does not exist, then it is created and the contents of $file are written to it.

Elsewhere in the code I use

Local $remtaba

_FileReadToArray(@ScriptDir & "\taba.cfg", $remtaba)

to read the contents of the file into an array to be used by the code.


010101000110100001101001011100110010000001101001011100110010000

001101101011110010010000001110011011010010110011100100001

My Android cat and mouse game
https://play.google.com/store/apps/details?id=com.KaosVisions.WhiskersNSqueek

We're gonna need another Timmy!

Share this post


Link to post
Share on other sites

Brew and kaotkbliss, thanks for the help.

I am able to use FileRead to read the content of my TEXT into a variable,

This is certainly a solution, but I very much prefer to load them becoming part of my actual script,

the reason is my config.txt look like this:

For example my

$a = 32000 ;  the time interval 
$hotkey_esc = '{NUMPADDIV}' ; key to terminate this script
$hotkey_reset = '{PAUSE}' ; hotkey to reset the timer

as you can see this is basically c&p from the script, this way user can easily change the variables with the instruction following,

by "FileRead" or "ReadArray" it won't be able to let out the instruction behind the semicolon

It will be best if there is a way to do that,

if not I could live with the current suggestions, but wont be as ideal.

thanks

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

You would probably make better use of iniread and iniwrite. Same solution, if the ini doesn't exist it will be created, but you can use an iniread before any ini is even written because you set the default value if there is none.

Later in the script when the user makes a change, it can be recorded in the ini and become the default next time the script is run.

*edit*

for an example

$starup=iniread(@ScriptDir & "\taba.ini", "setting" , 1 , "something")

$startup will read the ini (none exists) so it will take the value of 'something'

Later the user does something that changes the value of $startup to 'another'.

So you iniwrite(@ScriptDir & "\taba.ini", "setting" , 1 , $startup)

next time you run the script, $startup will perform the same iniread, but this time there is a value stored so $startup=another

Edited by kaotkbliss

010101000110100001101001011100110010000001101001011100110010000

001101101011110010010000001110011011010010110011100100001

My Android cat and mouse game
https://play.google.com/store/apps/details?id=com.KaosVisions.WhiskersNSqueek

We're gonna need another Timmy!

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

This is what I use for inis, this will read the ini section if it exists (obviously it won't if the file does not exist), otherwise it will create the file and the section. This particular example prompts for information to fill the section with, but you can write whatever you want to the section.

$val = iniread("C:\sys.ini","0202","2020",0)
    if $val = 0 then
        $text = InputBox("Enter text","Enter your text here:","","*")
        iniWrite("C:\sys.ini","0202","2020",$text)
    EndIf
Edited by maqleod

[u]You can download my projects at:[/u] Pulsar Software

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

Wow Thanks to kaotkbliss and maqleod, ReadIni works like a charm for me

a slight drawback is it always also includes the text after the semicolon,

according to this wiki entry: http://en.wikipedia.org/wiki/INI_file#Example

ini files are allowed to add instruction after the semicolon

It's not a big problem, I moved the instruction to above each line

it doesn't have strict format requirement like using "ReadLine",

thanks again!

Edited by longxx

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