Sidebar



en:objects:textfile:file_work:set_string_to_file

set_string_to_file

set_string_to_file($file,$str,$line,$add=true,$timeout=COMMAND_TIME); - set (change or add) the specified string in the file

The function accepts parameters as input:

  • $file – path to the file where the string should be set. It can be either absolute or relative (relative to the script folder).
  • $str – the string to set in the file
  • $line – the number of the specified line in the file (line numbering, as everywhere, starts from zero)
  • $add – the string will be added to the file (true) or replace the old string (false)
  • $timeout – the maximum time to perform this operation (in seconds), by default - 60 seconds, if the operation is not completed during this time, the script continues to run further.

    After testing, the function returns the result of its work to the script :
  • true – operation completed (boolean)
  • false – operation failed (boolean)

    PHP example:

    <?php $xhe_host = "127.0.0.1:7010";
     
    //connect the object to control the emulator, if not already connected
    if(!isset($path))
       $path = "../../../Templates/xweb_human_emulator.php";
    require($path);
     
    //Start
    echo "<hr> <font color = blue> textfile->".basename(__FILE__)."</font> <hr>";
     
    $file = "test \ test_insert.txt";
    $file_os->delete($file);
     
    //1
    echo("1.Add lines to the beginning of the file file: <br> <br>");
    for($i = 0; $i <5; $i ++)
    echo($textfile->set_string_to_file($file, "line #" + $i + " n", 0)."<br>");
     
    //2
    echo("<br> 2.Replace the line at the beginning of file: <br> <br>");
    echo($textfile->set_string_to_file($file, "line # 100  n", 0, false)."<br>");
     
    //show the result
    $app->shell_execute("", $file);
     
    //end
    echo "<hr> <br>";
     
    //Quit
    $app->quit();
    ?>

    Python example:

    # Additional paths
    import sys
    sys.path.insert(0, '../../../Templates PY/')
     
    xhe_host = "127.0.0.1:7011"
    from xweb_human_emulator import *
     
    # Start
    echo("<hr> <font color = blue> textfile.xxxxxxxxx </font> <hr>")
     
    file = "test \ test_insert.txt"
    file_os.delete(file)
     
    # 1
    echo("1.Add lines to the beginning of file: <br> <br>")
    for i in range(0,4):
    echo(textfile.set_string_to_file(file, "line #" + str(i) + "
    ", 0)," <br> ")
     
    # 2
    echo("<br> 2.Replace the line at the beginning of file: <br> <br>")
    echo(textfile.set_string_to_file(file, "line # 100
    ", 0, false)," <br> ")
     
    # show the result
    app.shell_execute("", file)
     
    # end
    echo("<hr> <br>")
     
    # Quit
    app.quit()

    C# example:

    #region using
     
    using System;
    using System.Diagnostics;
    using System.Collections.Generic;
    using System.Linq;
    using System.IO;
    using System.Text;
    using System.Threading;
     
    using XHE;
    using XHE.XHE_DOM;
    using XHE.XHE_System;
    using XHE.XHE_Window;
    using XHE.XHE_Web;
     
    #endregion
     
     class Program: XHEScript
     {
    static void Main(string [] args)
    {
    //init XHE
    server = "127.0.0.1:7010";
    InitXHE();
     
    //Start
    echo("<hr> <font color = blue> textfile.get_all_external_inner_texts_and_hrefs </font> <hr>");
     
    string file = "test \ test_insert.txt";
    file_os.delete(file);
     
    //1 step
    echo("1.Add lines to the beginning of the file file: <br> <br>");
    for(int i = 0; i <5; i ++)
    echo(textfile.set_string_to_file(file, "line #" + i + " n", 0) + "<br>");
     
    //step 2
    echo("<br> 2.Replace the line at the beginning of file: <br> <br>");
    echo(textfile.set_string_to_file(file, "line # 100  n", 0, false) + "<br>");
     
    //show the result
    app.shell_execute("", file);
     
    //end
    echo("<hr> <br>");
     
    app.quit();
    }
    }

    Java Script example:

    //connect the object to control the emulator, if not already connected
    xhe_host = "127.0.0.1:7010";
    echo = require("../../../Templates JS/xweb_human_emulator.js");
     
    //Start
    echo("<hr> <font color = blue> textfile.set_string_to_file </font> <hr>");
     
    file = "test \ test_insert.txt";
    file_os.delete(file);
     
    //1 step
    echo("1.Add lines to the beginning of the file file: <br> <br>");
    for(i = 0; i <5; i ++)
    echo(textfile.set_string_to_file(file, "line #" + i + " n", 0) + "<br>");
     
    //step 2
    echo("<br> 2.Replace the line at the beginning of file: <br> <br>");
    echo(textfile.set_string_to_file(file, "line # 100  n", 0, false) + "<br>");
     
    //show the result
    app.shell_execute("", file);
     
    //end
    echo("<hr> <br>");
     
    //Quit
    app.quit();
  • en/objects/textfile/file_work/set_string_to_file.txt · Last modified: 2026/08/05 22:52 (external edit)