1

Any ideas on why the below type'd batch file's for /f loop would be not displaying the first character of the second token (*) on Windows XP (only)?

NOTE: set "test=%%a" is to solve the issue with ping ¿only putting in a <CR> instead of <CR><LF> in it's output? on Windows XP as realised by jeb in this comment to my answer

Community
  • 1
  • 1
user66001
  • 774
  • 1
  • 13
  • 36

2 Answers2

2

It appears that in Windows XP, the ping command generates and extra 0x0D (Carriage Return) character just before the end of line. As a result, when %%b receives a value, it receives it together with the trailing CR char. And when you are printing %%b, the CR naturally causes the "carriage" to return to the beginning of the line, and so the closing " and the subsequent space character (the one between " and &) overwrite the first two characters already printed, i.e. the opening " and the value's first letter.

You can verify it easily by redirecting the output of ping to a file and then viewing the file in a hex viewer. I used Far Manager's built-in viewer to establish the fact:

  • this is the file's normal view with an arbitrary line ending highlighted:

    enter image description here

  • this is the same file in the hex view mode, and the same area is highlighted here as well to show what characters the line actually ends with:

    enter image description here

Sorry about the non-English contents, haven't got an English Win XP handy. Hopefully, the issue should still be apparent from the shots.

Community
  • 1
  • 1
Andriy M
  • 76,112
  • 17
  • 94
  • 154
1

Interesting. Based on what gets output without the @echo off, it appears to be a difference between when the variables get evaluated (as part of the "outer" shell or the "inner" one, I suspect).

However, if you pass the parameters to a function as follows, it appears to work okay in both XP and Win7:

@echo off
    goto :main

:xyzzy
    if "x%1"=="x""" goto :eof
    echo %1
    echo %2
    echo.
    goto :eof

:main
    echo.&echo.
    ping -n 1 google.com
    echo.&echo.
    for /f "tokens=1*" %%a in ('ping -n 1 google.com') do call :xyzzy "%%a" "%%b"
    echo.&echo.

You'll notice I've also ditched blank lines in the xyzzy function since they seem to add no value. If you really want them output, just remove the first line of that function. The output of that script has the first character preserved, unlike the original script:

Pinging google.com [74.125.225.105] with 32 bytes of data:

Reply from 74.125.225.105: bytes=32 time=253ms TTL=43

Ping statistics for 74.125.225.105:
    Packets: Sent = 1, Received = 1, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 253ms, Maximum = 253ms, Average = 253ms


"Pinging"
"google.com [74.125.225.105] with 32 bytes of data:"

"Reply"
"from 74.125.225.105: bytes=32 time=267ms TTL=43"

"Ping"
"statistics for 74.125.225.105:"

"Packets:"
"Sent = 1, Received = 1, Lost = 0 (0 loss),"

"Approximate"
"round trip times in milli-seconds:"

"Minimum"
"= 267ms, Maximum = 267ms, Average = 267ms"
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • Thanks paxdiablo. Can you elaborate on what you suspect (i.e., "...(as part of the "outer" shell or the "inner" one...")? as I did look at the output without `@echo off` and had no clue as to what was going on. Also, in looking at your edit, can you explain why you ¿had to? switch around the functions? – user66001 Feb 04 '13 at 08:28
  • @user66001, only that the debug stuff had real values for %%test%% but Win7 and XP had different value for %%b (valid and blank respectively) - that's what led me to believe it was an interpretation time issue. I didn't actually _have_ to switch the functions, I just (coming from C) prefer `main` at the bottom. It works equally well both ways but, with `main` first, you have to end it with `goto :eof`. – paxdiablo Feb 04 '13 at 08:31
  • A "interpretation time issue" (Sorry for not understanding)? ; Fair enough - Saves a line of code anyways :) – user66001 Feb 04 '13 at 08:35
  • I'll try post some more info tomorrow if you wish. End of day today and I haven't had my XP VM up for so long it's doing 105 updates, so out of action for a bit :-) – paxdiablo Feb 04 '13 at 09:01
  • No probs, @user66001, now that Andriy has nailed it, I don't need to do any more analysis. One more example of problems disappearing if you just ignore them long enough :-) – paxdiablo Feb 04 '13 at 10:38
  • LOL. Indeed. Thanks paxdiablo. – user66001 Feb 04 '13 at 10:56