1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Strange prob with %@replace..

Discussion in 'Support' started by MikeBaas, Jul 29, 2013.

  1. MikeBaas

    Joined:
    Feb 11, 2013
    Messages:
    17
    Likes Received:
    0
    Hi,

    I am working on a small script which is supposed to launch the PHP-File I am working in my webbrowser (after copying it to a temp-dir, if so required). In order to translate "local directory-separators" (\) into "web-friendly dir-separators" (/), I am using %@replace[^\,^/,%fp] - but unfortunately, when applied in a BTM, this empties the variable (whereas it works as expected when executing an echo of that on the command-line). (I am using TC 13 and have also tasted this against 15).

    Where is the bug that I do not see?
    usage: launch_php.btm c:\xampp\htdocs\foo.php


    rem @echo off
    ver
    set fn=%@full[%1]
    iff "%@ext[%fn]"=="php" then
    set fp=%@path[%fn]
    set fpu=%@upper[%fp]
    set i=%@index[%fp,C:\XAMPP\HTDOCS\]
    iff %i lt 0 then
    set newnam =c:\xampp\htdocs\tmp\%@filename[%fn]
    copy %fn %newnam
    set fp=%@path["%newnam%"]
    set fn=%newnam%
    endiff

    set relpth = %@instr[16,,%fn]
    echo relpth=%relpth
    echo repl1=%@replace[^\,^/,%relpth]
    echo repl2=%@replace[^\,^/,"%relpth"]
    echo repl3=%@replace[^\,^/,%relpth%]
    rem ^^^^THIS KILLS "relpth" ^^^

    echo Calling: http://localhost/xampp/%relpth
    pause

    http://localhost/xampp/%relpth
    else
    echo Only supporting PHP-Files currently!
    endiff
     
  2. samintz

    samintz Scott Mintz

    Joined:
    May 20, 2008
    Messages:
    1,188
    Likes Received:
    11
    You shouldn't need the escape characters before the slashes in your @replace calls.
    Also there are 2 places in your script where you have spaces before and after the equals sign. SET is very specific about that sort of thing.
    Code:
    set newnam =c:\xampp\htdocs\tmp\%@filename[%fn]
              ^extra space
    set relpth = %@instr[16,,%fn]
              ^ ^extra spaces
    
    So change your script to:
    Code:
    rem @echo off
    ver
    set fn=%@full[%1]
    iff "%@ext[%fn]"=="php" then
    set fp=%@path[%fn]
    set fpu=%@upper[%fp]
    set i=%@index[%fp,C:\XAMPP\HTDOCS\]
    iff %i lt 0 then
    set newnam=c:\xampp\htdocs\tmp\%@filename[%fn]
    copy %fn %newnam
    set fp=%@path["%newnam%"]
    set fn=%newnam%
    endiff
     
    set relpth=%@instr[16,,%fn]
    echo relpth=%@replace[\,/,%relpth]
     
    echo Calling: http://localhost/xampp/%relpth
    pause
     
    http://localhost/xampp/%relpth
    else 
    echo Only supporting PHP-Files currently!
    endiff 
    
     
  3. MikeBaas

    Joined:
    Feb 11, 2013
    Messages:
    17
    Likes Received:
    0
    Great, that fixed it. Maaaaany thanks! :)
     
  4. Steve Fabian

    Joined:
    May 20, 2008
    Messages:
    3,523
    Likes Received:
    4
    Some more simplification possibilities:
    1/ All string handling, including the variable function @INDEX, are case insensitive, unless explicitly stated. @REPLACE is one of the few exceptions - it is case sensitive. You can just use variable fp instead of fpu.
    2/ You can compound functions, e.g., %@replace[\,/,%@replace[c:\xampp\htdocs\,,%fn]] - but it is more difficult to debug. Note that I used @REPLACE to strip part of the path, eliminating the need to count characters; even better that path string could be put into a variable, making the whole procedure more flexible.

    But it works - you might as well leave it as is, and ignore my post.
     
  5. MikeBaas

    Joined:
    Feb 11, 2013
    Messages:
    17
    Likes Received:
    0
    1/ Actually I never used that fpu anyway! ;-(

    2/ That's nice, too - but then @replace would need fpu! ;-)

    But thanks -it's good to know these things. Although I may forget it till next time anyway ;-)
     

Share This Page