none
How to Validate files. RRS feed

  • Question

  • How do you validate files you create?

    I know how to create directories using file.createdirectory and also file.writecontents but how can I validate the files by creating a check so that files are not overwitten if the same file name is supplied in the file.createdirectory step.

    Thursday, July 5, 2012 10:30 AM

Answers

  • How can I validate files by creating a check so that files are not overwritten if the same file name is supplied in the File.CreateDirectory step?

    Well, I thought you were just afraid to erase files within a directory/folder when issuing File.CreateDirectory operation!

    But you took so long to show up here!

    Anyways, I've tweaked previous code example to find an unused file+number name, so it doesn't overwrite former ones!

    Hope I got it right this time!

    Another option is to use File.AppendContents operation as WhTurner33 already mentioned. It glues new content to the end of a file.

    '______________________________________________________________________'
    Dir  = Program.Directory + "\MyDir\"
    File = "MyFile"
    Ext  = ".txt"
    Num  = 1
    Content = "I'm intact "
    LF = Text.GetCharacter(10)
    
    File.CreateDirectory(Dir)  ' Creating directories does not erase any former contents
    '                                                             if by chance already existed within it
    
    CheckIfFileExists()    ' Search for a free file number slot
    
    File.WriteContents(Path, Content + Num) ' This file name doesn't exist and is free to use
    '______________________________________________________________________'
    Sub CheckIfFileExists
      
      For z = 0 To 0 Step 0  '  Infinite Loop!!!
        z = 1  '  If z > 0 when EndFor is reached, loop breaks free
        
        Path = Dir + File + Num + Ext
        Text = File.ReadContents(Path)
        
        If Text = "" Then
          TextWindow.Write    ("File #" + Num)
          TextWindow.WriteLine(" couldn't be read, was empty or didn't exist!" + LF)
          TextWindow.WriteLine(Path + " is free to use!" + LF + LF)
        Else
          TextWindow.WriteLine(Path + " was found!" + LF)
          TextWindow.WriteLine("Contents:" + LF)
          TextWindow.WriteLine(Text + LF + LF)
          Num = Num + 1   '  Try another file number next time
          z = 0           '  Do not let loop break free!
        EndIf
        
      EndFor
      
    EndSub
    '______________________________________________________________________'


    Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)

    Monday, July 9, 2012 2:13 PM
    Answerer
  • run this twice.   backupfile is made automatically.

    and see  WQP603-0 (controlbox  textfile load/save )

    PGD= Program.Directory+"\"
    FLST= File.GetFiles(PGD)
    OldContents="abcdefg  123456  hijklmn"

    fname="test_file.txt"
    path=PGD+fname                               '   file  full path
    Newcontents="abcdefg  123456  hijklmn 7890 opqrst"

    If Text.IsSubText(FLST,fname) Then              '  if same file name exists
        TP= Text.GetIndexOf(path,".txt")
        path2= Text.GetSubText(path,1,TP-1)+".bak"     ' backup file name
        File.WriteContents(path2,OldContents)            '   save backup file
        TextWindow.WriteLine(" Back Up file is made!") 
    EndIf
    File.WriteContents(path,Newcontents)               '   save new file

    Wednesday, July 11, 2012 1:11 PM
    Answerer

