This PowerShell script allows users to select a target directory either by entering a path directly or by navigating through subdirectories with a numbered menu. Once a directory is chosen, the script creates a series of sequentially numbered folders using a base name provided by the user.
Click to view script…
# Function: Recursively select a folder by listing subdirectories
function Select-Folder($startingPath) {
$currentPath = $startingPath
while ($true) {
Write-Host "`nCurrent Directory: $currentPath" -ForegroundColor Cyan
# Get subdirectories (if any)
$subDirs = Get-ChildItem -Path $currentPath -Directory -ErrorAction SilentlyContinue
if (!$subDirs -or $subDirs.Count -eq 0) {
Write-Host "No subdirectories found in $currentPath. Using this folder."
break
}
# List options: 0 to select current folder, then list each subfolder with a number
Write-Host "0: [Select this directory]"
$index = 1
foreach ($dir in $subDirs) {
Write-Host ("{0}: {1}" -f $index, $dir.Name)
$index++
}
$choice = Read-Host "Enter the number to navigate into a folder, or 0 to select the current directory"
if (-not [int]::TryParse($choice, [ref]$null)) {
Write-Host "Invalid input. Please enter a number."
continue
}
$choice = [int]$choice
if ($choice -eq 0) {
break
}
elseif ($choice -ge 1 -and $choice -le $subDirs.Count) {
$currentPath = $subDirs[$choice - 1].FullName
}
else {
Write-Host "Invalid selection. Please try again."
}
}
return $currentPath
}
# Prompt the user to choose the directory selection method
Write-Host "Select target directory method:" -ForegroundColor Green
Write-Host "1: Type the full directory path"
Write-Host "2: Navigate directories using a numbered list"
$method = Read-Host "Enter 1 or 2"
if ($method -eq "1") {
$targetDirectory = Read-Host "Enter the target directory path"
if (-not (Test-Path $targetDirectory)) {
Write-Host "Directory '$targetDirectory' does not exist. Exiting." -ForegroundColor Red
exit
}
}
elseif ($method -eq "2") {
# Optionally allow the user to set a starting directory; default is C:\
$startingPath = Read-Host "Enter starting directory path (press Enter for default C:\)"
if ([string]::IsNullOrWhiteSpace($startingPath)) {
$startingPath = "C:\"
}
if (-not (Test-Path $startingPath)) {
Write-Host "The starting directory '$startingPath' does not exist. Exiting." -ForegroundColor Red
exit
}
$targetDirectory = Select-Folder $startingPath
Write-Host "Selected Directory: $targetDirectory" -ForegroundColor Green
}
else {
Write-Host "Invalid selection. Exiting." -ForegroundColor Red
exit
}
# Prompt user for the base folder name
$baseName = Read-Host "Enter the base folder name for the folders to be created"
# Prompt user for the number of folders to create
$numFoldersInput = Read-Host "Enter the number of folders to create"
if (-not [int]::TryParse($numFoldersInput, [ref]$null)) {
Write-Host "The number of folders must be a valid integer. Exiting." -ForegroundColor Red
exit
}
$numFolders = [int]$numFoldersInput
# Create folders sequentially
for ($i = 1; $i -le $numFolders; $i++) {
# Construct folder name: "BaseName 1", "BaseName 2", etc.
$folderName = "{0} {1}" -f $baseName, $i
$folderPath = Join-Path $targetDirectory $folderName
if (-not (Test-Path $folderPath)) {
New-Item -Path $folderPath -ItemType Directory | Out-Null
Write-Host "Created folder: $folderPath" -ForegroundColor Yellow
}
else {
Write-Host "Folder already exists: $folderPath" -ForegroundColor DarkYellow
}
}