make a powershell script that will remove all "bin" and "obj" folders in main folder and…
- User
make a powershell script that will remove all "bin" and "obj" folders in main folder and subfolders, script will collect path's,
- Assistant
Here's a PowerShell script that finds and removes all "bin" and "obj" folders in the main folder and its subfolders, while collecting and displaying the paths of the deleted folders:
# Script to remove bin and obj folders and collect their paths # Get the current directory (where the script is run from) $rootPath = Get-Location # Array to store paths of deleted folders $deletedFolders = @() # Function to remove folders and collect paths function Remove-BuildFolders { param ( [string]$path ) try { # Get all bin and obj folders $folders = Get-ChildItem -Path $path -Directory -Recurse -Include "bin", "obj" -ErrorAction Stop foreach ($folder in $folders) { try { # Add folder path to the collection $deletedFolders += $folder.FullName # Remove the folder and its contents Remove-Item -Path $folder.FullName -Recurse -Force -ErrorAction Stop Write-Host "Successfully removed: $($folder.FullName)" -ForegroundColor Green } catch { Write-Host "Error removing $($folder.FullName): $_" -ForegroundColor Red } } } catch { Write-Host "Error accessing folders in ${path}: $_" -ForegroundColor Red } } # Start the removal process Write-Host "Starting cleanup of bin and obj folders in: $rootPath" -ForegroundColor Cyan Write-Host "----------------------------------------" -ForegroundColor Cyan Remove-BuildFolders -path $rootPath # Display summary Write-Host "----------------------------------------" -ForegroundColor Cyan Write-Host "Cleanup Summary:" -ForegroundColor Cyan if ($deletedFolders.Count -gt 0) { Write-Host "Total folders removed: $($deletedFolders.Count)" -ForegroundColor Yellow Write-Host "Removed folders:" -ForegroundColor Yellow # Display all deleted folder paths $deletedFolders | ForEach-Object { Write-Host $_ -ForegroundColor Gray } # Optionally, save the list of deleted folders to a log file $logFile = Join-Path $rootPath "cleanup_log_$(Get-Date -Format 'yyyyMMdd_HHmmss').txt" $deletedFolders | Out-File -FilePath $logFile Write-Host "List of removed folders has been saved to: $logFile" -ForegroundColor Cyan } else { Write-Host "No bin or obj folders were found." -ForegroundColor Yellow } Write-Host "Cleanup process completed." -ForegroundColor CyanFeatures of this script:
-
Path Collection: The script collects the full paths of all "bin" and "obj" folders it removes and stores them in an array (
$deletedFolders). -
Error Handling: The script includes error handling to catch and report issues during folder access or deletion.
-
Color-Coded Output: The script uses color-coded console output for better readability:
- Cyan for general information
- Green for successful deletions
- Red for errors
- Yellow for summaries
- Gray for listing deleted folder paths
-
Logging: The script optionally saves the list of deleted folder paths to a log file in the root directory, with a timestamp in the filename.
-
Recursive Search: The script searches for "bin" and "obj" folders in the main folder and all subfolders.
-
Summary: At the end, the script provides a summary of the total number of folders removed and lists their paths.
How to Use:
- Save the script to a
.ps1file (e.g.,CleanBuildFolders.ps1). - Open PowerShell and navigate to the directory where you want to run the cleanup.
- Run the script:
.\CleanBuildFolders.ps1
Example Output:
Starting cleanup of bin and obj folders in: C:\Projects ---------------------------------------- Successfully removed: C:\Projects\Project1\bin Successfully removed: C:\Projects\Project1\obj Successfully removed: C:\Projects\Project2\bin Successfully removed: C:\Projects\Project2\obj ---------------------------------------- Cleanup Summary: Total folders removed: 4 Removed folders: C:\Projects\Project1\bin C:\Projects\Project1\obj C:\Projects\Project2\bin C:\Projects\Project2\obj List of removed folders has been saved to: C:\Projects\cleanup_log_20231015_143022.txt Cleanup process completed.Notes:
- Permissions: Ensure you have sufficient permissions to delete folders in the target directory.
- Safety: Be cautious when running scripts that delete files/folders. Test the script in a safe environment first.
- Customization: You can modify the script to include additional folder names or change the logging behavior as needed.
Let me know if you need any modifications or have questions!
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.