Moving Same Name Folders into Another Folder

回眸只為那壹抹淺笑 提交于 2020-12-15 00:44:30

问题


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:

  1. 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.
  2. 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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!