Skip to content

Instantly share code, notes, and snippets.

@davidruhmann
davidruhmann / EchoOnSameLine.bat
Created January 16, 2013 02:36
[Batch] Alternate Method of echoing output onto the same line. No CR+LF !
@echo off
setlocal EnableDelayedExpansion
call :createSub
call :echoWithoutLinefeed "=hello"
call :echoWithoutLinefeed " world"
exit /b
:echoWithoutLinefeed
> txt.tmp (echo(%~1!sub!)
copy txt.tmp /a txt2.tmp /b > nul
@davidruhmann
davidruhmann / DynamicMenu.bat
Last active May 7, 2023 06:29
[Batch] Dynamic Menu
:: Hide Command and Set Scope
@echo off
setlocal EnableExtensions
:: Customize Window
title My Menu
:: Menu Options
:: Specify as many as you want, but they must be sequential from 1 with no gaps.
:: Step 1. List the Application Names
@davidruhmann
davidruhmann / FindandReplace.bat
Last active May 7, 2023 05:26
[Batch] Find and Replace loops through text files and replaces search terms with strings.
@echo off
setlocal EnableExtensions DisableDelayedExpansion
:: Find and Replace [Works with Special Characters]
:: by David Ruhmann
:: About
:: This is a proof of concept to illustrate the difficulty and limitations in
:: regards to the batch string replacement functionality. Delayed Expansion was
:: not used as an exercise in showing the most common usage of batch.
@davidruhmann
davidruhmann / ExtractURL.bat
Last active May 7, 2023 02:15
[Batch] Extract the URL from a bunch of .url shortcut files.
@echo off
setlocal EnableExtensions
for %%A in (*.url) do (
for /f "usebackq tokens=1,* delims==" %%X in ("%%~fA") do (
if /i "%%X"=="Url" (
echo(%%~nA = %%Y>>list.txt
echo(%%Y
)
)
)
@echo off
setlocal EnableDelayedExpansion
if "%~1"==":::" goto :spinnerThread
:menuLoop
<nul set /p menu=Select menu[1 or 2]=
call :GetKey
echo(
echo Pressed '!key!'
if !key!==1 call :menu1
@davidruhmann
davidruhmann / ExtractVariableSet.bat
Last active May 7, 2023 01:49
[Batch] Extract Variable Set
@echo off
setlocal EnableExtensions EnableDelayedExpansion
:: Setup
set "ResultsFolder=results"
set "ExportFile=output.txt"
:: Verify that the Results folder exists
if not exist "%ResultsFolder%\*" md "%ResultsFolder%"
set "OutputFile=%ResultsFolder%\%ExportFile%"
@davidruhmann
davidruhmann / Output.txt
Created July 26, 2013 02:45
[Batch] Output Tips and Tricks by Dave Benham
CMD processes redirection from left to right. You want to first redirect 2 (stderr) to &1 (stdout), then redirect 1 (stdout) to something else. At this point stderr will still be redirected to the previous definition of stdout. The pipe will still work with the old definition of stdout (which now contains stderr).
If you don't care about stdout then you can redirect to nul
program.exe 2>&1 1>nul | find " "
If you want to capture stdout to a file then redirect to a file
program.exe 2>&1 1>yourFile | find " "
@davidruhmann
davidruhmann / carlos.bat
Last active May 6, 2023 23:32
[Batch] Pure Batch Color Routine by David Ruhmann based of DosTips' Carlos
@Echo Off
Call :Color A "######" \n E "" C " 21 " E "!" \n B "######" \n
Pause >Nul
Exit /B
:Color
:: v21
:: Arguments: hexColor text [\n] ...
:: \n -> newline ... -> repeat
:: Supported in windows XP, 7, 8.
@davidruhmann
davidruhmann / EventParams.cs
Last active March 1, 2023 22:03
C# Dynamic EventArgs
// Copyright (c) 2015 David Ruhmann
using System;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Collections.Generic;
namespace ChartedCode
{
@davidruhmann
davidruhmann / Hybrid.bat
Last active November 22, 2022 01:16
[Batch] JScript and Batch Hybrid
@if (@CodeSection == @Batch) @then
:: The first line above is...
:: in Batch, a valid IF command that does nothing.
:: in JScript, a conditional compilation IF statement that is false.
:: So the following section is omitted until the next "[at]end".
:: Note: the "[at]then" does nothing and is only for readablility.
:: Batch Section