none
More questions on text file manipulation

    Dotaz

  • Hi guys,


    I'm using [System.IO.File]::ReadAllText to read text files to a variable in one of my scripts and I'm having a bit of an issue which I'm sure is a breeze to fix...

     

    The files I'm reading have variables of their own set like this "$build_server_ip", but they aren't Powershell variables (other applications are reading and swapping out text and use these tags). If I try a replace like this:

     

    $document = "c:\document.txt"

    $ip = "10.0.0.1"

    $a = [System.IO.File]::ReadAllText($document)

    $b = $a.replace($build_server_ip, $ip)

     

    I get the following:

     

    Exception calling "Replace" with "2" argument(s): "String cannot be of zero length.

    Parameter name: oldValue"

     

    I'm guessing that $build_Server_ip is a null value as far as Powershell is concerned (which is fine and makes sense) but I don’t want to and can't really change the format of the tags. Is there a way of setting $build_server_ip to a value in powershell and then import the text file, swapping all $ variables out for their Powershell counterparts (did that make sense)? Or perhaps a simpler solution? Any help would be appreciated.

     

    Cheers,

     

    Nick

    2. prosince 2008 18:35

Odpovědi

  • How about like this:

    PS > gc document.txt
    foo$build_server_ipbar
    PS > $a = [System.IO.File]::ReadAllText($document)
    PS > $a.replace("`$build_server_ip",$ip)
    foo10.0.0.1bar

    I quoted the first argument, and escaped any special characters.
    2. prosince 2008 19:25
    Moderátor