Refactor folder creation logic to add leading zeros to AVP folder numbers

This commit is contained in:
gribse 2024-10-31 00:56:34 +01:00
parent e19076efeb
commit 8f35f927c1
2 changed files with 1 additions and 71 deletions

View file

@ -103,6 +103,7 @@ def createNewAVP():
pause_debug("Finding the latest folder")
latest_number = max(int(f[3:]) for f in folders)
new_number = latest_number + 1
new_number = str(new_number).zfill(2)
new_folder_name = f"AVP{str(new_number).zfill(2)}"
pause_debug(f"New folder number determined: {new_number}")

View file

@ -1,71 +0,0 @@
# 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
}