【问题标题】:Powershell apply action on each subfolderPowershell 对每个子文件夹应用操作
【发布时间】:2023-04-07 00:42:01
【问题描述】:

我需要一些关于我编写的 PS 脚本的帮助。我对 Powershell 没有经验,但根据我在网上找到的内容,我能够编写一个脚本来完成我想要的。但是,我无法让它在任何子文件夹上工作。

目前我有一个名为“C:\Temp\Test”的文件夹。该文件夹内有许多子文件夹,每个用户一个,名为 Greg、Tom、Alice 等。因此文件夹结构如下所示

C:\Temp\Test\Greg

C:\Temp\Test\Tom

C:\Temp\Test\Alice

等等……

每个人的子文件夹都包含数十个甚至数百个 .JPG 和 .TIF 图片文件。每个图片文件都需要有一个同名的 .XML 文件,其中包含姓名和出生日期等属性。我为每个用户创建了第一个 .XML 文件,并创建了以下脚本来扫描文件夹并为每个剩余的图片文件创建 .XML 文件。然后将第一个 .XML 文件的内容复制到新创建的文件中。这是我目前所拥有的:

$sourceFiles = Get-ChildItem -Path "C:\Temp\Test\*" -Recurse -File -Include *.tif,*jpg
$destinationFiles = Get-ChildItem -Path "C:\Temp\Test" -Recurse -File -Filter *.xml

foreach($sourceFile in $sourceFiles) {
  $exists = $destinationFiles | Where-Object {$_.BaseName -eq $sourceFile.BaseName}
  if(!$exists) {
    New-Item -type File "C:\Temp\Test\$($sourceFile.BaseName).xml"
  }
}

$file1 = Get-ChildItem "C:\Temp\Test" -Filter *.xml | Select-Object -First 1
$file2 = Get-ChildItem "C:\Temp\Test" -Filter *.xml -recurse -Exclude $file1

cd "C:\Temp\Test"
Get-Content $file1 | Add-Content $file2

当我在“C:\Temp\Test”目录中有测试图片时,脚本运行良好。我想不通的是如何更改脚本以在每个人的子文件夹上运行并将新的 .XML 文件放在图片所在的子文件夹中。如果有人对我如何做到这一点有任何想法,或者我如何改进这个脚本我将非常感谢你的想法。正如我所说,我对PS不是很有经验。谢谢!

【问题讨论】:

    标签: xml powershell-3.0


    【解决方案1】:

    不确定,但试试用这个。

    # This will split the path and give parent folder
    $ParentFullPath = Split-Path $sourceFile.BaseName -Parent
    
    # This will give you the parent folder name
    $ParentPath = Split-Path $ParentFullPath -Leaf
    
    New-Item -type File "C:\Temp\Test\$ParentPath\$($sourceFile.BaseName).xml"
    

    很好读 Get-Help Split-Path -Full

    问候,

    kvprasoon

    【讨论】:

      【解决方案2】:

      感谢普拉松的回复!我最终把这个项目搁置了一段时间,当我把它重新捡起来时,我重新做了我做过的事情。在花了一些时间更好地学习 Powershell 之后,我想出了一些几乎正是我想要的东西。我觉得我应该分享它,以防其他人遇到类似问题。

      #This is the Template File
      
      $file1 = "C:\Temp\000.xml"
      
      #This browses through each subfolder and finds any files that are JPG or PNG 
      files. 
      
      Get-ChildItem -recurse -force | ForEach {
      If (($_.extension -eq ".tif") -or ($_.extension -eq ".jpg"))
      
      #This creates a new XML file for each image file found in the previous step 
      and gives it the same name as the image file. It then copies the contents of 
      the XML Template file into the new XML file.
      
      {New-Item -type File -Path $($_.DirectoryName) -Name "$($_.BaseName).xml"   
      -value "$(Get-Content $file1)" -ErrorAction SilentlyContinue}
      }
      
      #After the XML files are created, this finds any XML files in 
      subdirectories. It then looks for the \record\name element and updates it to 
      the name of the folder that the file is located in.
      
      Get-ChildItem -include *.xml -recurse | % ($_) {
      [xml]$xml = Get-Content ("$($_.Fullname)")
      $xml.record.name = (Get-
      Culture).TextInfo.ToTitleCase("$($_.Directory.BaseName.ToLower())")
      $xml.save("$($_.Fullname)")}
      

      图片和XML文件所在的文件夹都是大写的。 (即约翰·多伊)。我希望 XML 文件中的“名称”元素是标题大小写而不是全部大写,因此代码的最后一部分提取 XML 文件所在的文件夹名称并将其从 JOHN DOE 转换为 John Doe。然后将该值放入 XML 文件的“名称”元素中。

      希望有人觉得这个脚本有用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-03-21
        • 1970-01-01
        • 1970-01-01
        • 2021-01-21
        • 1970-01-01
        相关资源
        最近更新 更多