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

4
  • What do you mean with OUTSIDE?
    – LotPings
    Commented Jun 2, 2018 at 5:46
  • You're not showing the complete script. Commented Jun 2, 2018 at 6:29
  • Gerald - I am showing the complete script - what do you see is missing?
    – bobkush
    Commented Jun 2, 2018 at 19:46
  • 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. Commented Jun 3, 2018 at 0:37

2 Answers 2

1

Remove the space between directory and filename, put a backslash

$newFilename = $image.DirectoryName + "\" + $image.BaseName + $fileNameSuffix + $image.Extension
6
  • That's IMO contrary to the OPs intention.
    – LotPings
    Commented Jun 2, 2018 at 7:59
  • Gerald - please explain the purpose of a backslash instead of a space.
    – bobkush
    Commented Jun 2, 2018 at 19:49
  • 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" ??? Commented Jun 2, 2018 at 20:17
  • 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
    Commented Jun 2, 2018 at 20:38
  • In that case, you could perhaps accept my answer? Commented Jun 2, 2018 at 20:40
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.

4
  • 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
    Commented Jun 2, 2018 at 19:55
  • 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
    Commented Jun 2, 2018 at 20:16
  • Not toplevel that would be root. You should have better explained where you want them first place.
    – LotPings
    Commented Jun 2, 2018 at 20:20
  • 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
    Commented Jun 2, 2018 at 20:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .