Command inside for loop in a batch file is not working.


  • Hi All,

    Can anyone shed any light why the follwoing line would work on machine but not on another:

    for /f "tokens=2 delims=: " %%i in ('dir .') do set _OS_BUILDNUMBER=%%i

    I get this error while execuing this from a file containing only this line:


    C:\Users\User1>for /F "tokens=2 delims=: " %i in ('dir') do set _OS_BUILDNUMBER=%i
    'dir' is not recognized as an internal or external command,
    operable program or batch file.

    But it works fine in another machine with same OS version.

    Thanks. Any help is aprreciated.


    This is just an example. Practical applicability of the line is not expressed here.


    Tuesday, November 16, 2010 12:51 AM

All replies

  • My only guess is that extensions have been disabled on the non-working machine.

    setlocal /? has a code snippet that can be used to check whether extensions are enabled or not.

    The SETLOCAL command will set the ERRORLEVEL value if given
    an argument.  It will be zero if one of the two valid arguments
    is given and one otherwise.  You can use this in batch scripts
    to determine if the extensions are available, using the following
        VERIFY OTHER 2>nul
        IF ERRORLEVEL 1 echo Unable to enable extensions

    There is also more info in cmd /?

    Command Extensions are enabled by default.  You may also disable
    extensions for a particular invocation by using the /E:OFF switch.  You
    can enable or disable extensions for all invocations of CMD.EXE on a
    machine and/or user logon session by setting either or both of the
    following REG_DWORD values in the registry using REGEDT32.EXE:
        HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor\EnableExtensions
        HKEY_CURRENT_USER\Software\Microsoft\Command Processor\EnableExtensions
    to either 0x1 or 0x0.  The user specific setting takes precedence over
    the machine setting.  The command line switches take precedence over the
    registry settings.

    Thursday, January 17, 2013 7:01 PM