Powershell Image Conversion script – Part 4: Finalizing and check functionality


This the final part of the series. I’ll show the different functions that check several parameters so the script will run flawlessly.

The Init function

The name “Init” is a name I gave to the function becaue it’s the initial function that calls all the other functions.
The first part of the function look like this:

First all the errors are cleared so no messages from former executions will be in the logfile.
Second the script checks if the script is executed to create a config. If the parameter is set the “Create-Config” function is called.

The next part of the script checks if the parameter “config” is set.

It checks if the file exists. If it does it the file is loaded. If not the script is stopped. The config file is not mandatory for the script because it can work with only parameters.

The next part is to check all the values:

Function Check-Logdir

The code check if the logdir is given or exists. It look like this:

Function Check-SourceExtension

The code check for a source extension parameter. If no parameter is given it uses the standard source extension.

Function Check-TargetExtension

This function works the same as the function Source-Extension but sets a different value in case of an error.

Function Check-Dimensions

The following function checks if the dimensions given at the parameters are right.

Function Check-Directories

This function checks if a directory value is given or if it exists.

The next part is to get the image format of the target extension:

The code for getting the image format looks like this:

The next part is to get all the files and folders from the given directory.

The next part is to start converting the images

In this part of code another function is called named “Move-Image”. This function moves the original file to the given archive directory. The code looks like this:

The above code uses the function “Replace-String” that is also used in the function “Convert-Image”.

The last part of the code starts the reorganization and cleaning of the directory:

The function for reorganizing the directory by selecting the first three characters from the image’s directory. Creating an new directory and move the images to the archive:

An example for an archive folder would be:

  • C:\Imageconversion\Archive\104\10453678\

Where the original directory would be:

  • C:\Imageconversion\Source\10453678\

The last function cleans all the folder given. The original images are moved away from the source directory what leaves empty directories. To clean them the following function is used:

I hope you enjoyed the article. It contained a lot functionality what created the solution. If you have any recommendation or remarks about the article leave a comment.

The full script can be downloaded from here.

Leave a Reply