Results 1 to 7 of 7

Thread: strange thing happening....file_save is not overwriting...help file says it should.

  1. #1
    Junior Member
    Join Date
    Mar 2024
    Posts
    17
    Rep Power
    2

    [FIXED, dont look please] strange thing happening....

    strange thing happening....file_save is not overwriting...help file says it should.

    n = FILE_Save(APP_SCRIPTPATH & "test.txt", out_string)

    the above is the lat line of the script function.
    the help file says:

    quote
    If FileName already exists, FileName will be deleted before writing StringBuffer
    /quote

    that is not happening for me. the string is appended (added to) the file at the bottom.
    I expected the file to be over written. the system pauses for 2 seconds during the file write, seemed odd for 10 lines of text, but not critical.

    I was running in debug. i also tried just executing the script also.
    I deleted the file. ran the script 2 times.
    windows 10
    --------------------------------------------------------------------------
    made an example


    calling "save_file" while inside include file will append the text to the file..
    calling "save_file" from the main script file will overwrite the file
    I commented the code in main file and ran the function 2 times, it adds to file
    I un-commented code and commented function and ran 2 times. it over writes.
    pausing in between to the 2 writes a few seconds.
    code in the include file is inside function. code in main script file is not in a function.

    USES "FILE"
    
    Dim FileHandle As DWORD
    Dim Status     As DWORD
    Dim TestString As String
    Dim FileName   As String 
    Dim sMsg       As String
    
    TestString = "1234567890ABCDEF"           ' String to write to file
    FileName   = APP_SCRIPTPATH + "test.txt"  ' Build filename
    
    Status     = FILE_SAVE(FileName, TestString)   ' Save string to File
    
    Attached Files Attached Files
    Last edited by F_Dixon; 26-04-2024 at 05:21. Reason: EDIT to say I fixed it.

  2. #2
    Junior Member
    Join Date
    Mar 2024
    Posts
    17
    Rep Power
    2

    strange...not working when moved to include file..what am i missing.

    I am missing something simple.

    saveTestFile
    with append text to an existing file

    saveTestFile2
    will over write the file

    RichEdit_Check_FileExistsGO("test.txt")
    will work if the if/then is commented out

    fBackupFileIFExits("test.txt
    will work if the if/then is commented out

    RichEdit_Check_FileExistsGO2("test.txt")
    works

    fBackupFileIFExits2("test.txt")
    works

    the function are the same, just 1 set is in an included file.



    the code below

    file1.tbasic
    #include ".\file2.tbasic"
    'USES "Trace"
    USES "FILE"
    
    saveTestFile
          '[breakpoint] <Any note here. Breakpoint will be set to line following this statement>
    saveTestFile2                             ' inthis file
          '[breakpoint] <Any note here. Breakpoint will be set to line following this statement>
    RichEdit_Check_FileExistsGO("test.txt")
          '[breakpoint] <Any note here. Breakpoint will be set to line following this statement>
    fBackupFileIFExits("test.txt")
          '[breakpoint] <Any note here. Breakpoint will be set to line following this statement>
    RichEdit_Check_FileExistsGO2("test.txt")   ' inthis file
          '[breakpoint] <Any note here. Breakpoint will be set to line following this statement>
    fBackupFileIFExits2("test.txt")
          '[breakpoint] <Any note here. Breakpoint will be set to line following this statement>
    
      function RichEdit_Check_FileExistsGO2( sFileName as string) as long
        DIM Message   AS STRING
        
        function = %true
            
        '---If file already exists, ask if to replace
        if file_exists(sFileName) then
    
          Message = "File " & sFileName & "\n\n"
          Message += "already exists. Do yoy want to replace?\n\n"
          DIM lResult AS LONG = MSGBOX(0, Message, %MB_YESNOcancel, "Replace file?")
          IF lResult = %IDNO or lResult = %idcancel THEN
            function = %false
          END IF
        end if
    End Function
    
      
    
    
    Function fBackupFileIFExits2( sPassedFile as string) as long
    ' if file exits make a back up cooy with date
    'msgbox 0, date$(0) & "[" & Time$(2) & "]"
    
    USES "FILE"
    
    dim sTemp as String 
    sTemp =  "_" & date$(0) & "[" & Time$(2) & "].bak"
    '[breakpoint] <Any note here. Breakpoint will be set to line following this statement>
    
    
    If file_exists(sPassedFile)then
      FILE_Copy(sPassedFile,sPassedFile & sTemp)
    End If
    
    
    End Function
    
    
    Function saveTestFile2("test.txt")
    USES "FILE"
    
    
    Dim TestString As String
    Dim Status     As DWORD
    Dim FileName   As String 
    
    TestString = "1234567890ABCDEF"           ' String to write to file
    
    FileName   = APP_SCRIPTPATH + "test.txt"  ' Build filename
    Status     = FILE_SAVE(FileName, TestString)   ' Save string to File
    End Function
    

    file2.tbaisc
    
    
    function RichEdit_Check_FileExistsGO( sFileName as string) as long
        DIM Message   AS STRING
    'USES "FILE"
        
        function = %true
            
        '---If file already exists, ask if to replace
        if file_exists(sFileName) then
    
          Message = "File " & sFileName & "\n\n"
          Message += "already exists. Do yoy want to replace?\n\n"
          DIM lResult AS LONG = MSGBOX(0, Message, %MB_YESNOcancel, "Replace file?")
          IF lResult = %IDNO or lResult = %idcancel THEN
            function = %false
          END IF
        end if
    End Function
    
    
    Function fBackupFileIFExits( sPassedFile as string) as long
    ' if file exits make a back up cooy with date
    'msgbox 0, date$(0) & "[" & Time$(2) & "]"
    'USES "FILE"
    
    dim lStatus as Long
    dim sTemp as String 
    sTemp =  "_" & date$(0) & "[" & Time$(2) & "].bak"
    '[breakpoint] <Any note here. Breakpoint will be set to line following this statement>
    
    
    If file_exists(sPassedFile) then
      FILE_Copy(sPassedFile,sPassedFile & sTemp)
    End If
    
    End Function
    
    
    Function saveTestFile("test.txt")
    'USES "FILE"
    
    Dim TestString As String
    Dim Status     As DWORD
    Dim FileName   As String 
    
    TestString = "1234567890ABCDEF"           ' String to write to file
    
    FileName   = APP_SCRIPTPATH + "test.txt"  ' Build filename
    Status     = FILE_SAVE(FileName, TestString)   ' Save string to File
    End Function
    
    Attached Files Attached Files

  3. #3
    Junior Member
    Join Date
    Mar 2024
    Posts
    17
    Rep Power
    2

    [FIXED, please dont look] note to self...this first lines in a file should be USES

    i had the include file above the USES "FILE" line.

    which cause all sorts of strange things. I did not check every Foot-Gun yet, i am going to have a beer first.

    foot-gun is a reference to shooting myself in the foot.

    program flow is linear because script, doh. really embarrassing. glad no one looked at it yet.

    have pity on my and delete this whole thing. well maybe leave the part about order matters USES are first.
    funny, i did try USES in the included file but it did not help.
    Last edited by F_Dixon; 26-04-2024 at 05:23.

  4. #4
    thinBasic MVPs
    Join Date
    Oct 2012
    Location
    Germany
    Age
    55
    Posts
    1,555
    Rep Power
    174
    Perhaps something was intermixed there.
    Thinbasic has
    × File_Save
    ( core function) then
    × Save_File
    ( also a core function that
    Was long time present to allow
    Saving without the need of the file-Module
    and of course
    ×File_Save
    provided by File-Module

    I would try to put

     
    USES "File"
    
    To force the function of the file-module to be used.
    I suspect a change in Core (File_Append was added) has messed something up.

    But in any case you can do as follows

    (no specific module is needed)
    Begin Const
    $CMD = App.Environ.Comspec & " /c "
    $notPermitted = Chr$(0 to 31, 34,42,47,59,60,62,63,92,124,126,127)
    $needsQuotes = chr$(32 to 45,58 to 64, 91 to 94, o6, 123 to 127)
    End Const
    
    ' direct access the cmd.exe-command-
     '  interpreter from thinbasic:
    Function Exist(Byval sPath as String) As Boolean
    Boolean bNetPath
    ' if a folders existence is to test
    ' the path must end with a backslash
    
    ' you may concetanate strings for the
    ' sPath-parameter without to care about quotes 
    ' or if a backslash was present already, just put 
    ' one inbetween, duplications are removed 
    sPath = Remove$(sPath, $Dq)
    sPath = REPLACE$(sPath, "/" With "\")
    bNetPath =Startswith(sPath,"\\")
    If bNetpath Then sPath = RightF$( sPath, -2)
    
    ' if there is an error because of -2 upon RightF$
    ' use RightF$(sPath, LenF(sPath) - 2)
    
    While instr(1, sPath, "\\")
    sPath =REPLACE$(sPath,"\\", With "\")
    Wend
    If bNetPath then sPath ="\\" & sPath
    
    If instr(1, sPath, any $needsQuotes) then sPath = $Dq & sPath & $Dq
    FUNCTION =("TRUE" = Shell_CaptureOutput( $CMD & " Exist " & sPath & " (ECHO TRUE) ELSE (ECHO FALSE)", "", %SW_Hide, 10))
    End Function
    
    Sub  Write_File(Byval sPath as String, Byval sText As String,  Opt Byval bAppend As Boolean
    String sq, sa
    If Instr(1,sPath, Any $NotPermitted) Then
    MsgBox "Error:" & $Crlf &"Path-Parameter contains invalid char." & $Crlf & $dq & sPath & $Dq, %MB_iconError, Function_name
    Exit Sub
    End If
    If Instr(1, sPath, Any $needsQuotes) then sq =$DQ
    sa = iif$( bAppend, ">>", ">")
    
    
    If Shell_CaptureOutput( StrFormat$("{1} (Echo {2})>{3}{4}{5}{4}",  $Cmd, sText, sa, sq, sPath), "", %SW_Hide, 10) then Nop
    
    End Sub
    
    It may be difficult to pass more than a few verbs because ^ (circonflex) is the escape-char that needs to be added before newline in order to escspe from end of line and to continue on the following line. We would just put the string in quotes - that won't work here.
    But anyway the funtion can make you file an empty file when you pass an empty string without a value for bAppend.
    Last edited by ReneMiner; 04-05-2024 at 20:45.
    I think there are missing some Forum-sections as beta-testing and support

  5. #5
    Junior Member
    Join Date
    Mar 2024
    Posts
    17
    Rep Power
    2

    I think what i figured out was....

    I think what i figured out was that I needed to have the USES "File" in the starting file.

    I am still feeling out how things work. I had put the uses "file" inside the function and in an included file that was going to use it. vs in the main file. Once I rearranged the uses into the top of the main file the issue went away.
    I am pretty sure that the way thinbasic works places uses later in the script in asking for trouble.

    im guessing that with no intermediary code step I more or less confused the plumbing and flow of how things work.
    new- guy mistake. i made assumptions, and caused myself pain. all of the sample code showed me how to do it.
    I should have paid attention.

    I will look at you example when i get home. thanks.

    tldr.
    I put the using "file" in the wrong place. inside a function inside an include file.
    just as a organizing idea, so i would not forget it later, to keep from having a problem because i forgot it.
    i was trying to be smart, ,i should know better.

  6. #6
    thinBasic MVPs
    Join Date
    Oct 2012
    Location
    Germany
    Age
    55
    Posts
    1,555
    Rep Power
    174
    As suspected @Eros note: File_Append in core interchanged some condition of File_Save (also core)
    I think there are missing some Forum-sections as beta-testing and support

  7. #7
    Junior Member
    Join Date
    Jun 2023
    Posts
    7
    Rep Power
    2
    When you call the save function from the include file, it might act differently than in the main script. Check how you're calling it and make sure there are no lingering references to the file.

Similar Threads

  1. May I suggest a little thing.
    By dco045 in forum thinBasic General
    Replies: 1
    Last Post: 07-02-2024, 07:48
  2. the github-thing
    By ReneMiner in forum thinBasic General
    Replies: 4
    Last Post: 30-08-2020, 14:53
  3. CHOOSE and CALL used to do the same thing
    By marcuslee in forum Sources, Templates, Code Snippets, Tips and Tricks, Do you know ...
    Replies: 1
    Last Post: 09-09-2008, 07:01
  4. Intel Atom: the next big thing
    By ErosOlmi in forum Technology
    Replies: 1
    Last Post: 30-05-2008, 23:22
  5. Every Thing But VB6?
    By sfbm in forum Other languages SDK development
    Replies: 6
    Last Post: 06-01-2008, 22:38

Members who have read this thread: 5

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •