Microsoft KB Archive/318689

= A Logon Script Does Not Work If %0 or %0\..\ Calls Multiple Commands =

Article ID: 318689

Article Last Modified on 4/29/2003

-

APPLIES TO


 * Microsoft Windows XP Professional Edition

-



This article was previously published under Q318689



SYMPTOMS
Logon scripts that run in Windows XP may not work if %0 or %0\..\ is used in the script. The specific error message that appears depends on the command that follows %0 or %0\..\. A typical error message is &quot;File not found.&quot; If the batch file calls a Kixtart script, the error message is &quot;Script Error: failed to find/open script!&quot;.



CAUSE
This problem occurs because Windows XP expands the %0 variable differently from previous versions of Windows.



RESOLUTION
To resolve this problem, use one of the following methods.

Method 1
0% is not required for Microsoft Windows NT, Microsoft Windows 2000, or Windows XP. It is required for Microsoft Windows 95, Microsoft Windows 98, and Microsoft Windows Millennium Edition (Me).

Use a logon script that detects the version of Windows and branches to the version-dependent section. This article contains two examples. In the first example, the OS variable is detected from Windows NT, Windows 2000, and Windows XP. Windows 95, Windows 98, and Windows Me do not have this variable defined. If the OS variable is not defined, Window 95, Windows 98, or Windows Me is assumed.

@echo off

Rem Branched startup script for Kixtart.

Rem Windows XP drops all parameters after EXE if %0 is used.

Rem %0\..\ is only required for Windows 95, 98, and Me.

Rem Detect OS (NT or other)

if (%OS%) == (Windows_NT) goto NT_XP


 * 9x

%0\..\kix32.exe myscript.scr

goto end


 * NT_XP

kix32.exe myscript.scr


 * end

In the next sample script, ver and find are used to detect the Windows version. This method requires a section for all of the VER results, but is a more robust approach.

@ECHO off

SETLOCAL

VER | find &quot;NT&quot; > nul

IF not errorlevel 1 GOTO Win_NT

VER | find &quot;2000&quot; > nul

IF not errorlevel 1 GOTO Win_NT

VER | find &quot;XP&quot; > nul

IF not errorlevel 1 GOTO Win_NT

VER | find &quot;98&quot; > nul

IF not errorlevel 1 GOTO Win_9X

VER | find &quot;95&quot; > nul

IF not errorlevel 1 GOTO Win_9X

GOTO unknown_os


 * win_NT

call kix32.exe myscript.scr

goto end


 * win_9X

%0\..\kix.exe %0\..\kix32.exe myscript.scr

goto end


 * unknown_os


 * end

Method 2
The resolution in Method 1 requires that all commands that use the %0 or %0\..\ syntax be maintained in two forms. This requires dual code paths in the script. You can use the following resolution to avoid this code duplication. Put the following commands before the first use of %0: echo %0 | find &quot;&quot;&quot;&quot; > nul if errorlevel 1 goto NOQUOTES if errorlevel 0 goto HASQUOTE


 * NOQUOTES

set PERCENT0=%0 goto CONTINUE


 * HASQUOTE

set PERCENT0=%~0 goto CONTINUE


 * CONTINUE

After you do this, find all occurrences of %0 and replace them with %PERCENT0%.

These commands pipe the contents of %0 through FIND to detect the quotation marks. Four quotation marks are required because two are required to enclose the text to be tested by the FIND command, one is the text that is being tested, and one is required to escape the quotation mark being tested. If quotation marks are present, %0 is expanded by using the %~I syntax to remove surrounding quotation marks, and this is set to a new variable, PERCENT0.

The %~I syntax works on Windows 2000 and later. For details, see the Windows 2000 or Windows XP FOR command Help topic.

If the quotation marks are not present, PERCENT0 is set to %0. This allows existing logon scripts and batch files leveraging %0 to be used almost unchanged.



STATUS
Microsoft has confirmed that this is a problem in the Microsoft products that are listed at the beginning of this article.



MORE INFORMATION
When the %0 variable is expanded in Windows XP, the result is enclosed in quotation marks. The first file runs, but a second command on the same line is dropped. The following sample Kixtart script is started from a batch file. The logon script that is called is named Logon.bat, and contains the following line:

%0\..\kix32.exe myscript.scr

In Windows NT, Windows 2000, Windows 95, Windows 98, and Windows Me, when the variable is expanded, this is converted to:

\\mylogonserver\netlogon\logon.bat\..\kix32.exe myscript.scr

In Windows XP, the expanded variable is converted to:

&quot;\\mylogonserver\netlogon\logon.bat&quot;\..\kix32.exe myscript.scr

Kix32.exe is run, but Myscript.scr is not passed to Kix32.exe.

This behavior occurs when the command is run from a logon script or from the Run dialog box. This behavior does not occur if the command is run from a command prompt.

Additional query words: percent zero

Keywords: kberrmsg kbprb KB318689

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.