Skip to the content.

Control Flow

if/else conditions & for loops are probably the first thing anyone learns when starting their programming journey. Who doesn’t remember messing up those wonky nested if conditions involving i/j/k?

Overtime, one stops using these and turns to more sophisticated structures. In fact, the very fact that you infrequently rely on these constructs is considered an indicator of good programming skills. Is it though? I don’t know. You tell me.

Why do we study these constructs if we shouldn’t use them? We’ll not get into philosophical arguments about that; we’ll keep this civil. In this post, we’ll try to discover some use-cases where Control Flow statements can come in handy, our focus being on executing stuff from the commandline.

PS: Appending Custom Lines to File

I’ve been using a simple hack to insert filepaths into a MERGE.txt file, which when fed to FFMpeg, merges the specified AV files.

$count = ((ls) -match '^\d+\.mp4$').count;
$page = @(); $n=1; while ($n -ne $count+1) {$page += "file $n.mp4"; $n++}; $page | out-file -encoding ascii MERGE.txt; ffmpeg -f concat -safe 0 -i MERGE.txt -c copy OUTPUT.mp4
(ls) -match '^\d+\.mp4' | del

What this does, is it looks up all numbered .mp4 files in the current directory, pastes their paths (actually just their names since they’re in the current directory) with some customization into a MERGE.txt file using a while conditional, and feeds it to FFMpeg, which takes care of the merging business. (I usually number my files before merging, but you can obviously customize Get-ChildItem to get just the files that you need.) The last line deletes the individual files once a merged file is generated.

You can argue that you wouldn’t need a $n variable. Why don’t you just write to the file? Good question!

The advantage of using above approach is that you do just one write operation, instead of multiple write operations, depending on just how many times you want to write to file.

The other day, I used the same template to insert numbered lines to a CSV file.

$count=10;$page = @(); $n=1; while ($n -ne $count+1) {$page += "$n,,,"; $n++}
$page | out-file -encoding ascii test.csv -append

Although it did the job, I grimaced looking at it. “Why didn’t I just use a for loop?” I thought to myself. For a moment I sincerely began to doubt my abilities, but then I reminded myself that I’m only human. I’m allowed to err.

And so, I replaced this with a for loop.

$count=10;for(($i=1),($page=@()); $i -le $count; $i++){$page += "$i,,,"}
$page | Out-File -Encoding ascii test.csv -Append

There! Better. Now I can just focus on $count & my custom string, and I’m good. Powershell’s syntax for multiple initializations is a little clumsy, but it’s okay.

You can alternatively use the .. operator to specify the count range.

$count=10;$page=@(); 1..$count | forEach{$page += "$_,,,"}
$page | Out-File -Encoding ascii test.csv -Append

In essence, you’ve replaced for with a forEach.

You’ll see the merit of this last approach when you replace forEach with any cmdlet. You see, here, using 1..$count, an array of System.Int32 objects is passed down the pipeline. You can supply this to a custom-cmdlet that takes successive numeric values and produces suitable outputs.

That does it for now. I’ll update more use-cases as I find them along.

To learn how to write common Control Flow expressions in Powershell, you can take a look at my notes.