Page 1 of 1

mystery error in XP - but none in Win7

Posted: 09 Mar 2016 23:40
by mirrormirror
As the title says - XP error - but win7 none.
Notice the lines in this output example like "The system cannot find the file 쭀ŷ♣☼ŔЈ ":

Code: Select all

C:\>FOR /F "tokens=*" %A IN ("      ?/cmd: "shUser=user1"; "shPrivs=none"") DO SET tmpLn=%A

C:\>SET tmpLn=?/cmd: "shUser user1"; "shPrivs none"
The system cannot find the file Ѐ♫♫€ѠǨ§鿠Ś.

C:\>FOR /F "tokens=*" %A IN ("      ?/cmd: "shUser=user1"; "shPrivs=none"") DO SET tmpLn=%A

C:\>SET tmpLn=?/cmd: "shUser user1"; "shPrivs none"
The system cannot find the file Ѐ       ♫.

C:\>FOR /F "tokens=*" %A IN ("      ?/cmd: "shUser=user1"; "shPrivs=none"") DO SET tmpLn=%A

C:\>SET tmpLn=?/cmd: "shUser user1"; "shPrivs none"
The system cannot find the file Ј*♫8Хˈ§逈ƈrrLevel!.

C:\>set tmpLn
tmpLn=?/cmd: "shUser user1"; "shPrivs none"

the string in the FOR loop is padded with an unknown number of [tabs] and [spaces] at the beginning and sometimes at the end. The code above is my attempt to TRIM this whitespace. Now the code actually works fine even though it throws an error - notice that the [tmpLn] variable gets set properly.
I just can't figure out why it throws these errors on SOME lines - most are fine. Every line with an error begins with some [whitespace][?/text]. And every error throws different gibberish (i.e. cannot find the file ƈE]) - usually :).

Any thoughts ?

Re: mystery error in XP - but none in Win7

Posted: 10 Mar 2016 00:20
by ShadowThief
What does your actual code look like? The last time I saw something like this, the person was trying to use a for loop to process a text file that was encoded in UTF-8.

Re: mystery error in XP - but none in Win7

Posted: 10 Mar 2016 02:42
by b0gus
mirrormirror, on XP try this code:

Code: Select all

FOR /F "usebackq tokens=*" %A IN ('   ?/cmd: "shUser=user1"; "shPrivs=none"') DO SET tmpLn=%A

Re: mystery error in XP - but none in Win7

Posted: 10 Mar 2016 12:28
by dbenham
@mirrormirror

You have stumbled across a really nasty XP specific FOR /F bug. FOR /F cannot safely parse an IN() clause string on XP if it contains a token delimiter that is both unquoted and un-escaped, as explained at viewtopic.php?f=3&t=1972#p9062.

The problem token delimiters that must be escaped or quoted within an XP FOR /F string are:
  • space
  • tab
  • comma
  • semi-colon
  • equal sign
  • 0xFF (non-breaking space)

I believe you have 4 choices:

1) Abandon XP and use a more modern version of Windows - Probably not what you want to hear

2) Suppress the error message by redirecting stderr to NUL, and hope for the best:

Code: Select all

C:\>2>nul ( FOR /F "tokens=*" %A IN ("      ?/cmd: "shUser=user1"; "shPrivs=none"") DO SET tmpLn=%A )
This technique was invented by Ed Dyreen, and he seems to have had success without any ill effects. But I don't like it, because it seems as though random memory is being accessed, and I don't trust that it will always be benign.

3) Escape the unquoted token delimiters, perhaps easier said than done if the strings are dynamic.

Code: Select all

C:\>FOR /F "tokens=*" %A IN ("      ?/cmd: "shUser^=user1"; "shPrivs^=none"") DO SET tmpLn=%A

4) Assuming you are in a batch file, then you could transfer the string to a variable and use delayed expansion. I haven't tested this, but I'm pretty sure it will work.

Code: Select all

@echo off
setlocal
set "str=      ?/cmd: "shUser=user1"; "shPrivs=none""
setlocal enableDelayedExpansion
FOR /F "tokens=*" %%A IN ("!str!") DO (
  endlocal
  SET tmpLn=%%A
)


Dave Benham

Re: mystery error in XP - but none in Win7

Posted: 11 Mar 2016 17:52
by mirrormirror
Thanks for the responses from all. And thank you Dave for the detailed reply and informative links - I believe you pinpointed my issue.
1) Abandon XP and use a more modern version of Windows - Probably not what you want to hear

I laughed when I read that - because my script handles user/share/group permissions and I had a working version on Win7 but it needed to be cross-compatible with XP. When I tried it on XP it broke because some of the tools/syntax is different so I spent a good amount of time just making it work for XP :)
2) Suppress the error message by redirecting stderr to NUL, and hope for the best:

For now I'll probably just do this. In reading through some of the posts you linked, I never came across someone actually reporting a "problem" as a result of this error - just an annoying error message. Is this correct?
4) Assuming you are in a batch file, then you could transfer the string to a variable and use delayed expansion. I haven't tested this, but I'm pretty sure it will work.
Code:

@echo off
setlocal
set "str= ?/cmd: "shUser=user1"; "shPrivs=none""
setlocal enableDelayedExpansion
FOR /F "tokens=*" %%A IN ("!str!") DO (
endlocal
SET tmpLn=%%A
)

I'm making a note of this for future use.