Skip to content

Instantly share code, notes, and snippets.

@ijprest
ijprest / strlen.bat
Created September 1, 2011 03:41
CMD Batch file implementation of strlen
@echo off
setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
call :strlen "%~1"
echo strlen("%~1") == !ERRORLEVEL!
goto :EOF
:strlen
setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
set __LEN=0
@ijprest
ijprest / toupper.bat
Created September 10, 2011 01:52
CMD Batch file implementation of toupper() and tolower()
@echo off & SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
SET STR=Test String
SET STR
call :tolower STR
SET STR
call :toupper STR
set STR
goto :EOF
:: toupper & tolower; makes use of the fact that string
@ijprest
ijprest / tohex.bat
Created September 10, 2011 02:07
CMD Batch file conversion of a decimal number to hex
@echo off & setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
set LOOKUP=0123456789abcdef &set HEXSTR=&set PREFIX=
if "%1"=="" echo 0&goto :EOF
set /a A=%* || exit /b 1
if !A! LSS 0 set /a A=0xfffffff + !A! + 1 & set PREFIX=f
:loop
set /a B=!A! %% 16 & set /a A=!A! / 16
set HEXSTR=!LOOKUP:~%B%,1!%HEXSTR%
if %A% GTR 0 goto :loop
echo %PREFIX%%HEXSTR%
@ijprest
ijprest / setprompt.bat
Created September 15, 2011 03:14
CMD Batch file to put a separator between each command you run at the prompt
@echo off
for /f "tokens=1,2 delims=: " %%Q IN ('mode con:') DO IF "%%Q"=="Columns" SET _COLS=%%R
SET /A _COLS=%_COLS% - 13
SET _P=
:loop
SET _P=%_P%-
SET /A _COLS=%_COLS% - 1
IF %_COLS% GTR 0 goto :loop
SET PROMPT=%_P%$S$T$_$P$G
@ijprest
ijprest / guidgen.ahk
Created October 6, 2012 20:00
AutoHotkey (AHK) script to generate & paste a new GUID
; Exposes two hotkeys:
; - Win+G generates & pastes a new lowercase guid
; - Win+Shift+G generates & pastes a new UPPERCASE guid
; In both cases, the guid is left on the clipboard so you can easily paste it more than once.
;
GUID()
{
format = %A_FormatInteger% ; save original integer format
SetFormat Integer, Hex ; for converting bytes to hex
VarSetCapacity(A,16)
param (
[string]$UserName = "$((Get-ChildItem Env:USERDOMAIN).Value)\$((Get-ChildItem Env:USERNAME).Value)",
[string]$Password = $(Read-Host "Enter a password for user '$UserName'")
)
# Set up auto-logon
$resumed = 0
$auditing = 0
if ((Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Setup\State").ImageState -ne "IMAGE_STATE_COMPLETE") {
$auditing = 1
param(
[int]$Delay = 0
)
$Win32 = Add-Type -Name WinAPICall -Namespace SystemParametersInfo -PassThru -MemberDefinition @'
[DllImport("user32.dll", EntryPoint = "SystemParametersInfo")]
public static extern bool SystemParametersInfo(uint uiAction, uint uiParam, uint pvParam, uint fWinIni);
'@
if($Win32::SystemParametersInfo(0x17,$delay,$null,0)) {
Write-Host "Keyboard delay changed to: $delay"
} else {
@ijprest
ijprest / IniFile.psm1
Created April 13, 2015 00:57
Powershell function to read a standard Windows INI file (using Win32 APIs)
#requires -Version 2
Add-Type -Name _e9b6b3c331bdc58f488487320803ad89 -namespace _e95bcd88febe44bf44ad6600fdd509e7 -MemberDefinition @'
[DllImport("kernel32",CharSet=CharSet.Unicode)] public static extern int GetPrivateProfileStringW(string section, string key, string def, IntPtr retVal, int size, string filePath);
[DllImport("kernel32",CharSet=CharSet.Unicode)] public static extern int GetPrivateProfileStringW(string section, IntPtr key, string def, IntPtr retVal, int size, string filePath);
[DllImport("kernel32",CharSet=CharSet.Unicode)] public static extern int GetPrivateProfileStringW(IntPtr section, IntPtr key, string def, IntPtr retVal, int size, string filePath);
[DllImport("kernel32",CharSet=CharSet.Unicode)] public static extern long WritePrivateProfileStringW(string section, string key, string val, string filePath);
'@
function Read-IniFile {
<#
.SYNOPSIS
function Confirm-Host {
<#
.SYNOPSIS
Prompts the user to OK or Cancel an operation via the host.
.DESCRIPTION
Prompts the user to OK an operation by hitting ENTER or cancel
it by hitting ESC.
.EXAMPLE
if(Confirm-Host) { ... }
.OUTPUTS
@ijprest
ijprest / convert-to-amd.js
Created July 20, 2015 23:49
Very simple script to convert a normal JS module to a RequireJS-compatible AMD module; intended for command-line usage, and integration into build systems.
#!/usr/bin/env node
///
/// Very simple script to convert a normal JS module to a RequireJS-compatible
/// AMD module; intended for command-line usage, and integration into build
/// systems.
///
/// Reads from stdin & writes to stdout; output can be piped to uglifyjs to
/// minify/compress the code.
///
/// Examples: