Batch Files

Run a Windows Batch File from the Current Directory

From http://skyboygames.com/quick-tip-run-a-windows-batch-file-from-the-current-directory/

When you want your batch file commands to be executed from the current directory, putting the following at the start of your batch file should do the trick:

cd /d %~dp0

The “cd” meaning “change directory” is easy enough to understand. The “/d” tells cd to change drive and directory at the same time.

Now, that cryptic “%~dp0” is where the real work is done. %0 refers to the zeroth parameter of the batch file: the batch file itself. Adding the “~dp” modifier draws out the drive and path of the batch file sans its filename, hence the current directory.

For more information and other ways to manipulate the folder details see:

https://stackoverflow.com/questions/5034076/what-does-dp0-mean-and-how-does-it-work


Here's some extra commands that help at the start of a batch file:

setlocal

set RC=0

set START_DIR=%~dp0

set SCRIPT_NAME=%~n0


rem Convert the current working directory

rem to a format that is valid for use in a

rem a file URL

set CUR_PATH=%cd%

set DRIVE=%cd:~0,1%

set SHORT_PATH=%~sp0%

set SHORT_PATH=%SHORT_PATH:\=/%

set PATH_URL=%DRIVE%%%3a%SHORT_PATH%

ReTurning a unique number based on date and Time

From https://www.dostips.com/DtTipsDateTime.php

To use this, copy the subroutine to the end of the batch file, then call it using the command:

call :unique UNIQUE

then use the parameter %UNIQUE% to recall this number.

:Unique ret -- returns a unique string based on a date-time-stamp, YYYYMMDDhhmmsscc

::          -- ret    [out,opt] - unique string

:$created 20060101 :$changed 20080219 :$categories StringOperation,DateAndTime

:$source https://www.dostips.com

SETLOCAL

for /f "skip=1 tokens=2-4 delims=(-)" %%a in ('"echo.|date"') do (

    for /f "tokens=1-3 delims=/.- " %%A in ("%date:* =%") do (

        set %%a=%%A&set %%b=%%B&set %%c=%%C))

set /a "yy=10000%yy% %%10000,mm=100%mm% %% 100,dd=100%dd% %% 100"

for /f "tokens=1-4 delims=:. " %%A in ("%time: =0%") do @set UNIQUE=%yy%%mm%%dd%%%A%%B%%C%%D

ENDLOCAL & IF "%~1" NEQ "" (SET %~1=%UNIQUE%) ELSE echo.%UNIQUE%

EXIT /b 


Running a command on multiple files

DOS features two commands that allow you to run a command on multiple files:

For

This command does not spawn a new process for each file and so it tends to be quicker.

https://ss64.com/nt/for2.html

Conditionally perform a command on several files.

Forfiles

This command can create a new process for each file - which may be necessary since it is generally buggy.

For documentation see:

https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/forfiles 

https://ss64.com/nt/forfiles.html 

Selects and runs a command on a file or set of files. This command is most commonly used in batch files. 

Checking for Admin rights before running script

Some scripts need to be run with admin rights. The following script checks to make sure. Place this at the start of the script. This script will politely ask the user to re-run the script as an admin and provide instructions.

:check_Permissions

  echo Administrative permissions required. Detecting permissions...

  net session >nul 2>&1

  if %errorLevel% == 0 (

    echo Success: Administrative rights OK!

  ) else (

    echo Failure: Current permissions inadequate.

    echo.

    echo To run this batch file right-click and select

    echo "Run as administrator".

    echo.

    pause

    exit /b 1

  )

Another method is the following (from https://stackoverflow.com/questions/7044985/how-can-i-auto-elevate-my-batch-file-so-that-it-requests-from-uac-administrator). This will pop up a dialog box to elevate privileges.

@echo off

 

:: BatchGotAdmin

:-------------------------------------

REM  --> Check for permissions

IF "%PROCESSOR_ARCHITECTURE%" EQU "amd64" (

>nul 2>&1 "%SYSTEMROOT%\SysWOW64\cacls.exe" "%SYSTEMROOT%\SysWOW64\config\system"

) ELSE (

>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"

)

 

REM --> If error flag set, we do not have admin.

if '%errorlevel%' NEQ '0' (

echo Requesting administrative privileges...

goto UACPrompt

) else ( goto gotAdmin )

 

:UACPrompt

echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"

set params= %*

echo UAC.ShellExecute "cmd.exe", "/c ""%~s0"" %params:"=""%", "", "runas", 1 >> "%temp%\getadmin.vbs"

 

"%temp%\getadmin.vbs"

del "%temp%\getadmin.vbs"

exit /B

 

:gotAdmin

pushd "%CD%"

CD /D "%~dp0"

:--------------------------------------

<YOUR BATCH SCRIPT HERE>


Running multiple commands simultaneously

@start /b cmd /c net stop servicename1 

@start /b cmd /c net stop servicename2 

@start /b cmd /c net stop servicename3 

@start /b cmd /c net stop servicename4