Por que esse Script Powershell cria imagens convertidas FORA da Pasta Raiz?

1

O script Powershell a seguir deve processar todas as imagens designadas dentro da pasta raiz especificada. Algumas das imagens de saída renomeadas são geradas FORA da Rootfolder. Qualquer guru do Powershell tem alguma ideia do porquê? Como posso produzir arquivos apenas na Rootfolder e não FORA da 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

    
por bobkush 02.06.2018 / 07:30

2 respostas

0

Se bem entendi, as imagens redimensionadas devem ser colocadas em $RootFolder , mas mantém os nomes das subpastas como parte do nome do arquivo, separados por espaços.

Os seguintes resultados de script nesta árvore de exemplo:

> 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

Cria uma propriedade calculada RelPAth com um select, adicionando-a à coleção de arquivos $.
Isso é feito primeiro removendo o RootFolder de FullName e substituindo qualquer separador de caminho restante \ por espaços.

Ao criar o novo nome de arquivo, a extensão é substituída por sufixo + extensão.

## Q:\Test18\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

Substituído convert.exe com magick convert devido à minha versão do ImageMagick.

    
por 02.06.2018 / 09:50
1

Remova o espaço entre o diretório e o nome do arquivo, coloque uma barra invertida

$newFilename = $image.DirectoryName + "\" + $image.BaseName + $fileNameSuffix + $image.Extension
    
por 02.06.2018 / 08:28