I need to move files, which satisfy specific conditions, from folder A to a specific subfolder in folder B.
The conditions are:
Group the files that contain .exe and take the two with the highest number.
After the number take the string between the first two hyphen characters (-). The custom-string
Move these files in the folder B\win(custom-string), if win folder does not exist create it, the same goes for the custom-string folder.
So for example in the image below we would take the files CICone NT Setup 0.25.5-develop-build.0.exe
and CICone NT Setup 0.25.5-develop-build.0.exe.blockmap
and move them to a folder B\win\develop\
, here the develop is the name of the folder (the string between the two first hyphen characters).
Here is the solution:
$winFiles = get-childitem | Where-Object {$_.Name -like "*.exe*"} | Sort-Object -Descending -Property Name | Select-Object -First 2
ForEach ($file in $winFiles){
$EnvironmentSubstring = $file.Name.Split('-')[1]
if(!(Test-Path ..\B\win)){
New-Item -Path ..\B\win -ItemType Directory -Force | Out-Null
if(!(Test-Path ..\B\win\$EnvironmentSubstring)){
New-Item -Path ..\B\win\$EnvironmentSubstring -ItemType Directory -Force | Out-Null
}
}
Move-Item -Path $file.Name -Destination ..\B\win\$EnvironmentSubstring\ -Force
}