none
how to create new .txt file (sb)

    Question

  •  i need code that creates file in .txt format and save it in specified directory, also i would need to be able to name the that file.



    • Edited by TheGuxi Friday, October 05, 2012 9:05 AM
    Friday, October 05, 2012 7:20 AM

Answers

  • If you want to study "file" command, you'd better see this thread first.

    Beginner Developer Learning Center > Small Basic > Small Basic Curriculum >

    Lesson 3.1: File Input and Output

    and below is a sample I made.
    '--------------   create a sub directory  -----------------
    dir=program.Directory+"\"       '  or "d:\" or "e:\" or....... "z:\"
    subdir="dumydir"                '  sub directory name is   dumydir
    File.CreateDirectory(dir+subdir)

    '-------------- Make a  (dumy)  file ------------- -----------------
    fname="testfile2"  
    CRLF=Text.GetCharacter(13)+Text.GetCharacter(10)  ' carrige return + line feed
    cont1="This is dumy file"+CRLF+" 2012.10.05"+CRLF+" You made this."
    File.WriteContents(dir+subdir+"\"+fname+".txt",cont1)

    '--------------   Make a text file list    ------------- -----------------
    flist= File.GetFiles(dir+subdir)     '   gets all file list
    for i=1 to array.getitemcount(flist)
      if Text.IsSubText(flist[i],".txt") then '  if  it is a text file
        TextWindow.WriteLine(flist[i])  '  show  txt file name with full path
      endif
    EndFor
    TextWindow.WriteLine("  ")

    '--------------   search a file   ------------- -----------------
    searchname="test"
    for i=1 to array.getitemcount(flist)
      If Text.IsSubText(flist,searchname) Then '  if file name contains test
        TextWindow.WriteLine(flist[i]+ ".txt  is exist")
        TextWindow.WriteLine("  ")
        cont2=File.readContents(flist[i])        ' read the contents
        TextWindow.WriteLine(cont2)           '  show its contents
      EndIf
    EndFor
    TextWindow.WriteLine("  ")

     

    Friday, October 05, 2012 1:47 PM

All replies

  • You can start with the program below and expand on that.

    Direct=" here the name of you directory (full path)"
    FI=Direct+"\filename.txt")
    File.AppendContents("text to write to file")


    Jan [ WhTurner ] The Netherlands

    Friday, October 05, 2012 9:46 AM
  • If you want to study "file" command, you'd better see this thread first.

    Beginner Developer Learning Center > Small Basic > Small Basic Curriculum >

    Lesson 3.1: File Input and Output

    and below is a sample I made.
    '--------------   create a sub directory  -----------------
    dir=program.Directory+"\"       '  or "d:\" or "e:\" or....... "z:\"
    subdir="dumydir"                '  sub directory name is   dumydir
    File.CreateDirectory(dir+subdir)

    '-------------- Make a  (dumy)  file ------------- -----------------
    fname="testfile2"  
    CRLF=Text.GetCharacter(13)+Text.GetCharacter(10)  ' carrige return + line feed
    cont1="This is dumy file"+CRLF+" 2012.10.05"+CRLF+" You made this."
    File.WriteContents(dir+subdir+"\"+fname+".txt",cont1)

    '--------------   Make a text file list    ------------- -----------------
    flist= File.GetFiles(dir+subdir)     '   gets all file list
    for i=1 to array.getitemcount(flist)
      if Text.IsSubText(flist[i],".txt") then '  if  it is a text file
        TextWindow.WriteLine(flist[i])  '  show  txt file name with full path
      endif
    EndFor
    TextWindow.WriteLine("  ")

    '--------------   search a file   ------------- -----------------
    searchname="test"
    for i=1 to array.getitemcount(flist)
      If Text.IsSubText(flist,searchname) Then '  if file name contains test
        TextWindow.WriteLine(flist[i]+ ".txt  is exist")
        TextWindow.WriteLine("  ")
        cont2=File.readContents(flist[i])        ' read the contents
        TextWindow.WriteLine(cont2)           '  show its contents
      EndIf
    EndFor
    TextWindow.WriteLine("  ")

     

    Friday, October 05, 2012 1:47 PM