All replies

  • PLEASE HELP. I'll pay you in gold. wow/rs/mtg/diablo3/rom

    Thursday, July 5, 2012 10:54 AM
  • Hello O2iver

    For what I know, re-creating directories does not erase any contents which by chance were within it!

    Here's some code for you to check it out for yourself:

    path = Program.Directory + "\MyDir\"
    
    File.CreateDirectory(path)  ' Creating directories does not erase any former content
                                'if by chance, it already existed!
    
    text = File.ReadContents(path + "MyFile.txt")
    
    If text = "" Then
      TextWindow.WriteLine("File couldn't be read, was empty or didn't exist!")
    Else
      TextWindow.WriteLine(text)
    EndIf

    You can put anything inside path and re-run the program above and you'll notice all is intact!

    Hope it gives ya peace! ^_^


    Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)

    Thursday, July 5, 2012 11:03 AM
    Answerer
  • Not playing D3 yet :P

    Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)

    Thursday, July 5, 2012 11:04 AM
    Answerer
  • Thanks for the help but when I run the program with files of the same name it still over writes the previous file. :s
    Monday, July 9, 2012 8:51 AM
  • File.WriteContents overwrites the file with new contents.

    File.AppendContents adds contents to the file.

    See description in the IntelliSense Wheel


    Jan [ WhTurner ] The Netherlands

    Monday, July 9, 2012 10:47 AM
    Answerer
  • How can I validate files by creating a check so that files are not overwritten if the same file name is supplied in the File.CreateDirectory step?

    Well, I thought you were just afraid to erase files within a directory/folder when issuing File.CreateDirectory operation!

    But you took so long to show up here!

    Anyways, I've tweaked previous code example to find an unused file+number name, so it doesn't overwrite former ones!

    Hope I got it right this time!

    Another option is to use File.AppendContents operation as WhTurner33 already mentioned. It glues new content to the end of a file.

    '______________________________________________________________________'
    Dir  = Program.Directory + "\MyDir\"
    File = "MyFile"
    Ext  = ".txt"
    Num  = 1
    Content = "I'm intact "
    LF = Text.GetCharacter(10)
    
    File.CreateDirectory(Dir)  ' Creating directories does not erase any former contents
    '                                                             if by chance already existed within it
    
    CheckIfFileExists()    ' Search for a free file number slot
    
    File.WriteContents(Path, Content + Num) ' This file name doesn't exist and is free to use
    '______________________________________________________________________'
    Sub CheckIfFileExists
      
      For z = 0 To 0 Step 0  '  Infinite Loop!!!
        z = 1  '  If z > 0 when EndFor is reached, loop breaks free
        
        Path = Dir + File + Num + Ext
        Text = File.ReadContents(Path)
        
        If Text = "" Then
          TextWindow.Write    ("File #" + Num)
          TextWindow.WriteLine(" couldn't be read, was empty or didn't exist!" + LF)
          TextWindow.WriteLine(Path + " is free to use!" + LF + LF)
        Else
          TextWindow.WriteLine(Path + " was found!" + LF)
          TextWindow.WriteLine("Contents:" + LF)
          TextWindow.WriteLine(Text + LF + LF)
          Num = Num + 1   '  Try another file number next time
          z = 0           '  Do not let loop break free!
        EndIf
        
      EndFor
      
    EndSub
    '______________________________________________________________________'


    Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)

    Monday, July 9, 2012 2:13 PM
    Answerer
  • File.WriteContents overwrites the file with new contents.

    File.AppendContents adds contents to the file.

    See description in the IntelliSense Wheel


    Jan [ WhTurner ] The Netherlands

    LEGEND ily.
    Wednesday, July 11, 2012 10:35 AM
  • run this twice.   backupfile is made automatically.

    and see  WQP603-0 (controlbox  textfile load/save )

    PGD= Program.Directory+"\"
    FLST= File.GetFiles(PGD)
    OldContents="abcdefg  123456  hijklmn"

    fname="test_file.txt"
    path=PGD+fname                               '   file  full path
    Newcontents="abcdefg  123456  hijklmn 7890 opqrst"

    If Text.IsSubText(FLST,fname) Then              '  if same file name exists
        TP= Text.GetIndexOf(path,".txt")
        path2= Text.GetSubText(path,1,TP-1)+".bak"     ' backup file name
        File.WriteContents(path2,OldContents)            '   save backup file
        TextWindow.WriteLine(" Back Up file is made!") 
    EndIf
    File.WriteContents(path,Newcontents)               '   save new file

    Wednesday, July 11, 2012 1:11 PM
    Answerer