PowerShell
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 = @" <img src="./images/{0}" title="" alt="" > "@ $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
$html_tag = @"
<img
src="./images/{0}"
title=""
alt=""
>
"@- This is a Multi-Line String Variable.
- $ denotes a variable.
- @" & "@ (on lines by themselves) begin and end the string.
- When we use this for a text substitution later, the inserted text will go between the braces...
- {0}
- 0 for the first passed variable.
- We could pass more.
- {0}
$text = ""
- We will keep on adding the output text to this variable as it is created.
- Don't ask me why I am saying we.
- I really don't like it when folks say we, so I'll try to stop that.
- Or maybe, I should think of it as the Computer and I.
- And we can do as we like.
$DIR_IN = 'C:\alpha\input'
$DIR_OUT = 'C:\alpha\output'- These are personal defaults.
- The needs of others will likely be different.
$Items = Get-ChildItem -Path $DIR_IN -Name
- Creates a listing of the file names in the input directory.
- Get-ChildItem gets all the files in a directory.
- -Name tells it we don't care about the files, only their names.
- I could do this in the loop, but it's just easier for me to do it in a separate line.
ForEach ($file_name in $items)
{
$text += $html_tag -f $file_name
}- A simple for loop.
- This is where the file name is inserted into the multi-line string and the lot is appended to the running $text variable.
- -f indicates we want to format the preceding string variable...
- Or in other words, insert the trailing string variable into the {0} slot.
$file_out = Join-Path $DIR_OUT "img_text.txt"
- This is the save name.
$text > $file_out
- > is the redirect operator.
- I am (and I had to catch myself, as I was going to say we are) redirecting the output from standard output (the screen) to a text file for ease of use at a later time.
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.