Why does this Powershell Script create converted images OUTSIDE the Rootfolder?

1

The following Powershell script is supposed to process all designated images INSIDE the specified Rootfolder. Some of the renamed output images are generated OUTSIDE the Rootfolder. Any Powershell gurus have any idea why? How can I output files only in the Rootfolder and not OUTSIDE the Rootfolder?



# This script requires ImageMagick
# Configuration
# Enter the full path of the folder that contains the images
$Rootfolder = "C:\temp\rktest"


$Recursive=$true
# Change these if necessary
$fileExtensions = "*.png"
$fileNameSuffix = "_resized" # the text to be appended to the output filename to indicate that it has been modified

$files = $null;
$fileCount = 0

# Check if the root folder is a valid folder. If not, try again.
if ((Test-Path $RootFolder -PathType 'Container') -eq $false) {
    Write-Host "'$RootFolder' doesn't seem to be a valid folder. Please try again" -ForegroundColor Red
    break
}

# Get all image files in the folder
if ($Recursive) {
    $files = gci $RootFolder -Filter $fileExtensions -File -Recurse
} 

# If there are no image files found, write out a message and quit
if ($files.Count -lt 1) {
    Write-Host "No image files with extension '$fileExtensions' were found in the folder '$RootFolder'" -ForegroundColor Red
    break
}

# Loop through each of the files and process it
foreach ($image in $files) {
    $newFilename = $image.DirectoryName + " " + $image.BaseName + $fileNameSuffix + $image.Extension
    $imageFullname = $image.FullName

    write-host "Processing image: $imageFullname" -ForegroundColor Green
#This line contains the ImageMagick commands
    & convert.exe $image.FullName -resize 50% $newFilename

    $fileCount++
}

Write-Host "$fileCount images processed" -ForegroundColor Yellow

bobkush

Posted 2018-06-02T05:30:31.000

Reputation: 360

What do you mean with OUTSIDE? – LotPings – 2018-06-02T05:46:10.480

You're not showing the complete script. – Gerard H. Pille – 2018-06-02T06:29:42.040

Gerald - I am showing the complete script - what do you see is missing? – bobkush – 2018-06-02T19:46:30.470

I see break commands. They are meant to exit a loop but are not inside a loop. I see a variable $Recursive that is never set. – Gerard H. Pille – 2018-06-03T00:37:35.990

Answers

1

Remove the space between directory and filename, put a backslash

$newFilename = $image.DirectoryName + "\" + $image.BaseName + $fileNameSuffix + $image.Extension

Gerard H. Pille

Posted 2018-06-02T05:30:31.000

Reputation: 542

That's IMO contrary to the OPs intention. – LotPings – 2018-06-02T07:59:25.607

Gerald - please explain the purpose of a backslash instead of a space. – bobkush – 2018-06-02T19:49:00.590

The purpose is to put the file in the directory specified by DirectoryName. Don't you know the meaning of the backslashes as in "C:\temp\rktest" ??? – Gerard H. Pille – 2018-06-02T20:17:27.337

Gerald - Thanks for the explanation. Your suggestion to change the newFilename line from the space between directory and filename to a backslash FIXED the problem. The script now creates all OUTPUT files in the same subfolder as the SOURCE images. – bobkush – 2018-06-02T20:38:34.460

In that case, you could perhaps accept my answer? – Gerard H. Pille – 2018-06-02T20:40:40.427

@LotPings, thanks for showing me the errors of my ways. – Gerard H. Pille – 2018-06-02T20:42:13.160

0

If I understand right the resized images should be placed in $RootFolder but retain the subfolder names as part of the filename, space separated.

The following script results in this sample tree:

> tree /F
C:.
└───temp
    └───rktest
        │   Image.png
        │   Image_resized.png
        │   SubDirL1 Image_resized.png
        │   SubDirL1 SubDirL2 Image_resized.png
        │
        └───SubDirL1
            │   Image.png
            │
            └───SubDirL2
                    Image.png

It creates a calculated property RelPAth with a select, adding it to the $file collection.
This is done by first removing the RootFolder from FullName and replacing any remaining path separators \ with spaces.

When building the new file name the extension is replaced with suffix+extension.

## Q:\Test\2018\06\02\SU_1327985.ps1
# This script requires ImageMagick
# Configuration
# Enter the full path of the folder that contains the images
$Rootfolder = "C:\temp\rktest"

$Recursive=$true
# Change these if necessary
$fileExtensions = "*.png"
$fileNameSuffix = "_resized" # the text to be appended to the output filename to indicate that it has been modified

$files = $null;
$fileCount = 0

# Check if the root folder is a valid folder. If not, try again.
if ((Test-Path $RootFolder -PathType 'Container') -eq $false) {
    Write-Host "'$RootFolder' doesn't seem to be a valid folder. Please try again" -ForegroundColor Red
    break
}

# Get all image files in the folder
if ($Recursive) {
    $files = gci $RootFolder -Filter $fileExtensions -File -Recurse |
      select *,@{n='RelPath';e={ ($_.FullName.Replace($RootFolder+"\",'')).Replace('\',' ') }}
} 

# If there are no image files found, write out a message and quit
if ($files.Count -lt 1) {
    Write-Host "No image files with extension '$fileExtensions' were found in the folder '$RootFolder'" -ForegroundColor Red
    break
}

# Loop through each of the files and process it
foreach ($image in $files) {
    $newFilename = Join-Path $RootFolder ($image.RelPath.Replace($image.Extension,($fileNameSuffix+$image.Extension)))
    write-host "Processing image: $($image.Fullname)" -ForegroundColor Green
    #This line contains the ImageMagick commands
    & magick convert $image.FullName -resize 50% $newFilename

    $fileCount++
}

Write-Host "$fileCount images processed" -ForegroundColor Yellow

Replaced convert.exe with magick convert due to my ImageMagick version.

LotPings

Posted 2018-06-02T05:30:31.000

Reputation: 6 150

Sorry I can't follow: as my sample tree shows all files in $RootFolder and in subdirs were processed. As long as the -Recurse parameter in the gci command is present it should process all subdirs. – LotPings – 2018-06-02T19:55:05.897

I did ask what you mean with OUTSIDE and got no answer. I assumed it was your intention to place them all in $RootFolder and contain the folder name prepended in the file name - thats what the code does now. – LotPings – 2018-06-02T20:16:42.100

Not toplevel that would be root. You should have better explained where you want them first place. – LotPings – 2018-06-02T20:20:13.967

LotPings - Thank you for your instructions. Your script works perfectly for placing the OUTPUT files in the TOP level folder. I am sorry if my question made it difficult to understand what I wanted - but between you and Gerald - I now have a script that can create ALL output files in the TOP folder and a script that can create ALL output files in the same subfolder as the source images. – bobkush – 2018-06-02T20:47:29.920