# Define the source directory $sourceDirectory = ".\AVP" # Get all folders starting with "AVP" in the source directory Write-Host "Getting folders in $sourceDirectory" $folders = Get-ChildItem -Path $sourceDirectory -Directory | Where-Object { $_.Name -like "AVP*" } # If no folders are found, create the first folder if ($folders.Count -eq 0) { Write-Host "No folders found. Creating first folder: AVP01" New-Item -ItemType Directory -Path (Join-Path -Path $sourceDirectory -ChildPath "AVP01") -ErrorAction SilentlyContinue return } # Find the folder with the largest number after "AVP" Write-Host "Finding the latest folder" $latestFolder = $folders | Sort-Object -Property { [int]($_.Name -replace "[^\d]") } | Select-Object -Last 1 # Increment the number in the folder name $latestNumber = [int]($latestFolder.Name -replace "[^\d]") $newNumber = $latestNumber + 1 $newFolderName = "AVP" + $newNumber.ToString().PadLeft(2, '0') # List to the console the folders found, ask before creating the new one "AVPXX" Write-Host "`nFolders found:" $folders | ForEach-Object { Write-Host $_.Name } Write-Host "`nWould you like to create the nex AVP "$newFolderName" ? Press any key to continue or CTRL+C to exit..." $null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") # Create the new folder $newFolderPath = Join-Path -Path $sourceDirectory -ChildPath $newFolderName Write-Host "Creating new folder: $newFolderName" New-Item -ItemType Directory -Path $newFolderPath -ErrorAction SilentlyContinue # Copy the contents of the latest folder to the new folder $latestFolderPath = Join-Path -Path $sourceDirectory -ChildPath $latestFolder.Name $filesToCopy = Get-ChildItem -Path $latestFolderPath -File | Where-Object { $_.Extension -ne ".stl" } foreach ($file in $filesToCopy) { $newFileName = $file.Name -replace "AVP\d{2}", $newFolderName $newFilePath = Join-Path -Path $newFolderPath -ChildPath $newFileName Write-Host "Copying file: $($file.Name)" Copy-Item -Path $file.FullName -Destination $newFilePath } # Pause at the end $filesCopied = Get-ChildItem -Path $newFolderPath -File $totalSize = 0 foreach ($file in $filesCopied) { $totalSize += $file.Length } $sizeInMB = [Math]::Round($totalSize / 1MB, 1) Write-Host "`nAll files copied for a total of $sizeInMB MB.`nPress any key to exit..." $null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")