NT Shell Scripts vs. Batch Scripts

Yesterday I posted a script in the F-Prot support forum. Many people think that .cmd files (such as DDKBUILD.CMD) are no more than renamed batch files. The trick employed in the posted script would not be possible using normal batch scripts. Only the advanced functionality of the NT command interpreter allows this script to perform its work. The problem was to extract the last part of a file/directory path, i.e. everything after the last backslash. Here is how it works.

Excerpt from the script in the forum:

:GetFilePart
:: Get the name of the variable we are working with
setlocal ENABLEEXTENSIONS & set VAR_NAME=%1
set VAR_TEMPRET2=%%%VAR_NAME%%%
:GetFilePartLoop
set VAR_TEMPRET1=%VAR_TEMPRET2%
set VAR_TEMPRET2=%VAR_TEMPRET1%
for /f "tokens=1,* delims=\" %%i in ('echo %VAR_TEMPRET1%') do (
  if not "%%j" == "" set VAR_TEMPRET2=%%j
)
if not "%VAR_TEMPRET1%" == "%VAR_TEMPRET2%" goto :GetFilePartLoop
:: Re-export the variable out of the local scope
endlocal & set %VAR_NAME%=%VAR_TEMPRET1%
goto :EOF

Any questions? :mrgreen:

// Oliver

This entry was posted in EN, Programming. Bookmark the permalink.

2 Responses to NT Shell Scripts vs. Batch Scripts

  1. Luckie says:

    As far as I know under NT *.cmd envokes the 32-Bit command shell where as *.bat invokes the 16-bit command shell. And you only got he advanced “batch” commands with the 32-bit command shell.

  2. Oliver says:

    Not quite right. Simple test script:

    @echo off
    echo Bla
    pause

    Check the process started. On XP it is always CMD.EXE. AFAIK it was the same for Windows 2000.

    // Oliver

Leave a Reply

Your email address will not be published. Required fields are marked *