# Define the source directory $sourceDirectory = ".\AVP" # Check if the parent directory is named "Projets" $parentDirectory = Split-Path -Path $sourceDirectory -Parent if ((Split-Path -Path $parentDirectory -Leaf) -eq "Projets") { # Prompt the user for the project name $projectName = Read-Host "Enter the project name" # Get the current year and month $currentYear = (Get-Date).Year $currentMonth = (Get-Date).Month.ToString("D2") # Get all folders starting with the current year and month in the source directory $pattern = "$currentYear $currentMonth*" $folders = Get-ChildItem -Path $sourceDirectory -Directory | Where-Object { $_.Name -like $pattern } # Find the folder with the largest number after the year and month if ($folders.Count -eq 0) { $newNumber = 1 } else { $latestFolder = $folders | Sort-Object -Property { [int]($_.Name -replace "[^\d]") } | Select-Object -Last 1 $latestNumber = [int]($latestFolder.Name -replace "[^\d]") $newNumber = $latestNumber + 1 } # Create the new folder name $newFolderName = "$currentYear $currentMonth $newNumber $projectName" # 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 current script to the new folder $currentScriptPath = $MyInvocation.MyCommand.Path Copy-Item -Path $currentScriptPath -Destination $newFolderPath -ErrorAction SilentlyContinue # Create "AVP" and "Donnees" folders inside the new folder New-Item -ItemType Directory -Path (Join-Path -Path $newFolderPath -ChildPath "AVP") -ErrorAction SilentlyContinue New-Item -ItemType Directory -Path (Join-Path -Path $newFolderPath -ChildPath "Donnees") -ErrorAction SilentlyContinue } else { # Existing logic for creating AVP folders Write-Host "Getting folders in $sourceDirectory" $folders = Get-ChildItem -Path $sourceDirectory -Directory | Where-Object { $_.Name -like "AVP*" } 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 } Write-Host "Finding the latest folder" $latestFolder = $folders | Sort-Object -Property { [int]($_.Name -replace "[^\d]") } | Select-Object -Last 1 $latestNumber = [int]($latestFolder.Name -replace "[^\d]") $newNumber = $latestNumber + 1 $newFolderName = "AVP" + $newNumber.ToString().PadLeft(2, '0') Write-Host "`nFolders found:" $folders | ForEach-Object { Write-Host $_.Name } Write-Host "`nWould you like to create the next AVP "$newFolderName" ? Press any key to continue or CTRL+C to exit..." $null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") $newFolderPath = Join-Path -Path $sourceDirectory -ChildPath $newFolderName Write-Host "Creating new folder: $newFolderName" New-Item -ItemType Directory -Path $newFolderPath -ErrorAction SilentlyContinue }