问题
I have report folders that have unique parent folder names as such:
C:\User\USER\Downloads\LTFT01\Report
C:\User\USER\Downloads\LTFT02\Report
I have made a script that changes the name of the 'Report' folder to include the name of its parent folder and move that folder elsewhere (e.g. Report -> LTFT01Report). However, now I'm running into an issue whereby once this is done once, the second report (made within the original folder) has the same name as the one I moved previously and refuses to move. Here's the code below:
#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
So, after the 3rd run of a report, the 2nd report (from 2nd run) will fail to move over since LTFT01Report (example) already exists in $MasterReportPath.
I think I need to append or prepend a number based on what already exists and increment based on that. Example if LTFT01Report already exists in $MasterReportPath then the 2rd run of the same report should rename from LTFT01Report to LTFT01Report2 or something to differentiate.
However I'm not familiar with what PowerShell cmdlets would help me do that, but I'll be researching. If anyone in the meantime could nudge me in the right direction, it would be really helpful!
回答1:
Well I ended up creating the answer thankfully:
#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
The two things I did:
- Append date of folder creation to end of file name AFTER adding the parent name. So for example Report -> LTFT01Report20201130-112918. This helps create unique report folder names for any test, even same tests run multiple times.
- Ensured the location of the MasterReports folder did not fall under the first wildcard, since it was also changing the child report names within it. I just subtended all folders on the same level as MasterReports by another parent. So for example:
C:\Users\USER\Downloads\LTFT01 -> C:\Users\USER\Downloads\TestPlans\LTFT01
来源:https://stackoverflow.com/questions/65054811/moving-same-name-folders-into-another-folder