【问题标题】:Moving Same Name Folders into Another Folder将同名文件夹移动到另一个文件夹
【发布时间】:2020-11-28 21:50:04
【问题描述】:

我有具有唯一父文件夹名称的报表文件夹:

C:\User\USER\Downloads\LTFT01\Report
C:\User\USER\Downloads\LTFT02\Report

我制作了一个脚本,将“报告”文件夹的名称更改为包含其父文件夹的名称,并将该文件夹移至其他位置(例如 Report -> LTFT01Report)。但是,现在我遇到了一个问题,一旦完成一次,第二份报告(在原始文件夹中制作)与我之前移动的报告同名并且拒绝移动。下面是代码:

#Get report folder path
$ReportPath = "C:\Users\USER\Downloads\*\Report"
$MasterReportPath = "C:\Users\USER\Downloads\MasterReports"

#Rename report folder to {currentparentname}report
Get-Item -Path $ReportPath | ForEach-Object {$a = $_.FullName | split-path -Parent | split-path -leaf; Rename-Item -Path $_.FullName -NewName $a"Report"}

#Move report folder
$AnyNamedReportFolder = Get-Item "C:\Users\USER\Downloads\*\*Report*" -Exclude *.jmx, *.csv
Move-Item -Path $AnyNamedReportFolder -Destination $MasterReportPath

因此,在报告第 3 次运行后,第 2 次报告(来自第 2 次运行)将无法移动,因为 LTFT01Report(示例)已存在于 $MasterReportPath 中。

我认为我需要根据已经存在的内容附加或添加一个数字,并在此基础上增加。例如,如果 LTFT01Report 已存在于 $MasterReportPath 中,则同一报告的第二次运行应从 LTFT01Report 重命名为 LTFT01Report2 或其他区分。

但是,我不熟悉哪些 PowerShell cmdlet 可以帮助我做到这一点,但我会进行研究。如果在此期间有人可以将我推向正确的方向,那将非常有帮助!

【问题讨论】:

    标签: powershell


    【解决方案1】:

    谢天谢地,我最终创造了答案:

    #Get report folder path
    $ReportPath = "C:\Users\USER\Downloads\TestPlans\*\Report"
    $ReportInNamePath = "C:\Users\USER\Downloads\TestPlans\*\*Report*"
    $MasterReportPath = "C:\Users\USER\Downloads\MasterReports"
    
    #Rename report folder
    Get-Item -Path $ReportPath | ForEach-Object {$a = $_.FullName | split-path -Parent | split-path -leaf; Rename-Item -Path $_.FullName -NewName $a"Report"}
    
    #Append Date and Time
    Get-Item -Path $ReportInNamePath | ForEach-Object {$a = $_.FullName | split-path -leaf; Rename-Item -Path $_.FullName -NewName ($a + $_.CreationTime.ToString("yyyMMdd-HHmmss"))}
    
    #Move report folder
    $AnyNamedReportFolder = Get-Item $ReportInNamePath -Exclude *.jmx, *.csv
    Move-Item -Path $AnyNamedReportFolder -Destination $MasterReportPath
    

    我做了两件事:

    1. 在添加父名称后将文件夹创建日期附加到文件名末尾。例如报告 -> LTFT01Report20201130-112918。这有助于为任何测试创建唯一的报告文件夹名称,即使是多次运行相同的测试。
    2. 确保 MasterReports 文件夹的位置不属于第一个通配符,因为它也在更改其中的子报表名称。我只是将所有文件夹与另一位父级的 MasterReports 处于同一级别。例如:

    C:\Users\USER\Downloads\LTFT01 -> C:\Users\USER\Downloads\TestPlans\LTFT01

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-10-25
      • 1970-01-01
      • 2022-08-21
      • 1970-01-01
      • 2021-11-29
      • 1970-01-01
      相关资源
      最近更新 更多