Unknown model· unknown
from ShareGPT 90K (shared ChatGPT conversations)
Give me a powershell script which lists all directories in a menu where I can select one…
- User
Give me a powershell script which lists all directories in a menu where I can select one of them to create a symbolic link pointing from ./latest/ to the selected directory.
- Assistant
Sure, here's a PowerShell script that should do what you're asking for:
perl$dirs = Get-ChildItem -Directory | Select-Object -ExpandProperty FullName if ($dirs.Count -eq 0) { Write-Host "No directories found." return } Write-Host "Select a directory to link to:" for ($i = 0; $i -lt $dirs.Count; $i++) { Write-Host "$($i+1). $($dirs[$i])" } $selection = Read-Host "Enter selection number (1-$($dirs.Count))" if (-not ([int]::TryParse($selection, [ref]$null))) { Write-Host "Invalid selection." return } $selection = [int]$selection - 1 if ($selection -lt 0 -or $selection -ge $dirs.Count) { Write-Host "Invalid selection." return } $selectedDir = $dirs[$selection] $linkPath = Join-Path -Path $PWD -ChildPath "latest" if (-not (Test-Path $linkPath)) { New-Item -ItemType SymbolicLink -Path $linkPath -Target $selectedDir Write-Host "Created new symbolic link: $($linkPath) -> $($selectedDir)" } else { $existingLink = Get-Item $linkPath if ($existingLink.Target -eq $selectedDir) { Write-Host "Symbolic link already exists with selected directory as target." } else { Set-ItemProperty $linkPath -Name Target -Value $selectedDir Write-Host "Updated symbolic link: $($linkPath) -> $($selectedDir)" } }Here's what the script does:
- Gets a list of all directories in the current directory using
Get-ChildItem -Directory. - If no directories are found, the script exits with a message.
- Displays a numbered menu of the directories and prompts the user to select one.
- Validates the user's selection and sets the
$selectedDirvariable to the chosen directory. - Sets the
$linkPathvariable to the path of the symbolic link in the current directory.
- Gets a list of all directories in the current directory using
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.