I have a files which are stored in the format
C:\Users\xx\test powershell script\studyID\number\filenamedfolder\example.jpg
The desired output is:
studyID_number_1
#the final digit should be sequentially numbered for each instance of the jpg in 'filenamedfolder'.
I have used code given in previous answers to find, rename and copy files to the root directory.
I am using Windows 10 and Powershell:
Major Minor Build Revision
----- ----- ----- --------
5 1 15063 1478
Get-childitem "C:\Users\xx\test powershell script\*" -include *.jpg -recurse |% {
$nextName = Join-Path -Path 'C:\Users\xx\test powershell script\*' -ChildPath $_.name
while(Test-Path -Path $nextName)
{
$nextName = Join-Path "C:\Users\xx\test powershell script" ($_.BaseName + "_$num" + $_.Extension)
$num+=1
}
$ParentOjbect =$_.Directory
$Parent =$ParentOjbect.Name
$GrandParent = $ParentOjbect.Parent
$GreatGran = $GrandParent.Parent
$Extension = $_.Extension
$countRef = [ref] 0
Copy-item $_ -Destination (Join-Path "C:\Users\xx\test powershell script" ('{0}_{1}_{2}{3}' -f $GrandParent,$GreatGran,++$countRef.Value,$Extension))
}
I have attempted to use code to sequentially name files but my code is not sequentially naming files when more than one jpg exists in the 'filenamedfolder'. Instead only one jpg is copied and renamed into the root folder.
I have never coded in powershell and I am not sure what I have done wrong. I cannot seem to find an existing answer. Could anyone please suggest an adjustment to get this working?