같은 이름의 폴더를 다른 폴더로 이동
다음과 같이 고유 한 상위 폴더 이름을 가진 보고서 폴더가 있습니다.
C:\User\USER\Downloads\LTFT01\Report
C:\User\USER\Downloads\LTFT02\Report
부모 폴더의 이름을 포함하도록 '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
따라서 보고서를 세 번째 실행 한 후 $ MasterReportPath에 LTFT01Report (예제)가 이미 존재하므로 두 번째 보고서 (두 번째 실행에서)가 이동하지 못합니다.
이미 존재하는 것을 기반으로 숫자를 추가하거나 앞에 추가하고 그에 따라 증가시켜야한다고 생각합니다. 예를 들어 LTFT01Report가 이미 $ MasterReportPath에 존재하는 경우 동일한 보고서의 두 번째 실행은 LTFT01Report에서 LTFT01Report2 또는 구별 할 이름으로 이름을 변경해야합니다.
그러나 PowerShell cmdlet이이 작업을 수행하는 데 어떤 도움이되는지 잘 모르지만 연구 할 것입니다. 그동안 누군가가 저를 올바른 방향으로 밀어 줄 수 있다면 정말 도움이 될 것입니다!
답변
감사하게도 답을 만들었습니다.
#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
내가 한 두 가지 :
- 상위 이름을 추가 한 후 파일 이름 끝에 폴더 생성 날짜를 추가합니다. 예를 들어 Report-> LTFT01Report20201130-112918. 이렇게하면 동일한 테스트가 여러 번 실행되는 경우에도 모든 테스트에 대해 고유 한 보고서 폴더 이름을 만드는 데 도움이됩니다.
- MasterReports 폴더의 위치가 첫 번째 와일드 카드에 속하지 않는지 확인했습니다. 그 안에있는 하위 보고서 이름도 변경 되었기 때문입니다. 다른 부모가 MasterReports와 동일한 수준의 모든 폴더를 대체했습니다. 예를 들면 다음과 같습니다.
C:\Users\USER\Downloads\LTFT01 -> C:\Users\USER\Downloads\TestPlans\LTFT01