Results 1 to 4 of 4

Thread: Batch scripting

  1. #1
    2 Star Lounger
    Join Date
    Aug 2014
    Posts
    123
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Batch scripting

    Trying to understand how batch passes by reference and
    uses the %~# where # is a number.
    Tried following the examples in several tutorials but
    whatever I do seems to fly in the face of the those
    examples. Trying to do this without getting into
    use of disabling of expansion.
    The test input file is:
    XXXX any text at all
    Space separated text for n-number of lines
    YYYY
    Where XXXX is the first lines unique id and YYYY is
    always the same last line for all text sets.
    The file is meant to be read line by line.
    Here is what I have so far and output seems to indicate
    a problem with the "set" command in passing values.
    This is on a Windows 8.1 system.
    Code:
    cls
    set /a nam="HOME"
    set /a knt="0"
    echo TESTAA nam=%nam%  knt=%knt%
    for /F "tokens=*" %%A in (aa.txt) do (
        echo "MAIN-TESTBB=%%A"	
        echo "MAIN-TESTCC %6"
        set /a rtd="%nam%"
        call :doit %%A %nam% %rtd%
        if "%rtd%" == "3" echo "Found = %nam%" & goto END
        set /a knt=knt+1
        echo "LINE %knt% =%%A ..."
    )
    
    :doit
    echo IN doit
    SETLOCAL
    set /a lin="%~1"
    set /a fnd="%~2"
    set /a rtd=:%~3"
    ENDLOCAL
    echo.%lin% | find /I "%fnd%" > nul && (
       echo "Found %fnd% in"
       set /a %~3="3"
    echo "TESTOO %~2"
       ) || (
       echo "Did not find %fnd%"
       )
    goto :eof
    
    :END
    echo "LINE %knt% =%%A %%B %%C ..."
    echo "
    pause
    Thanks for UR help.
    Last edited by spark55; 2014-11-17 at 06:25.

  2. #2
    WS Lounge VIP
    Join Date
    Dec 2009
    Location
    Earth
    Posts
    8,191
    Thanks
    48
    Thanked 986 Times in 916 Posts
    You are using "set /a" incorrectly. Where you are not evaluating anything, just setting initial conditions, use "set". "set /a knt=knt+1" is the only correct use because you want it to evaluate - add 1 to knt - although it's quicker to do "set /a knt+=1".
    You have not used a "goto :EOF" before your label "doit". This will cause the batch file to loop weirdly.

    cheers, Paul

  3. #3
    Super Moderator BATcher's Avatar
    Join Date
    Feb 2008
    Location
    A cultural area in SW England
    Posts
    3,420
    Thanks
    33
    Thanked 195 Times in 175 Posts
    if you want to look into "passing by reference" the main source I have found are the example subroutines of Ritchie Lawrence. Here are the Date and Time ones. They have a tiny calling routine at the top of each.

    Since he has compressed out lots of spaces, you need to read them Vewwy Carefuwwy.
    Last edited by BATcher; 2014-11-17 at 15:45.
    BATcher

    Time prevents everything happening all at once...

  4. #4
    WS Lounge VIP
    Join Date
    Dec 2009
    Location
    Earth
    Posts
    8,191
    Thanks
    48
    Thanked 986 Times in 916 Posts
    I didn't know you had a lisp!

    cheers, Paul

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •