Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

Strange prob with %@replace..

Feb
64
1
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
 
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
 
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.
 
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 ;-)
 

Similar threads

Back
Top