IonicWind Software

Creative Basic => General Questions => Topic started by: TexasPete on October 24, 2009, 05:31:10 AM

Title: Saving Files
Post by: TexasPete on October 24, 2009, 05:31:10 AM
Converting from Lb to Cb.
I did the following example.


FILESAVE$=DDefaultDir$+"\"+"ProjectFiles"+"\" +ProjectName$+"\" +PageName$+".ckl"
def SaveFile:FILE
If (OPENFILE(SaveFile,FILESAVE$,"W")=0)
Write SaveFile, "This is a Test"
CloseFile SaveFile


Any comments of suggestions would be helpful.

Thanks Texas Pete
Title: Re: Saving Files
Post by: SnarlingSheep on October 24, 2009, 08:48:51 AM
Try this example:
def SaveFile:FILE
def filename:STRING

filename = "c:\CBTestWrite.txt"

if (OPENFILE(SaveFile,filename,"W")=0)
  messagebox 0,"Opened '"+filename+"' for writing.","CB Write Test"

  write SaveFile, "This is a Test"

  messagebox 0,"Wrote string to '"+filename+"'.","CB Write Test"

  closefile SaveFile

  messagebox 0,"Closed '"+filename+"'.","CB Write Test"
else
  messagebox 0,"Unable to open '"+filename+"' for writing","CB Write Test"
endif
Title: Re: Saving Files
Post by: aurelCB on October 24, 2009, 09:29:45 AM
Snarling i think that your example is not right answer.
Pete need first create folder then create subfolder with project name then write file in this subfolder.
How do that - I dont know....?
Title: Re: Saving Files
Post by: SnarlingSheep on October 24, 2009, 09:34:04 AM
My example is the basics of what Pete needs to open a file and write to it.
That is exactly what he needs to learn before jumping into converting LB code.
Title: Re: Saving Files
Post by: aurelCB on October 24, 2009, 10:36:51 AM
OK your example is base for write file I agree.
But do you see :
DEF FILESAVE$:STRING
DEF filter:STRING
DEF DDefaultDir$, ProjectName$,PageName$:STRING

So my question still stay - how create subfolder in folder ???
Title: Re: Saving Files
Post by: SnarlingSheep on October 24, 2009, 11:24:33 AM
Something like this?

def SaveFile:FILE
def filename$,DDefaultDir$,ProjectName$,PageName$:STRING
def handle:INT

DDefaultDir$ = "c:\CBTestDir"
ProjectName$ = "TestProject"
PageName$ = "TestPage"

'Set to path of directory
filename$ = DDefaultDir$+"\"+"ProjectFiles"+"\"+ProjectName$

'Check if DDefaultDir$ exists
handle = findopen(DDefaultDir$)
if (handle == 0)
  'If it doesn't exist, create it.
  if (createdir(DDefaultDir$) == 0)
    messagebox 0,"Error creating '"+DDefaultDir$+"' directory","CB WriteTest"
    findclose handle
    end
  endif
endif
findclose handle

'Check if ProjectFiles exists
handle = findopen(DDefaultDir$+"\"+"ProjectFiles")
if (handle == 0)
  'If it doesn't exist, create it.
  if (createdir(DDefaultDir$+"\"+"ProjectFiles") == 0)
    messagebox 0,"Error creating '"+DDefaultDir$+"\"+"ProjectFiles""' directory","CB WriteTest"
    findclose handle
    end
  endif
endif
findclose handle

'Check if ProjectName$ exists
handle = findopen(DDefaultDir$+"\"+"ProjectFiles"+"\"+ProjectName$)
if (handle == 0)
  'If it doesn't exist, create it.
  if (createdir(DDefaultDir$+"\"+"ProjectFiles"+"\"+ProjectName$) == 0)
    messagebox 0,"Error creating '"+DDefaultDir$+"\"+"ProjectFiles"+"\"+ProjectName$+"' directory","CB WriteTest"
    findclose handle
    end
  endif
endif
findclose handle

'Add file name to the end of the directory
filename$ = filename$+"\"+PageName$+".ckl"

if (OPENFILE(SaveFile,filename$,"W")=0)
  messagebox 0,"Opened '"+filename$+"' for writing.","CB Write Test"

  write SaveFile, "This is a Test"

  messagebox 0,"Wrote string to '"+filename$+"'.","CB Write Test"

  closefile SaveFile

  messagebox 0,"Closed '"+filename$+"'.","CB Write Test"
else
  messagebox 0,"Unable to open '"+filename$+"' for writing","CB Write Test"
endif
Title: Re: Saving Files
Post by: aurelCB on October 24, 2009, 12:25:37 PM
Yes... ;D
Oh my I completly forget command FINDOPEN ::)
This is excellent example ,thank you Snarling ;)