Search code examples
windows-7batch-filewindows-server-2003

Batch filre runs on Win erver 2003 but not on Windows 7


I have the following batch script:

@echo off
FOR /F "delims=|" %%I IN ('DIR "*.*" /B /O:D') DO (SET %NewestFile=%%I)
echo %NewestFile%

on a Windows 2003 Server it works as expected and outputs the newest file

IntelMPFilesPath=C:\Programme\Intel\NGSMS\MPFiles
IntelMPFilesPath=C:\Programme\Intel\NGSMS\MPFiles
test.bat

but running the same batch file on Windows 7 gives the following:

Die Umgebungsvariable "I" ist nicht definiert.
Die Umgebungsvariable "I" ist nicht definiert.
Die Umgebungsvariable "I" ist nicht definiert.
Die Umgebungsvariable "I" ist nicht definiert.
ECHO ist ausgeschaltet (OFF).

Does anybody know if there is that big of a difference between the Win7 and the Srv2003 command shell?


Solution

  • I don't know why it's working in Server 2003. You have an extra %.

    You should remove the % in SET %NewestFile:

    @echo off
    FOR /F "delims=|" %%I IN ('DIR "*.*" /B /O:D') DO (SET NewestFile=%%I)
    echo %NewestFile%