The longer I work in Windows, the more I find myself using cmd.exe. I use PowerShell plenty too, but if I want something quick that can run anywhere [someone's on Windows], I use a batch file, and over the years, kinda like VIm, I've slowly become if not proficient then at least competent.

Nearly (and maybe even over at this point) thirty years ago I had a guy wisely tell me, when I was considering buying a new Mac or Windows PC, "It's all zeros and ones." Same for script languages, mostly. It might be a pain to learn batch scripting on Windows sometimes, but there's very little you can't do if you set your mind to it.

But this is a really neat trick to create "arrays" in batch that I've never seen. I've edited a bit to allow running in a .bat cleanly:

REM https://stackoverflow.com/questions/18462169/how-to-loop-through-array-in-batch

echo off


set Arr[0]=apple
set Arr[1]=banana
set Arr[2]=cherry
set Arr[3]=donut

set "x=0"

:SymLoop
if defined Arr[%x%] (
    call echo %%Arr[%x%]%%
    set /a "x+=1"
    GOTO :SymLoop

Clever.


While I'm at it, here's a PowerShell script I've been using to approximate grep there. I've dabbled in this problem before, but it's usually a good idea to reduce it to script instead of leaving only human-readable lessons learned:

param (
    [Parameter(Position=0)]
    [string]$needle,
    [Parameter(Position=1)]
    [string]$filepathToSearch,
    [Parameter(Position=2)]
    [string]$optionalFileForOutput
)

if ($optionalFileForOutput) {
    Get-ChildItem -Path $filepathToSearch |Select-String -Pattern $needle |Out-File -width 99999 $optionalFileForOutput
} else {
    select-string -path $filepathToSearch -pattern $needle
}

That captures the Out-File wackiness from the previous post but also wraps Select-Stringing a file, making it easier to remember, not that it's difficult. I should fix the casing.

Labels: , , , ,