以下 Powershell 脚本应该处理指定 Rootfolder 内的所有指定图像。一些重命名的输出图像是在 Rootfolder 之外生成的。任何 Powershell 专家都知道为什么吗?我怎样才能只在 Rootfolder 中输出文件,而不在 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
答案1
删除目录和文件名之间的空格,添加反斜杠
$newFilename = $image.DirectoryName + "\" + $image.BaseName + $fileNameSuffix + $image.Extension
答案2
如果我理解正确的话,应该放置调整大小的图像,
$RootFolder
但保留子文件夹名称作为文件名的一部分,并用空格分隔。
以下脚本生成此示例树:
> 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
RelPAth
它使用选择创建一个计算属性,并将其添加到 $file 集合中。
首先从 FullName 中删除 RootFolder,然后将任何剩余的路径分隔符替换\
为空格。
创建新文件名时,扩展名将被后缀+扩展名替换。
## 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
magick convert
由于我的 ImageMagick 版本,因此用 convert.exe 替换了它。