Some useful Windows Batch scripts

Assigning output of a program to a variable

@echo off
setlocal enabledelayedexpansion

REM: Here we run the program "hostname"
hostname > HOSTNAME.tmp
set /p HOSTNAME=< HOSTNAME.tmp
echo %HOSTNAME%

Getting machine time

@echo off
setlocal enabledelayedexpansion

set hour=%time:~0,2%
if "%hour:~0,1%" == " " set hour=0%hour:~1,1%
echo hour=%hour%
set min=%time:~3,2%
if "%min:~0,1%" == " " set min=0%min:~1,1%
echo min=%min%
set secs=%time:~6,2%
if "%secs:~0,1%" == " " set secs=0%secs:~1,1%
echo secs=%secs%

Getting machine date

@echo off
setlocal enabledelayedexpansion

for /f "tokens=1-4 delims=/ " %%i in ("%date%") do (
set dow=%%i
set month=%%j
set day=%%k
set year=%%l
)
set datestr=%month%_%day%_%year%
echo datestr is %datestr%

Converting UPPER CASE letters to lower case by perl

@echo off
setlocal enabledelayedexpansion

set LOWER_CASE="I AM AN UPPER CASE"
@FOR /F %%s IN ('perl -e "print lc(pop)" %LOWER_CASE%') DO @set LC_LOWER_CASE=%%s
echo %LOWER_CASE%

Loading