mystery error in XP - but none in Win7

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
mirrormirror
Posts: 129
Joined: 08 Feb 2016 20:25

mystery error in XP - but none in Win7

#1 Post by mirrormirror » 09 Mar 2016 23:40

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 ?

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: mystery error in XP - but none in Win7

#2 Post by ShadowThief » 10 Mar 2016 00:20

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.

b0gus
Posts: 7
Joined: 02 Mar 2016 12:58

Re: mystery error in XP - but none in Win7

#3 Post by b0gus » 10 Mar 2016 02:42

mirrormirror, on XP try this code:

Code: Select all

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

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: mystery error in XP - but none in Win7

#4 Post by dbenham » 10 Mar 2016 12:28

@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

mirrormirror
Posts: 129
Joined: 08 Feb 2016 20:25

Re: mystery error in XP - but none in Win7

#5 Post by mirrormirror » 11 Mar 2016 17:52

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.

Post Reply