Page 1 of 1

Getting variables from a file

Posted: Sun Jul 13, 2008 9:58 am
by dannyboy99
I am writing a small program and to make life a bit easy I thought I would put the variables in a small text file so they could be edited easier after the program had been compiled

I put the variable a$= "d:docs" in the txt file

I used the program that I found in a book, and whilst it debugs and shows a$ , a$ does not seem to be passed on to the CreateDirectory command. Only if I have a$ on the top of the code

#FILE_REPORT = 1
NewList FileContents.s()
If ReadFile(#FILE_REPORT, "Report.txt")
While Eof(#FILE_REPORT) = #False
AddElement(FileContents())
FileContents() = ReadString(#FILE_REPORT)
Wend
CloseFile(#FILE_REPORT)
EndIf
ForEach FileContents()
Debug FileContents()
createdirectory (a$)
Next

Can someone help

Thanks

Posted: Sun Jul 13, 2008 11:11 am
by eJan
If you need Directory name as a$ use quotation marks "":

Code: Select all

#FILE_REPORT = 1
NewList FileContents.s()

If ReadFile(#FILE_REPORT, "Report.txt")
  While Eof(#FILE_REPORT) = #False
    AddElement(FileContents())
    FileContents() = ReadString(#FILE_REPORT)
  Wend
  CloseFile(#FILE_REPORT)
EndIf

ForEach FileContents()
  Debug FileContents()
  CreateDirectory("a$")
Next
or as variable:

Code: Select all

#FILE_REPORT = 1
NewList FileContents.s()

a$ = "PB Created Dir"

If ReadFile(#FILE_REPORT, "Report.txt")
  While Eof(#FILE_REPORT) = #False
    AddElement(FileContents())
    FileContents() = ReadString(#FILE_REPORT)
  Wend
  CloseFile(#FILE_REPORT)
EndIf

ForEach FileContents()
  Debug FileContents()
  CreateDirectory(a$)
Next

Variable

Posted: Sun Jul 13, 2008 11:46 am
by dannyboy99
Thanks for this.

It is probably throught lack of sleep (I have been trying to get this to work all night)

I can get the variable to work in the program, but I cannot find a way of importing the variable from a text file. The reason is that I want to use the file like win.ini so I can alter the variable in an easy manner.

Mind you, can it be done, or am I going about it the wrong way.

Re: Variable

Posted: Sun Jul 13, 2008 2:43 pm
by Demivec
dannyboy99 wrote:I can get the variable to work in the program, but I cannot find a way of importing the variable from a text file. The reason is that I want to use the file like win.ini so I can alter the variable in an easy manner.

Mind you, can it be done, or am I going about it the wrong way.
Yes it can be done.

It did not work because you never assigned any value to a$.

By looking at your code I would assume a$ is stored somewhere in "Report.txt". If so one of these might would work for you:

Code: Select all

CreateDirectory(FileContents()) ;according to your code this is where the string is stored

;or 
#FILE_REPORT = 1
NewList FileContents.s()

If ReadFile(#FILE_REPORT, "Report.txt")
  While Eof(#FILE_REPORT) = #False
    AddElement(FileContents())
    FileContents() = ReadString(#FILE_REPORT)
  Wend
  CloseFile(#FILE_REPORT)
EndIf

ForEach FileContents()
  Debug FileContents()
  a$ = FileContents() 
  CreateDirectory(a$)
  ;or just use CreateDirectory(FileContents())
Next

Re: Variable

Posted: Sun Jul 13, 2008 3:17 pm
by Fangbeast
dannyboy99 wrote:Thanks for this.

It is probably throught lack of sleep (I have been trying to get this to work all night)

I can get the variable to work in the program, but I cannot find a way of importing the variable from a text file. The reason is that I want to use the file like win.ini so I can alter the variable in an easy manner.

Mind you, can it be done, or am I going about it the wrong way.
If you want to handles things like win.ini, have a look at the "preference" commands section of the help files. That area manipulate standard INI types files.

Posted: Sun Jul 13, 2008 6:15 pm
by dannyboy99
Thanks for your help

I tried the script, but I still cannot get it to work.

You are quite right the "report.txt" contains one line a$="c\data"

When I started programming this I had hoped to be able to have more than one line ie a$="c:\data", b$="d:\save" etc.

It shows up ok in debug, but not in any commands. report.txt was a notepad file.

Do you think I am doing anything wrong - or is there another way of pulling variables in from a text file.

As you may guess I am totally new to programming

Posted: Sun Jul 13, 2008 8:30 pm
by eJan
a$="c\data"
is complete line/string extracted from opened file, now you have to use string functions to get only the part of string:

Report.txt:

Code: Select all

a$="c:\data"

Code: Select all

#FILE_REPORT = 1
NewList FileContents.s()

If ReadFile(#FILE_REPORT, "Report.txt")
  While Eof(#FILE_REPORT) = #False
    AddElement(FileContents())
    FileContents() = ReadString(#FILE_REPORT)
  Wend
  CloseFile(#FILE_REPORT)
EndIf

ForEach FileContents()
  ;   Debug FileContents()
  ;Debug StringField(FileContents(), 2, Chr(34))
  Debug StringField(FileContents(), 2, #DQUOTE$)

  a$ = StringField(FileContents(), 2, #DQUOTE$)
  l_case.s = LCase(a$) ; convert it to lover case

  ;CreateDirectory(StringField(FileContents(), 2, #DQUOTE$))

  If Left(l_case, 3) = "c:\"
    CreateDirectory(a$)
  EndIf
Next

Posted: Mon Jul 14, 2008 7:14 pm
by dannyboy99
Works a treat - thanks very much

Posted: Mon Jul 14, 2008 8:59 pm
by eJan
You're welcome.

Thanks

Posted: Thu Aug 21, 2008 7:04 pm
by dannyboy99
Thanks very much for those who helped me here.

I have finsihed the programs, which seem to working well.

Thanks again

Danny