同じ名前のフォルダを別のフォルダに移動する

Nov 28 2020

次のような一意の親フォルダー名を持つレポートフォルダーがあります。

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

'Report'フォルダーの名前を変更して、その親フォルダーの名前を含め、そのフォルダーを別の場所に移動するスクリプトを作成しました(たとえば、Report-> LTFT01Report)。しかし、今、これが一度行われると、2番目のレポート(元のフォルダー内で作成された)が以前に移動したものと同じ名前になり、移動を拒否するという問題が発生しています。以下のコードは次のとおりです。

#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回目の実行後、LTFT01Report(例)が$ MasterReportPathに既に存在するため、2回目のレポート(2回目の実行から)は移動に失敗します。

すでに存在するものに基づいて番号を追加または追加し、それに基づいてインクリメントする必要があると思います。たとえば、LTFT01Reportが$ MasterReportPathにすでに存在する場合、同じレポートの2回目の実行では、名前をLTFT01ReportからLTFT01Report2または区別するために変更する必要があります。

ただし、PowerShellコマンドレットがそれを行うのにどのように役立つかについてはよくわかりませんが、調査する予定です。その間に誰かが私を正しい方向に動かすことができれば、それは本当に役に立ちます!

回答

Adil.yuken Nov 30 2020 at 11:37

ありがたいことに、私は答えを作成することになりました。

#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

私がした2つのこと:

  1. 親名を追加した後、ファイル名の末尾にフォルダ作成日を追加します。たとえば、レポート-> LTFT01Report20201130-112918です。これにより、同じテストが複数回実行された場合でも、任意のテストに対して一意のレポートフォルダー名を作成できます。
  2. MasterReportsフォルダー内の子レポート名も変更されていたため、MasterReportsフォルダーの場所が最初のワイルドカードに該当しないことを確認しました。別の親がMasterReportsと同じレベルにあるすべてのフォルダーをサブテンディングしました。したがって、たとえば:

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