Last active
March 12, 2024 20:40
-
-
Save Dalboz/797262537f66643ba24b55e99045469e to your computer and use it in GitHub Desktop.
win folder utils
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Function to show a folder selection dialog | |
function Get-FolderDialog($initialDirectory) { | |
$folderBrowser = New-Object System.Windows.Forms.FolderBrowserDialog | |
$folderBrowser.RootFolder = [System.Environment+SpecialFolder]::MyComputer | |
$folderBrowser.SelectedPath = $initialDirectory | |
$folderBrowser.ShowNewFolderButton = $false | |
$result = $folderBrowser.ShowDialog() | |
if ($result -eq [System.Windows.Forms.DialogResult]::OK) { | |
return $folderBrowser.SelectedPath | |
} else { | |
return $null | |
} | |
} | |
# Get source directory using folder selection dialog | |
$sourceDirectory = Get-FolderDialog -initialDirectory "C:\Path\To\Source" | |
if (-not $sourceDirectory) { | |
Write-Host "Source directory selection canceled." | |
exit | |
} | |
# Get destination directory using folder selection dialog | |
$destinationDirectory = Get-FolderDialog -initialDirectory "D:\Path\To\Destination" | |
if (-not $destinationDirectory) { | |
Write-Host "Destination directory selection canceled." | |
exit | |
} | |
# Continue with the rest of the script | |
# Check if source directory exists | |
if (-not (Test-Path $sourceDirectory -PathType Container)) { | |
Write-Host "Source directory does not exist." | |
exit | |
} | |
# Create destination directory if it doesn't exist | |
if (-not (Test-Path $destinationDirectory -PathType Container)) { | |
New-Item -ItemType Directory -Path $destinationDirectory | Out-Null | |
} | |
# Get all subdirectories in the source directory | |
$subDirectories = Get-ChildItem -Path $sourceDirectory -Directory -Recurse | |
# Copy the folder structure to the destination directory | |
foreach ($subDirectory in $subDirectories) { | |
$targetPath = $subDirectory.FullName -replace [regex]::Escape($sourceDirectory), $destinationDirectory | |
New-Item -ItemType Directory -Path $targetPath -Force | Out-Null | |
} | |
Write-Host "Folder structure copied from $sourceDirectory to $destinationDirectory." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment