Brett Code


Images to HTML Tags

I have a bunch of images that I want to add to a webpage. Since there are over seventy of them, adding them one by one would be tedious. Also, there's no individual artistry in their addition to the page. It is a simple mass posting with each image having the same style.

I wrote this code to output the appropriate HTML <img> Tags.

The code comes first (stripped of superfluous comments and commented out print statements) followed by a line-by-line deconstruction for deeper understanding.

PowerShell Code
Creating a List of <img> Tags from a Directory of Images

In order to work, this code would need to be copy/pasted into a PowerShell Script. It will not run if simply copy/pasted to the Command Line.

$html_tag = @"


$text = ""

$DIR_IN = 'C:\alpha\input'
$DIR_OUT = 'C:\alpha\output'

$Items = Get-ChildItem -Path $DIR_IN -Name
ForEach ($file_name in $items)
    $text += $html_tag -f $file_name

$file_out = Join-Path $DIR_OUT "img_text.txt"
$text > $file_out

Line-By-Line Code Deconstruction

A Brief Debriefing

This isn't rocket science. And although this and another short script have occupied me for most of the morning, coding gets faster over time.

Also, if we are being honest, the write-up eats as much time (if not more) than writing the script, itself. And yet, it keeps me off the streets, out of trouble, and motivated to take the next step. So, there you are.

command line image manipulations

© copyright 2019 Brett Paufler