Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: What is this?  (Read 2470 times)

0 Members and 1 Guest are viewing this topic.

Raven19528

    Topic Starter


    Hopeful
  • Thanked: 30
    • Computer: Specs
    • Experience: Experienced
    • OS: Windows 7
    What is this?
    « on: October 06, 2011, 10:55:30 PM »
    Try writing the following two files then run the Input.bat file. Check in the Output.txt file. What is the symbol right after Sum=? What is it's purpose (other than being the symbol used to denote those of a female variety?)

    >>Input.bat<<
    Code: [Select]
    @echo off
    cls
    set envpath=%~dp0
    set /p var1=Enter a number-
    set /p var2=Enter another number-
    cls
    @for /f "tokens=*" %%F in ('Math.bat %var1% %var2%') do set result=%%F
    echo Sum=%result% >Output.txt

    >>Math.bat<<
    Code: [Select]
    @echo off
    cls
    set /a result=%1+%2
    echo %result%

    Also, if anyone knows, why does this symbol show up at all?
    "All things that are
    Are with more spirit chased than enjoy'd" -Shakespeare

    Salmon Trout

    • Guest
    Re: What is this?
    « Reply #1 on: October 07, 2011, 02:08:01 AM »
    FOR is capturing the CLS in math.bat. The unexpected symbol is a form feed (FF) character. CLS sends an FF character - ASCII (decimal) 12 (hex) 0C - to the console. As the FOR command is capturing the output stream of Math.bat I suppose it is getting prepended to the intended string from the echo %result% command at the end of Math.bat. I tested this by creating another batch with only a CLS command in it and processing that after Math.bat and appending the result to Output.txt. In fact parsing a bare CLS command in FOR does the same thing. See below.

    I imagine this is happening because the behaviour of FOR with the /F switch is to treat the dataset (the part in the brackets) as a series of lines to be processed. What you are doing is running Math.bat invisibly and parsing the output line by line, assigning the value of %%F in turn to the variable %result%. You would imagine that Math.bat has only one line of output. Now you would expect that when you exit the loop, that %result% would hold the contents of the last (final) (in this case the only) line processed by FOR /F.You could have a hundred ECHO HELLO WORLDs in Math.bat before the line which echoes %result% and FOR /F will dutifully see them, assign them to %%F and dump each one when the next line is read. So when you exit the loop only the final value of %%F is in %result%.

    What I think is the reason for the behaviour is that unlike a normal command, CLS does not send a newline to the console so FOR does not discard the FF character it picks up.

    I am not sure if you would call this a bug, exactly, more a feature, maybe.

    Possible workarounds:

    1. Since FOR /F processes batch file and program output text streams invisibly, then a CLS in a batch file designed to be so processed is a waste of space. It won't clear anything. So take 'em out.

    2. If you must have a CLS in such a batch file for some reason, put an echo. after it so that FOR /F sees a new line.

    A couple of personal style preferences which you are free to ignore:

    3. In general, as I progressed in batch scripting I tended not to sprinkle CLS commands into my scripts.

    4. Also a sign of a beginner is randomly placed @ characters starting lines when you already have @echo off at the top of the script (it says "copied-and-pasted" quite loudly!)

    a. My Input.bat

    Code: [Select]
    @echo off
    set envpath=%~dp0
    set /p var1=Enter a number-
    set /p var2=Enter another number-
    for /f "tokens=*" %%F in ('Math.bat %var1% %var2%') do set result=%%F
    echo Sum=                  %result% >Output.txt
    for /f "tokens=*" %%G in ('MY-CLS.bat') do set result=%%G
    echo Output of cls.bat=    %result% >> output.txt
    for /f "tokens=*" %%H in ('CLS') do set result=%%H
    echo Output of cls command=%result% >> output.txt
    echo.
    echo This is output.txt:
    type output.txt

    b. My MY-CLS.bat

    Code: [Select]
    @cls
    c. Output: (can you see the ♀ characters?)

    Code: [Select]
    Enter a number-5
    Enter another number-5

    This is output.txt:
    Sum=                  ♀10
    Output of cls.bat=    ♀
    Output of cls command=♀

    Raven19528

      Topic Starter


      Hopeful
    • Thanked: 30
      • Computer: Specs
      • Experience: Experienced
      • OS: Windows 7
      Re: What is this?
      « Reply #2 on: October 07, 2011, 09:29:55 AM »
      Thank you for the insight. Interesting how some things seem so simple when they are explained properly.

      In regards to the cls command being sprinkled throughout, I guess I do so because I like a "clean" console when I run a batch file. It distracts me if too much information gets put on the screen so I tend to cls a lot throughout my programs. Personal preference I guess. The @for was copied from another batch that I wrote that I use as my testing file that has all sorts of lines in it that do various things and allow me to play around with commands. At one point the entire file had @ symbols in front of every line, just to see what would change, if anything. When I copied it over I should have checked that. Oops.  :D

      Anyway, thank you for all of the information. Now I know how to get a ♀ into program if I want to. Now I just need to figure out how to create the male symbol and I'll be all set.  ;D
      "All things that are
      Are with more spirit chased than enjoy'd" -Shakespeare