Batch script collection (outsourced)

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Locked
Message
Author
aGerman
Expert
Posts: 3518
Joined: 22 Jan 2010 18:01
Location: Germany

Batch script collection (outsourced)

#1 Post by aGerman » 19 Sep 2016 10:56

This script belongs to the sticky post "How to get help for a batch script - quickly!". For its purpose and usage see
http://www.dostips.com/forum/viewtopic. ... 091#p49091

info.zip
(4.18 KiB) Downloaded 80 times

Code: Select all

:: INFO.BAT version 1.5
:: source http://www.dostips.com
:: Bugs and suggestions can be reported in thread http://www.dostips.com/forum/viewtopic.php?f=3&t=7347
::
:: Release History:
::  2018-05-03 v1.5: More reliable OEM codepage determination (https://www.dostips.com/forum/viewtopic.php?f=3&t=8533)
::  2017-01-05 v1.4: Bugfix for trailing backslashes in PATH
::  2016-09-24 v1.3: RunAs check simplified
::  2016-09-20 v1.2: Version added to the output
::  2016-09-19 v1.1: Faulty percent sign in the "dirs" variable corrected
::  2016-09-10 v1.0: Initial release

@echo off
set ex_noreg=Disabled
2>nul set "ex_noreg=Enabled "
set de_noreg=Disabled
if "!!"=="" (set de_noreg=Enabled )

setlocal EnableExtensions DisableDelayedExpansion
cd /d "%~dp0"
set /p "v="<"%~fs0"
>"%temp%\info.txt" echo [code^]%v:~2%

:: prepare some variables to shorten lines in the script
set "International=Control Panel\International"
set "CurrentVersion=SOFTWARE\Microsoft\Windows NT\CurrentVersion"
set "CodePage=SYSTEM\CurrentControlSet\Control\Nls\CodePage"
set "CMDproc=Software\Microsoft\Command Processor"
set /a "HKCU=80000001, HKLM=80000002, HKU=80000003"

:: try to assign variables for used tools in order to make the script run even if the path or pathext variables are corrupted
if exist "%__APPDIR__%chcp.com" (set "chcp=%__APPDIR__%chcp.com") else (set "chcp=echo" &>>"%temp%\info.txt" echo chcp.com not found.)
if exist "%__APPDIR__%find.exe" (set "find=%__APPDIR__%find.exe") else (set "find=echo" &>>"%temp%\info.txt" echo find.exe not found.)
if exist "%__APPDIR__%findstr.exe" (set "findstr=%__APPDIR__%findstr.exe") else (set "findstr=echo" &>>"%temp%\info.txt" echo findstr.exe not found.)
if exist "%__APPDIR__%gpresult.exe" (set "gpresult=%__APPDIR__%gpresult.exe") else (set "gpresult=" &>>"%temp%\info.txt" echo gpresult.exe not found.)
if defined gpresult >nul 2>nul %gpresult% /? || (set "gpresult=" &>>"%temp%\info.txt" echo gpresult.exe not accessible.)
if exist "%__APPDIR__%net.exe" (set "net=%__APPDIR__%net.exe") else (set "net=echo" &>>"%temp%\info.txt" echo net.exe not found.)
if exist "%SystemRoot%\notepad.exe" (set "notepad=%SystemRoot%\notepad.exe") else if exist "%__APPDIR__%notepad.exe" (
  set "notepad=%__APPDIR__%notepad.exe"
) else (set "notepad=%__APPDIR__%cmd.exe /k type" &>>"%temp%\info.txt" echo notepad.exe not found.)
if exist "%__APPDIR__%ping.exe" (set "ping=%__APPDIR__%ping.exe") else (set "ping=(for /l %%i in (0 1 10000) do echo %%i>nul)&echo" &>>"%temp%\info.txt" echo ping.exe not found.)
if exist "%__APPDIR__%reg.exe" (set "reg=%__APPDIR__%reg.exe") else (set "reg=foo?.exe" &>>"%temp%\info.txt" echo reg.exe not found.)
if exist "%__APPDIR__%whoami.exe" (set "whoami=%__APPDIR__%whoami.exe") else (set "whoami=" &>>"%temp%\info.txt" echo whoami.exe not found.)
if defined whoami >nul 2>nul %whoami% /? || (set "whoami=" &>>"%temp%\info.txt" echo whoami.exe not acessible.)
if exist "%__APPDIR__%wbem\WMIC.exe" (set "wmic=%__APPDIR__%wbem\WMIC.exe") else (set "wmic=" &>>"%temp%\info.txt" echo wmic.exe not found.)
if defined wmic >nul 2>nul %wmic% /? || (set "wmic=" &>>"%temp%\info.txt" echo wmic.exe not accessible.)
>>"%temp%\info.txt" echo --------------------------------------------------------------------------------

:: shorten WMI StdRegProv command lines
set "RegHead=set "dat="&for /f "tokens=1* delims==" %%i in ('2^>nul %wmic% /NameSpace:\\root\default Class StdRegProv Call"
set "RegTail=^|%findstr% /rc:"\^<[su]Value = "') do for %%k in (%%j) do set "dat=%%~k""


:: list of directories to check if missing in the path variable
set dirs="%SystemRoot%","%SystemRoot%\System32","%__APPDIR__%wbem","%__APPDIR__%WindowsPowerShell\v1.0"

:: list of tools to check if missing in the path environment
set tools=certutil,choice,clip,debug,forfiles,gpresult,icacls,openfiles,powershell,robocopy,timeout,whoami,wmic

:: check reg access
%reg% query "HKCU\%International%" >nul 2>&1 && (set "RegUserInternational=1") || (set "RegUserInternational=0")
%reg% query "HKU\.DEFAULT\%International%" >nul 2>&1 && (set "RegDefInternational=1") || (set "RegDefInternational=0")
%reg% query "HKLM\%CurrentVersion%" >nul 2>&1 && (set "RegSysCurrentVersion=1") || (set "RegSysCurrentVersion=0")
%reg% query "HKLM\%CodePage%" >nul 2>&1 && (set "RegSysCodePage=1") || (set "RegSysCodePage=0")
%reg% query "HKCU\%CMDproc%" >nul 2>&1 && (set "RegUserCMDproc=1") || (set "RegUserCMDproc=0")
%reg% query "HKLM\%CMDproc%" >nul 2>&1 && (set "RegSysCMDproc=1") || (set "RegSysCMDproc=0")

:: check AutoRun settings
set "AutoRunUser="
>"%temp%\autorun.tmp~" type nul
if %RegUserCMDproc%==1 (
  2>nul %reg% query "HKCU\%CMDproc%" /v AutoRun |>"%temp%\autorun.tmp~" %find% "REG_"
) else if defined wmic (
  2>nul %wmic% /NameSpace:\\root\default Class StdRegProv Call GetExpandedStringValue hDefKey="&H%HKCU%" sSubKeyName="%CMDproc%" sValueName="AutoRun" |>"%temp%\autorun.tmp~" %findstr% /rc:"\<[su]Value = "
)
<"%temp%\autorun.tmp~" set /p "AutoRunUser="
set "AutoRunSys="
>"%temp%\autorun.tmp~" type nul
if %RegSysCMDproc%==1 (
  2>nul %reg% query "HKLM\%CMDproc%" /v AutoRun |>"%temp%\autorun.tmp~" %find% "REG_"
) else if defined wmic (
  2>nul %wmic% /NameSpace:\\root\default Class StdRegProv Call GetExpandedStringValue hDefKey="&H%HKLM%" sSubKeyName="%CMDproc%" sValueName="AutoRun" |>"%temp%\autorun.tmp~" %findstr% /rc:"\<[su]Value = "
)
<"%temp%\autorun.tmp~" set /p "AutoRunSys="
del "%temp%\autorun.tmp~"

:: check elevation and Admin membership
set "RunAs="
%net% session >nul 2>&1 && (set "RunAs=Yes") || (set "RunAs=No")
set "LocalAdmin="
if defined whoami 2>nul %whoami% /groups|>nul %findstr% /i "\<S-1-5-32-544\>" && set "LocalAdmin=Yes" || set "LocalAdmin=No"
if not defined LocalAdmin if defined wmic (
  for /f "tokens=1* delims==" %%i in (
    '%wmic% path Win32_Group WHERE "LocalAccount='TRUE' AND SID='S-1-5-32-544'" GET Name /value'
  ) do for /f "delims=" %%k in ("%%j") do (
    for /f "tokens=1* delims=:" %%l in ('2^>nul %gpresult% /r /scope user ^| %findstr% /n /c:"--------" /c:"%%k"') do (
      set "check="
      for /f "delims=- " %%n in ("%%m") do set "check=1"
      if not defined check (
        set "n=%%l"
        set "LocalAdmin=No"
      ) else for /f %%n in ('set /a n') do if %%n lss %%l set "LocalAdmin=Yes"
    )
  )
) else set "LocalAdmin=Not found"

:: International
for %%a in ("iDate" "LocaleName") do set "%%~a="
if %RegUserInternational%==1 (call :RegInternational) else if defined wmic call :WmiInternational
if not defined iDate set "iDate=0"
if not defined LocaleName call :MUI
if "%iDate%"=="0" (set "format=mm/dd/yy") else if "%iDate%"=="1" (set "format=dd/mm/yy") else if "%iDate%"=="2" (set "format=yy/mm/dd")

:: ProductName
set "ProductName="
if %RegSysCurrentVersion%==1 (call :RegProductName) else if defined wmic call :WmiProductName
if not defined ProductName for /f "tokens=1* delims==" %%i in ('2^>nul %wmic% os get Caption /value') do set "ProductName=%%j"

:: Extensions and DelayedExpansion
for %%a in (es eu ds du) do set "%%a=Disabled"
if %RegUserCMDproc%==1 (call :RegUserProc) else if defined wmic call :WmiUserProc
if %RegSysCMDproc%==1 (call :RegSysProc) else if defined wmic call :WmiSysProc
if not defined eu set "eu=%ex_noreg%"
if not defined du set "du=%de_noreg%"

:: Code Pages
set "OEMCP=" &set "ACP="
if %RegSysCodePage%==1 (call :RegCodePage) else if defined wmic call :WmiCodePage
if not defined OEMCP for /f "tokens=*" %%i in ('%chcp%') do for %%j in (%%j) do set "OEMCP=%%~nj"
if not defined ACP (
  if defined wmic (
    for /f "tokens=2 delims==" %%i in ('%wmic% os get CodeSet /value') do set /a "ACP=%%i"
  )
)

:: checks path
:: removes random double quotes, adds double quotes, removes trailing slash
:: http://stackoverflow.com/questions/5471556/pretty-print-windows-path-variable-how-to-split-on-in-cmd-shell/5472168#5472168
set "p=%path:"=""%"
set "p=%p:^=^^%"
set "p=%p:&=^&%"
set "p=%p:|=^|%"
set "p=%p:<=^<%"
set "p=%p:>=^>%"
set "p=%p:;=^;^;%"
set p=%p:""="%
set "p=%p:"=""%"
set "p=%p:;;="";""%"
set "p=%p:^;^;=;%"
set "p=%p:""="%"
set "p=%p:"=""%"
set "p=%p:"";""=";"%"
set "p=%p:"""="%"
set p="%p%"
set p=%p:\"="%

:: Check for 64 bit windows.
set "bit="
if defined wmic (
  for /f "tokens=1* delims==" %%i in ('2^>nul %wmic% os GET OSArchitecture /value') do for /f "delims=" %%k in ("%%j") do set "bit=%%k"
)
if defined bit (echo "%bit%"|%find% "64" >nul && set "bit=64" || set "bit=32") else (
  echo "%PROCESSOR_ARCHITECTURE%"|%find% "86" >nul && set "bit=32" || (
    if exist "%SystemRoot%\SysWOW64\" (set "bit=64") else set "bit=32"
  )
)

:: RAM space
set "ram="
if defined wmic (
  for /f "tokens=1* delims==" %%i in ('2^>nul %wmic% os GET TotalVisibleMemorySize /value') do for /f "delims=" %%k in ("%%j") do set "ram=%%k"
)

:: get Windows version and the DIR command format to display
if "%notepad:~-3%"=="exe" (
  >nul %chcp% %ACP%
  for /f "delims=" %%a in ('^>nul %chcp% %ACP% ^&ver') do set "WinVer=%%a"
  if exist "%SystemDrive%\pagefile.sys" (
    for /f "delims=" %%a in ('^>nul %chcp% %ACP% ^&dir "%SystemDrive%\pagefile.sys" /a ^|%find% ":"') do set "DirFormat=%%a"
  ) else for /f "delims=" %%a in ('^>nul %chcp% %ACP% ^&dir "%__APPDIR__%ntoskrnl.exe" /a ^|%find% ":"') do set "DirFormat=%%a"
  >nul %chcp% %OEMCP%
) else (
  for /f "delims=" %%a in ('ver') do set "WinVer=%%a"
  if exist "%SystemDrive%\pagefile.sys" (
    for /f "delims=" %%a in ('dir "%SystemDrive%\pagefile.sys" /a ^|%find% ":"') do set "DirFormat=%%a"
  ) else for /f "delims=" %%a in ('dir "%__APPDIR__%ntoskrnl.exe" /a ^|%find% ":"') do set "DirFormat=%%a"
)

set "pad=                          "
:: create the information file and send the information to the clipboard if clip is available
>>"%temp%\info.txt" (
  setlocal EnableDelayedExpansion
  echo Windows version        :  !WinVer!
  echo Product name           :  !ProductName!, !bit! bit
  echo Performance indicators :  Processor Cores: !NUMBER_OF_PROCESSORS!      Visible RAM: !ram! kilobytes&echo(

  echo Date/Time format       :  (!format!^)  !date!  !time!
  echo __APPDIR__             :  !__APPDIR__!
  echo ComSpec                :  !comspec!
  echo PathExt                :  !PathExt!
  echo Extensions             :  system: !es!  user: !eu!
  echo Delayed expansion      :  system: !ds!  user: !du!
  echo Locale name            :  !LocaleName!       Code Pages: OEM  !OEMCP!    ANSI !ACP!
  echo DIR  format            :  !DirFormat!
  echo Permissions            :  Elevated Admin=!RunAs!, Admin group=!LocalAdmin!
  if defined AutoRunSys echo System  Autorun found  :  !AutoRunSys!
  if defined AutoRunUser echo User    AutoRun found  :  !AutoRunUser!
  endlocal&echo(

  rem report if path elements are missing
  for %%i in (%dirs%) do (
    set "found="
    setlocal EnableDelayedExpansion
    for %%j in (!p!) do (
      endlocal
      if /i "%%~i"==%%j set "found=1"
      setlocal EnableDelayedExpansion
    )
    endlocal
    if not defined found echo(%pad%Missing from the PATH environment: %%~i
  )

  rem report if tools are missing
  for %%i in (%tools%) do for /f "tokens=1,2 delims=?" %%j in ("%%~i.exe?%%~i.com") do if "%%~$PATH:j%%~$PATH:k"=="" (
    echo(%pad%Missing from the tool collection:  %%i
  )
  echo [/code^]
)

:: load the information into Notepad where it can also be copied to the clipboard
start "" %notepad% "%temp%\info.txt" & %ping% -n 2 127.0.0.1 >nul & del "%temp%\info.txt"

goto :eof

:: ~~~~~~~~~ Sub Routines ~~~~~~~~~
:RegInternational
for /f "tokens=1,2*" %%a in ('%reg% query "HKCU\%International%" /v "iDate" 2^>nul ^| %find% "REG_"') do set "%%~a=%%~c"
if %RegDefInternational%==1 for /f "tokens=1,2*" %%a in ('%reg% query "HKU\.DEFAULT\%International%" /v "iDate" 2^>nul ^| %find% "REG_"') do if not defined %%~a set "%%~a=%%~c"
for /f "tokens=1,2*" %%a in ('%reg% query "HKCU\%International%" /v "LocaleName" 2^>nul ^| %find% "REG_"') do set "%%~a=%%~c"
goto :eof

:WmiInternational
%RegHead% GetStringValue hDefKey^="&H%HKCU%" sSubKeyName^="%international%" sValueName^="iDate" %RegTail%
if not defined dat %RegHead% GetStringValue hDefKey^="&H%HKU%" sSubKeyName^=".DEFAULT\%international%" sValueName^="iDate" %RegTail%
if defined dat (set "iDate=%dat%") else set "iDate=0"
%RegHead% GetStringValue hDefKey^="&H%HKCU%" sSubKeyName^="%international%" sValueName^="LocaleName" %RegTail%
if defined dat (set "LocaleName=%dat%") else set "LocaleName="
goto :eof

:MUI
for /f "tokens=2 delims=={}" %%i in ('2^>nul %wmic% os get MUILanguages /value') do set "LocaleName=%%i"
if defined LocaleName (
  set "LocaleName=%LocaleName:"=%"
) else (
  setlocal EnableDelayedExpansion
  for /f %%i in ('dir /ad /b "%__APPDIR__%??-??"^|%findstr% /x "[a-z][a-z]-[a-z][a-z]"') do (
    if exist "%__APPDIR__%%%i\ulib.dll.mui" set "LocaleName=!LocaleName!,%%i"
  )
  if defined LocaleName (for /f %%j in ("!LocaleName:~1!") do (endlocal &set "LocaleName=%%j")) else endlocal
)
goto :eof

:RegProductName
for /f "tokens=2*" %%a in ('%reg% query "HKLM\%CurrentVersion%"^|%find% /i "ProductName"') do set "ProductName=%%b"
goto :eof

:WmiProductName
%RegHead% GetStringValue hDefKey^="&H%HKLM%" sSubKeyName^="%CurrentVersion%" sValueName^="ProductName" %RegTail%
if defined dat (set "ProductName=%dat%") else set "ProductName="
goto :eof

:RegUserProc
%reg% query "HKCU\%CMDproc%" /v "EnableExtensions" 2>nul|%find% "0x1">nul && set "eu=Enabled "
%reg% query "HKCU\%CMDproc%" /v "DelayedExpansion" 2>nul|%find% "0x1">nul && set "du=Enabled "
goto :eof

:WmiUserProc
%RegHead% GetDWORDValue hDefKey^="&H%HKCU%" sSubKeyName^="%CMDproc%" sValueName^="EnableExtensions" %RegTail%
if "%dat%"=="1" set "eu=Enabled "
%RegHead% GetDWORDValue hDefKey^="&H%HKCU%" sSubKeyName^="%CMDproc%" sValueName^="DelayedExpansion" %RegTail%
if "%dat%"=="1" set "du=Enabled "
goto :eof

:RegSysProc
%reg% query "HKLM\%CMDproc%" /v "EnableExtensions" 2>nul|%find% "0x1">nul && set "es=Enabled "
%reg% query "HKLM\%CMDproc%" /v "DelayedExpansion" 2>nul|%find% "0x1">nul && set "ds=Enabled "
goto :eof

:WmiSysProc
%RegHead% GetDWORDValue hDefKey^="&H%HKLM%" sSubKeyName^="%CMDproc%" sValueName^="EnableExtensions" %RegTail%
if "%dat%"=="1" set "es=Enabled "
%RegHead% GetDWORDValue hDefKey^="&H%HKLM%" sSubKeyName^="%CMDproc%" sValueName^="DelayedExpansion" %RegTail%
if "%dat%"=="1" set "ds=Enabled "
goto :eof

:RegCodePage
for /f "tokens=3" %%a in ('%reg% query "HKLM\%CodePage%" /v "OEMCP"') do set /a "OEMCP=%%a"
for /f "tokens=3" %%a in ('%reg% query "HKLM\%CodePage%" /v "ACP"') do set /a "ACP=%%a"
goto :eof

:WmiCodePage
%RegHead% GetStringValue hDefKey^="&H%HKLM%" sSubKeyName^="%CodePage%" sValueName^="OEMCP" %RegTail%
if defined dat (set "OEMCP=%dat%") else set "OEMCP="
%RegHead% GetStringValue hDefKey^="&H%HKLM%" sSubKeyName^="%CodePage%" sValueName^="ACP" %RegTail%
if defined dat (set "ACP=%dat%") else set "ACP="
goto :eof

aGerman
Expert
Posts: 3518
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Batch script collection (outsourced)

#2 Post by aGerman » 19 Sep 2016 11:16

If batch files or commands don't behave as expected then standard external commands could have been overriden by non-standard files with the same base name within the PATH environment. The following script tests all EXE and COM files within %__APPDIR__% (usually C:\Windows\system32).

Code: Select all

:: TESTOVERRIDE.BAT version 1.0
:: source http://www.dostips.com
::  Thanks to dbenham who provided this script.
:: Bugs and suggestions can be reported in thread http://www.dostips.com/forum/viewtopic.php?f=3&t=7347
::
:: Release History:
::  2016-09-19 v1.0: Initial release

@echo off
>"%temp%\overridden.txt" type nul
if "%~1" equ "__:TestOverride__" (
  if not defined pathext set "pathExt=.com;.exe;.bat;.cmd"
  goto :TestOverride
)
call :TestOverride
exit /b
 
 
:TestOverride
setlocal disableDelayedExpansion
if not defined pathext (
  "%__APPDIR__%cmd.exe" /c "%~f0" __:TestOverride__
  exit /b
)
 
:: Define LF to contain a new line (0x0A) character
(set LF=^
%= Do not remove or alter this line =%
)
:: Prepare the list of paths in PATH
set "list=.\;%path:"=""%"
set "list=%list:^=^^%"
set "list=%list:&=^&%"
set "list=%list:|=^|%"
set "list=%list:<=^<%"
set "list=%list:>=^>%"
set "list=%list:;=^;^;%"
set list=%list:""="%
set "list=%list:"=""Q%"
set "list=%list:;;="S"S%"
set "list=%list:^;^;=;%"
set "list=%list:""="%"
setlocal EnableDelayedExpansion
set "list=!list:"Q=!"
for %%N in ("!LF!") do set "list="!list:"S"S=.\"%%~N"!""
setlocal disableDelayedExpansion
for /f %%C in ('copy /z "%~dpf0" nul') do (
  for %%F in (
    "%__APPDIR__%*.exe"
    "%__APPDIR__%*.com"
  ) do (
    <nul set /p "=.%%C                                                                              %%C%%F"
    set "skip="
    setlocal enableDelayedExpansion
    for /f "delims=" %%P in ("!list!") do if not defined skip (
      if "!!" equ "" endlocal
      for %%X in (%pathext%) do if not defined skip (
        if exist "%%~fP%%~nF%%X" (
          if /i "%%~fF" neq "%%~dpP%%~nF%%X" >>"%temp%\overridden.txt" echo "%%F" is overridden by "%%~dpP%%~nF%%X"
          set "skip=1"
        )
      )
      if /i "%%~dpF" equ "%%~dpP" set "skip=1"
    )
  )
  (echo(%%C                                                                              )
)
for %%i in ("%temp%\overridden.txt") do if %%~zi gtr 0 (
  start "" %__APPDIR__%notepad.exe "%temp%\overridden.txt" & %__APPDIR__%ping.exe -n 2 127.0.0.1 >nul & del "%temp%\overridden.txt"
) else (
  del "%temp%\overridden.txt" & echo No overridden tools found.& pause
)
exit /b

Locked