joeware - never stop exploring... :)

Information about joeware mixed with wild and crazy opinions...

Some possibly useful batch commands

by @ 10:31 am on 7/8/2010. Filed under tech

I had to work out a few things to do in a batch file the last couple of days so I thought I would share. Note: so this doesn’t become a huge powershell versus perl versus whatever discussion, I had no choice, I had batch available period and even that was possibly stretching it a little as the people involved seemed very concerned about scripts, etc. Also, yes I am aware of Tim–Toadie (TIMTOWTDI). So please feel free to comment with additional methods.

 

Getting date and time in a useful format

for /f "tokens=1-4 Delims=/ " %%i in (‘date /t’) do  set dt=%%l/%%j/%%k
for /f "tokens=1" %%i in (‘time /t’) do set tm=%%i
echo Date: %dt%-%tm%

 

Is this machine virtual or physical? It could be VMWare, HyperV or Virtual Server or Virtual PC. I don’t have the strings for other virtualization techs, if people are using them and can post the results of the command “wmic computersystem get model” then they can be added to the batch commands.

set VIRTUAL=NO
set VMWARE=NO
set HYPERV=NO
for /f "skip=2 tokens=1-2 delims=," %%i in (‘wmic computersystem get model /format:csv’) do set MODEL=%%j

: Microsoft Virtualization
if "%MODEL%"=="Virtual Machine" (
  set VIRTUAL=YES
  set HYPERV=YES
)

: VMWare Virtualization
if "%MODEL%"=="VMware Virtual Platform" (
  set VIRTUAL=YES
  set VMWARE=YES
)

 

Is this machine Vista or better?

SET VISTA+=NO
if exist %systemroot%\system32\bcdedit.exe set VISTA+=YES

 

Is this machine x64?

SET x64=NO
if exist "%systemdrive%\program files (x86)" set x64=YES

Rating 4.00 out of 5

5 Responses to “Some possibly useful batch commands”

  1. Scotte says:

    Not quite as clean as your Vista test, but this doesn’t rely on the correct existence of bcdedit.exe

    set VISTA+=NO
    for /f “tokens=1-2 delims=[” %%i in (‘ver’) do set VERSION=%%j
    for /f “tokens=1-2 delims=.] ” %%i in (‘set VERSION’) do set VERSION=%%j
    if %VERSION% gtr 5 set VISTA+=YES

  2. Andrew from Vancouver says:

    How about a batch file that applies the group policies on the local machine? This is great for running interactively or for pushing with the SysInternals psexec tool.

    @echo off
    REM This file is ForceGPUpdateAnyOS.cmd
    if exist %windir%\system32\gpupdate.exe goto OS-is-new

    :OS-is-old
    REM NT4 and Windows 2000
    echo Updating the Computer Group Policies ———
    secedit /refreshpolicy machine_policy /enforce> nul
    echo.
    echo Updating the User Group Policies ———
    secedit /refreshpolicy user_policy /enforce> nul
    echo.

    goto quit

    :OS-is-new
    REM WXP, W2K3, Vista, W2K8…
    echo Updating the Computer Group Policies ———
    echo n|GPUpdate /Target:Computer /Force> nul
    echo.
    echo Updating the User Group Policies ———
    echo n|GPUpdate /Target:User /Force> nul
    echo.

    goto quit

    :QUIT
    echo Done. Some policies may require a logoff or a reboot to take effect.
    echo.

  3. Adam says:

    I, too, just had occasion to whip up a few batch files, and came across these:

    %DATE% and %TIME% are already set and available to you. %TIME% is actually better than `time /t` if you need seconds.

    If you want to get substrings from a variable, use %VARIABLE:~start,length%. So to get the hour you would use, SET HR=%TIME:~0,2%. If you want to do string substitution, use %VARIABLE:orig=repl%. So to replace / with _, you would use something like SETDT=%DATE:/=_%. Doesn’t give you a string-sortable date, but useful in other scenarios.

    And if you need to escape a character (like a pipe), use ^, as in ^|. Useful if you want to be piping output inside a “FOR /f %a in (‘command1 ^| command2’)” scenario.

  4. Version tests based on the presence of a directory are inherently risky, as “Program Files (x86)” is called something else in other languages. The Wikipedia article on the “Program Files” folder has more info. http://en.wikipedia.org/wiki/Program_Files. According to http://en.wikipedia.org/wiki/Environment_variable The %ProgramFiles% environment variable names change on 64-bit systems. “In 64-bit editions of Windows (XP, 2003, Vista), there are also %ProgramFiles(x86)% which defaults to C:\Program Files (x86) and %ProgramW6432% which defaults to C:\Program Files. The %ProgramFiles% itself depends on whether the process requesting the environment variable is itself 32-bit or 64-bit (this is caused by Windows-on-Windows 64-bit redirection).”

    This page: “HOWTO: Detect Process Bitness – David Wang – Site Home – MSDN Blogs” at
    http://blogs.msdn.com/b/david.wang/archive/2006/03/26/howto-detect-process-bitness.aspx has some very useful info on detecting whether or not you’re running 32-bit or 64-bit. Looks like Windows XP and later have two existing environment variables which can be tested.

    IF .%PROCESSOR_ARCHITECTURE%==.amd64 goto 64bit
    IF .%PROCESSOR_ARCHITEW6432%==.amd64 goto 64bit

    I have a number of useful batch-file tricks for using date env.vars that I have picked up over the years. I have a long batch-file called GetDat2k that creates the following environment variables and runs in Windows 2000 and XP. Haven’t tested it under Windows 7 yet.

    =================================================================
    date=Sun 07/18/2010
    mdy=07/18/2010
    cymd=20100718
    ymd=100718
    y-md=2010-07-18
    day=18 month=07 year=2010 yy=10
    MMM=Jul week=3 dow=Sun
    time=20:32:04.13 hour=20 min=32 sec=04 sech=04.13
    =================================================================

    Get it here:

    GeoApps Downloadable Files: http://www.geoapps.com/files.shtml
    “Loads today’s date and time into a series of environment variables for use in other batch file. Run GETDAT2K SHOW to see env.vars displayed/”

  5. Djimi says:

    Cool stuff. I’ve used a lot of batch stuff in the last year to come up with the same type of information, with what’s available, as I want it to be fast. WMIC is really quite good and you can get almost anything with it, of course the first time it runs it takes awhile to return something.

    For the the architecture you can use a variant of both of Joe’s and Angus’:
    +++++++++
    set arch=32bit
    set | find /i “amd64” >nul && if errorlevel 0 set arch=64bit
    +++++++++
    or whatever value you need

    For the “model” you can look up the vendor information (I believe that’s what you’re after):
    +++++++++
    wmic csproduct list full | find /i “Vendor=”
    +++++++++

    I was trying to wmic and a for loop to do the same thing as Scotte did, but wmic outputs a final EOL null, so I gave up on that 😉

[joeware – never stop exploring… :) is proudly powered by WordPress.]