Skip to content

Instantly share code, notes, and snippets.

@davidruhmann
davidruhmann / Admin.bat
Last active May 7, 2023 06:37
Check if user has Administrator privileges and request them if needed.
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:Admin <Return> [Needed] [Success]
:: Check for Administrator privileges and request privileges if Needed 'true'.
:::: Usage: call :Admin xReturn true
:: Return success value, if user is Admin. Default `true` if Success not set.
setlocal
set "xVBUAC=%Temp%\AdminUAC.vbs"
set "xSuccess=true"
set "xAdmin=false"
if not "%~3"=="" set "xSuccess=%~3"
setlocal
set "myString=abcdef!%%^^()^!"
call :strlen result myString
echo %result%
goto :eof
:strlen <resultVar> <stringVar>
(
setlocal EnableDelayedExpansion
set "s=!%~2!#"
@davidruhmann
davidruhmann / SingleChar.bat
Created December 20, 2012 19:05
[Batch] Output Single Char
:: Output a single character.
:: Does not work with whitespace on versions of Windows
:: after XP which trim leading whitespace.
>File.txt ( <nul set /p "= " )
:: Output 0x0C New Page
cls>File.txt
@davidruhmann
davidruhmann / CopyAllDrivesToExternal.bat
Created December 26, 2012 20:11
[Batch] Copy contents for all of a systems drives onto a specified external HDD.
@echo off
setlocal EnableDelayedExpansion
rem Loop through all of the drives to find the external HDD
for %%D in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do (
for /f "tokens=5" %%S in ('vol %%~D:') do set "xSerial=%%S"
if "%xSerial%"=="ABCD-1234" (
rem TODO Specify the exact target copy base location that you want.
set "xTarget=%%~D:\%ComputerName%"
)
@davidruhmann
davidruhmann / RoboCopyDelete.bat
Last active December 10, 2015 07:38
[Batch] Delete files younger than 5 days old with RoboCopy
:: Create an empty temp directory and purge all files and folders in the current directory that are younger than 5 days old.
:: See RoboCopy /? for all of the available options like /E for recursive. Remove /L to actually perform the deletions.
RD /S /Q "%Temp%\Temp" 2>nul & MKDIR "%Temp%\Temp" && ROBOCOPY "%Temp%\Temp" "C:\Backup" /PURGE /MT /MAXAGE:5 /NS /NC /NJH /NJS /L
@davidruhmann
davidruhmann / ReadBlank.bat
Created December 31, 2012 16:07
[Batch] Read Blank Lines and Separate Output
:: Hide Commands
@echo off
:: Erase Existing Files
>match.txt ( <nul set /p "=" )
>nomatch.txt ( <nul set /p "=" )
:: Loop through Source and Generate Output
for /f "tokens=1,* delims=]" %%K in ('type temp.txt ^| find /V /N ""') do (
for /f "delims=" %%X in ('echo(%%L ^| find /V "green"') do (
@davidruhmann
davidruhmann / TabDelimRemoveLast.bat
Last active December 10, 2015 10:39
[Batch] Loop through tab delimited list and remove the last item from the list.
@echo off
set "Var=2210 2215 2211 22106 22109 221"
:: Set Unique EOL Identifier, Loop through list, remove last item, and save.
:: The EOL Identifier should be a sequence or character that is not allowed in the variable normally.
:: This must be determined at the time of usage by the programmer.
set "Var=%Var%@"
for /f "delims=" %%A in ('echo(%Var: =^&echo(%') do call set "Var2=%%Var: %%A=%%"
set "Var=%Var2%"
@davidruhmann
davidruhmann / ParsePipeSeparatedValuesFile.bat
Created January 10, 2013 15:42
[Batch] Parse a file with pipe | delimiters.
@echo off
setlocal EnableExtensions DisableDelayedExpansion
for /F "delims=" %%Z in ('type file.txt') do (
set "xLine=|%%Z"
call :Parse xLine
)
endlocal
pause >nul
goto :eof
@davidruhmann
davidruhmann / CombineLines.bat
Last active December 10, 2015 23:48
[Batch] Combine a files lines when the last char is not the eol character.
:: Without Delayed Expansion
@echo off
setlocal EnableExtensions DisableDelayedExpansion
for /f delims^=^ eol^= %%L in (myfile.txt) do (
set "xLine=%%L"
call set "xLine=%%xLine:"=%%" %= Fix for Limitation 2 =%
rem Remove the .0x08 when wanting to output the text to a file.
2>nul ( <nul set /p =".%%L" ) %= Fixed Limitation 1, 3, and 4 =%
call :NewLine
)
@davidruhmann
davidruhmann / InjectHtml.bat
Last active December 11, 2015 02:58
[Batch] Find and Inject into a HTML file.
@echo off
setlocal EnableExtensions DisableDelayedExpansion
:: Limitations
:: 1. Any leading close bracket ] characters will be trimmed due to the delims=]
:: 2. The text being replaced (search term) may not contain any equal signs =
for /r %%F in (html.txt) do if exist "%%~fF" (
for /f "tokens=1,* delims=]" %%K in ('type "%%~fF" ^| find /n /v ""') do (
set "_=%%L"