First, I'm not a programmer, so go easy on me! I'm using TCMD v11.00.48 x64.
I've been struggling to figure out how to get a space character passed from the command line to @replace in a batch file. @replace works fine when it's written like %@replace[ ,_,text]. But it won't work if I pass " " from the command line and then use @unquote, such as %@replace[%@unquote[%1],%2,%3]. For example, what happens is:
SET 1=" "
SET x=@unquote[%1]
ECHO %x
In this case X seems to have null value. I also tried to directly send the character to a variable, as in:
SET 1=%@char[32]
%@replace[%1,%2,%3].
But %@char[32] seems to return a null value. I have worked around it by testing for " " and making a special version of @replace for that situation, but I can't help but feel that there must be a more elegant way to handle " " on the command line. What follows is what I've written so far (and it does work!). Thanks!
I've been struggling to figure out how to get a space character passed from the command line to @replace in a batch file. @replace works fine when it's written like %@replace[ ,_,text]. But it won't work if I pass " " from the command line and then use @unquote, such as %@replace[%@unquote[%1],%2,%3]. For example, what happens is:
SET 1=" "
SET x=@unquote[%1]
ECHO %x
In this case X seems to have null value. I also tried to directly send the character to a variable, as in:
SET 1=%@char[32]
%@replace[%1,%2,%3].
But %@char[32] seems to return a null value. I have worked around it by testing for " " and making a special version of @replace for that situation, but I can't help but feel that there must be a more elegant way to handle " " on the command line. What follows is what I've written so far (and it does work!). Thanks!
:: FCLEAN.BTM
:: Renames files by replacing *ALL* instances of [Search String] with [Replacement String] in file names matching [Filespec].
:: John A. Long
@echo off
SETLOCAL
UNSET *
SET X=0
echo.
SET fn=%@findfirst[%1,-d]
DO until "%fn" == ""
IFF "%1" == "/?" .OR. "%1" == "" THEN
(GOTO PARAMETERS)
ELSEIFF "%2" == "/T" .OR. "%2" == "" THEN
(echo %fn & set fn=%@findnext[%1,-d] & ITERATE)
ELSEIFF %2 == " " .AND. %3 != " " THEN
SET newfn=%@replace[ ,%3,%fn]
ELSEIFF %2 != " " .AND. %3 == " " THEN
SET newfn=%@replace[%2, ,%fn]
ELSEIFF %2 == " " .AND. %3 == " " THEN
SET newfn=%@replace[ , ,%fn]
ELSE
SET newfn=%@replace[%2,%3,%fn]
ENDIFF
IF %fn != %newfn .AND. %2 != "/t" (ren "%fn" "%newfn" & SET X=%@eval[%x + 1])
echo.
set fn=%@findnext[%1,-d]
ENDDO
Echo.
Echo %X files processed.
GOTO END
:PARAMETERS
echo.
echo FCLEAN.BTM
echo Renames files by replacing *ALL* instances of [Search String] with [Replacement String] in file names matching [Filespec].
echo.
echo USAGE
echo =====
echo Display parameters: fclean /?
echo Test Filespec on filenames: fclean [Filespec] [/T]
echo Replace Search String with Replacement String: fclean [Filespec] [Search String] [Replacement String]
echo.
echo NOTE: [Filespec] may contain wild cards. Use " " to denote a SPACE.
echo Special characters such as ^,&,(,( must be proceeded by a "^", as in ^&.
echo Use with caution.
echo.
echo EXAMPLE
echo =======
echo The command FCLEAN.BTM zz* " " _
echo will replace all spaces with underscores in files beginning with zz.
echo.
echo The command FCLEAN.BTM *.txt .txt .cmd
echo will replace file extension .txt with extension .cmd in all files with the extension .txt.
echo.
:END
ENDLOCAL