OoooooldSchool_DOS_BatchScripts

String manipulations

SubStringinginginging

@echo last 4 chars of %test% is %test:~-4%

Remove bin from envVar hi

C:\Users\MDockery>set hi=c:\bla\bin
C:\Users\MDockery>echo %hi%
c:\bla\bin
C:\Users\MDockery>echo %hi:bin=%
c:\bla\
C:\Users\MDockery>echo %hi:BIN=%
c:\bla\

Get file size

Rem **************************
Rem ****  Get File Size ************
Rem ****  filesizzzzzzer.bat *********
Rem **************************
@echo off
call :filesize "somefile.csv"
echo file size is %size%
goto :eof


:filesize
  set size=%~z1
 exit /b 0

Rem **************************

Wrap long commands with line continuation

rem you cannot have any spaces after the caret/line_continuation/wrap characterecho This script looks ^ for a single parm to be passed. ^ If not passed, it will prompt the user ^ If not entered, it will assume localhost


FOR LOOPS


@echo ---- simple loops ---
for  %%x IN (1 2 3) do echo %%x
for  %%x IN (a b c) do echo %%x

ex2:
SETLOCAL ENABLEDELAYEDEXPANSION
set simplecommalist=1,2,3
FOR %%a IN (%simplecommalist%) DO (
  set listitem=%%a
  echo listitem: !listitem!
)

REM copy files in the current dir to the “destfile.txt” ...overwriting it each time 
for /r %%i in (*) do (
copy "%%i" destfile.txt
)

GUI POPUP box with Message

msg /V /W /TIME:2 * gui popup box for 2 seconds to all users on this computer

Dates

set TodayVar=%date:~10,4%%date:~4,2%%date:~7,2%
echo %TodayVar%
or

rem Parse time (depending on your region settings)
set cur_hh=%time:~0,2%
if %cur_hh% lss 10 (set cur_hh=0%time:~1,1%)
set cur_nn=%time:~3,2%
set cur_ss=%time:~6,2%
set log=runLog_%cur_yyyy%%cur_mm%%cur_dd%_%cur_hh%%cur_nn%%cur_ss%.txt
echo date_time: %log%

Choices

rem *********** choices **********************************
rem D=default is Y below
rem T=time limit before default is auto-selected
rem ****************************************************
echo CHOICE /T 5 /C YNC /D Y /M "Press Y for Yes, N for No or C for Cancel."
echo   ERRORLEVEL=1... set to choice key index selected. (starting at 1)
echo   ERRORLEVEL=0 if CTRL+C pressed
echo   ERRORLEVEL=255 if real error condition
echo   T is timeout before D default is used
CHOICE /T 5 /C YNC /D Y /M "Press Y for Yes, N for No or C for Cancel."
echo err level is %ERRORLEVEL%

Functions

Call a reUsable function (passing 3 parms)
call:GET_PATH_FOR_CMD ORACLE_HOME_BIN,"C:\app",sqlplus.exe

Here is the function which can be called/reUsed inside a bat script
::---------------------------------------------------------------------
::-- Function: get path for specified command under specified folder
::---------------------------------------------------------------------
:GET_PATH_FOR_CMD
@echo off
rem echo parm 1 is %~1 parm 2 is %~2 parm 3 is %~3
set var=%~1
set searchDir=%~2
set cmdToFind=%~3
set tmpFile="%temp%\%var:~0,3%.txt"
echo > %tmpFile%
echo %var% via dir %searchDir% for cmd %cmdToFind%
rem run dir to search for command if it exists
if exist "%searchDir%"  dir "%searchDir%\%cmdToFind%" /s | find /I "DIRE" >%tmpFile%
set /P %var%=<%tmpFile%
set /P tempVal=<%tmpFile%
rem echo %var% value from temp file is %tempVal%
if not defined %var% goto:eof
rem remove Directory prefix
@echo on
set %var%=%tempVal:~14%
@echo off
goto:eof
:SKIP_FUNC

Another easy function:

@echo off
set srcdir=h:\Visual Studio 2013\Projects\SharedLib\SharedLib\bin\Debug
set irddir=h:\Visual Studio 2013\Projects\Traf\Traf\bin\Debug
set dlldir=C:\app\DLLs

call:copyFilesFunction "%srcdir%","%irddir%"
call:copyFilesFunction "%srcdir%","%dlldir%"
goto :theEnd

:copyFilesFunction
set srcDir=%~1
set dstDir=%~2
echo **********************************
echo Copying the dll and xml comments.
echo srcDir is %srcDir%
echo dstDir is %dstDir%
echo **********************************
copy "%srcdir%\SharedLib.dll" "%dstdir%"
copy "%srcdir%\SharedLib.xml" "%dstdir%"
goto:eof

:theEnd

Comments

rem ************* comments ********************************
REM alternate comments below via "invalid labels")
:: (a comment/invalid label) like a REMark (ignored by the command processor) w/a few differences:
:: --> ECHO ON shows REM's (but NOT :: lines)
:: --> :: allow "line end" caret (^ at the end of a line makes the next line a comment too)
:: --> can cause problems in parenthesis blocks ( ) and has other pitfalls
::   Note: many other special chars can/could be used instead of ::
:~ This is a comment
:` This is a comment
:! This is a comment
:@ This is a comment
:# This is a comment
:$ This is a comment
:% This is a comment
:^ This is a comment
:& This is a comment
:* This is a comment
:( This is a comment
:) This is a comment
:_ This is a comment
:- This is a comment
:+ This is a comment
:= This is a comment
:{ This is a comment
:} This is a comment
:[ This is a comment
:] This is a comment
:| This is a comment
:\ This is a comment
:: This is a comment
:; This is a comment
:" This is a comment
:' This is a comment
:< This is a comment
:> This is a comment
:, This is a comment
:. This is a comment
:? This is a comment
:/ This is a comment

Comments

Popular Posts