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

How to pass a character like " " to a function

Discussion in 'Support' started by jalong, May 24, 2010.

  1. jalong

    Joined:
    Apr 11, 2009
    Messages:
    7
    Likes Received:
    0
    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!

     
  2. Charles Dye

    Charles Dye Super Moderator
    Staff Member

    Joined:
    May 20, 2008
    Messages:
    3,307
    Likes Received:
    39
    re: How to pass a character like " " to a function

    I have not tested this, but I suspect that you want something like this:

    Code:
    set x=%@unquote[%1]``
    TCC strips off trailing spaces at the end of a command line....
     
  3. Steve Fabian

    Joined:
    May 20, 2008
    Messages:
    3,520
    Likes Received:
    4
    How to pass a character like " " to a function as a command line argument

    | 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!

    The issue is not with %@char[32] but with SET. As is documented
    in HELP topic "set.htm" SET drops trailing whitespace in the value it
    assigns to the variable, UNLESS it is protected from evaluation by
    the parser. In all versions of the JPsoft command processors (going
    back to 4DOS) you protect strings with back quotes `. All of the
    commands below would work for you:

    set l=` `
    set l= ``
    set l=%@char[32]``
    set l=`%@char[32]`

    The first three set the variable to a single space character; the
    last sets the variable to the string %@char[32] which will be
    converted to a space character each time the parser encounters %l in
    a command. For purposes of understandability, I would recommand
    either of the first two methods.
    There are many simplifications one can make to your batch file,
    not the least of which is the use of the TEXT and QUIT commands, as
    shown below. Of course, this comes from one who is a retired
    professional programmer with over 15 years specific experience with
    JPsoft command processors.

    There are some errors in the program.

    1/ @REPLACE is case sensitive. Windows is case retentive. A file
    associated with extension .txt may actually have any one of the
    following eight extensions: .txt .txT .tXt .tXT .Txt .TxT .TXt .TXT.
    Vince Fatica provided the freeware plugin function @XREPLACE to
    overcome this limitation. Either one works on STRINGS. Consequently
    your second example will replace the extension .txtfile with
    .cmdfile, and rename x.txt.csv to x.cmd.csv.

    2/ Every use of @FINDFIRST allocates internal memory. It must be
    followed by @FINDCLOSE to release that memory, else TCC will
    eventually crash. OTOH you can use either FOR or DO to iterate through
    all file specifications matching the desired wildcard without
    explicit opening and closing the directory search functions.

    Major simplifications are based on the following:

    1/ It is generally much faster to parse the command line as much as
    possible before the loop rather then to repeat the parsing every time.
    Of course, there are cases when this cannot be done.

    2/ IFF is designed so that you would not need to use command groups
    to execute multiple commands once you found condition which is TRUE.
    Just list each command in a separate line, resulting in much more
    readable code.

    :: FCLEAN.BTM
    :: Renames files by replacing *ALL* instances of [Search String] with
    :: [Replacement String] in file names matching [Filespec].
    :: John A. Long

    ======================================
    W A R N I N G ! N O T T E S T E D !

    ======================================
    --- begin code ---
    @echo off

    if %# LT 1 .or. %@ GT 3 .or. %1 == /? goto USAGE

    SETLOCAL

    switch %#
    case 1
    goto LIST
    case 2
    if %2 EQ /T goto LIST
    goto USAGE
    case 3
    iff %2 EQC %3 then
    echo Cannot change string %2 to itself
    quit
    endiff
    switch %2
    case " "
    set src= ``
    default
    set src=%2
    endswitch
    switch %3
    case " "
    set tgt= ``
    default
    set tgt=%3
    endswitch
    endswitch

    SET X=0
    echo.

    do fn in /a:-d %1
    set x=%@inc[%x]
    ren %@quote[%fn] %@quote[%@replace[%src,%tgt,%fn]]
    enddo
    Echo %X files renamed.
    QUIT

    :LIST
    SET X=0
    echo.
    do fn in /a:-d %1
    set x=%@inc[%x]
    echo %fn
    enddo
    Echo %X files would be renamed.
    QUIT

    :USAGE
    text

    FCLEAN.BTM

    Renames files by replacing *ALL* instances of [Search String] with
    [Replacement String] in file names matching [Filespec].

    USAGE
    ====Display usage:

    fclean /?

    List matching files:

    fclean Filespec [/T]

    Replace Search_String with Replacement_String:

    fclean Filespec Search_String Replacement_String

    NOTES:
    Filespec may contain wild cards.

    Use " " to denote a SPACE string.

    In strings special characters such as ^,&,(,( must be proceeded by
    the EscapeChar (default: caret "^"), e.g. ^&.

    USE WITH CAUTION.

    EXAMPLE
    ======The command
    FCLEAN.BTM zz* " " _
    will replace all spaces with underscores in files beginning with zz.

    The command
    FCLEAN.BTM *.txt .txt .cmd
    will replace .txt with .cmd in all filenames including .txt.

    endtext
    --- end of code ---


    --
    HTH, Steve
     
  4. jalong

    Joined:
    Apr 11, 2009
    Messages:
    7
    Likes Received:
    0
    re: How to pass a character like " " to a function

    Steve, there's a lot of good learning for me in the example you've given. I'm going to go over it closely today.

    Thanks so much to both of you!
     

Share This Page