PowerShell:旋转多个图像文件并将它们保存到具有新名称的目录中

PowerShell: Rotate multiple image files and save them to a directory with new name

我是 powershell 的新手(我刚在我们当地的大学上了一个学期)。我有一个目录,其中有很多图像文件 (.WMF) 不断更新,我需要编写一个脚本来获取这些文件,将它们保存到一个新目录,并使用“_90.wmf”将它们旋转 90 度添加到结尾。我已经搜索了一段时间,想出了一个可以旋转图像的小代码,但我无法将它保存到新目录中。有帮助吗?

if (Test-Path J:\CutRite\v90\Import\MV_Nest_PTX_copy)

{

   [Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")

   foreach($file in (ls "J:\CutRite\v90\Import\MV_Nest_PTX_copy\*.wmf")){

       $convertfile = new-object System.Drawing.Bitmap($file.Fullname)

       $convertfile.rotateflip("Rotate90FlipNone")

       $newfilname = ($file.Fullname)

       $convertfile.Save($newfilname, "wmf")

       $file.Fullname

   }  

}
else
{
   Write-Host "Path not found."
}

大功告成,只需正确创建新文件名即可:

if (Test-Path J:\CutRite\v90\Import\MV_Nest_PTX_copy)
{
   [Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
   foreach($file in (ls "J:\CutRite\v90\Import\MV_Nest_PTX_copy\*.wmf")){
       $convertfile = new-object System.Drawing.Bitmap($file.Fullname)
       $convertfile.rotateflip("Rotate90FlipNone")
       $newfilename = $file.Fullname.replace(".wmf","_90.wmf")
       $convertfile.Save($newfilname)
   }  
}

你可以用System.String的replace方法把.wmf改成_90.wmf存到$newfilename变量里,然后保存图片使用那个名字。

此外,不确定您为什么要加载 System.Windows.Forms - 除非您在未包含的片段中使用它,否则您不需要它。

更新:

如果您想完全保存到不同的目录:

$newfilename = "C:\path\to\new\dir\" + $file.name.replace(".wmf","_90.wmf")