File and Folder Manipulation Toolkit
<# .SYNOPSIS File and Folder Manipulation Toolkit .DESCRIPTION This script provides a set of tools for manipulating and analyzing files and folders, including operations like copying, moving, deleting, renaming, and analyzing. .NOTES File Name : FileAndFolderToolkit.ps1 Author : [Your Name] Prerequisite : PowerShell V5.1 or later, appropriate permissions Version : 1.0 Date : [Current Date] .EXAMPLE .\FileAndFolderToolkit.ps1 #> # Global variables $global:sourcePath = "" $global:destinationPath = "" $global:reportPath = "$env:USERPROFILE\Desktop\File_Folder_Analysis_Report_$(Get-Date -Format 'yyyyMMdd_HHmmss').html" function Show-Menu { Clear-Host Write-Host "=== File and Folder Manipulation Toolkit ===" -ForegroundColor Cyan Write-Host "Current Source Path: $global:sourcePath" Write-Host "Current Destination Path: $global:destinationPath" Write-Host "1. Set Source Path" Write-Host "2. Set Destination Path" Write-Host "3. Copy Files/Folders" Write-Host "4. Move Files/Folders" Write-Host "5. Delete Files/Folders" Write-Host "6. Rename Files/Folders" Write-Host "7. Analyze Folder Structure" Write-Host "8. Search for Files" Write-Host "9. Compare Folders" Write-Host "10. Generate File Hash" Write-Host "11. Generate HTML Report" Write-Host "12. Exit" } function Set-SourcePath { $path = Read-Host "Enter the source path" if (Test-Path -Path $path) { $global:sourcePath = $path Write-Host "Source path set to: $global:sourcePath" -ForegroundColor Green } else { Write-Host "Invalid path. Please try again." -ForegroundColor Red } } function Set-DestinationPath { $path = Read-Host "Enter the destination path" if (Test-Path -Path $path) { $global:destinationPath = $path Write-Host "Destination path set to: $global:destinationPath" -ForegroundColor Green } else { Write-Host "Invalid path. Please try again." -ForegroundColor Red } } function Copy-FilesAndFolders { if ([string]::IsNullOrEmpty($global:sourcePath) -or [string]::IsNullOrEmpty($global:destinationPath)) { Write-Host "Please set both source and destination paths first." -ForegroundColor Red return } Write-Host "`nCopying Files and Folders..." -ForegroundColor Yellow try { Copy-Item -Path $global:sourcePath -Destination $global:destinationPath -Recurse -Force Write-Host "Copy operation completed successfully." -ForegroundColor Green } catch { Write-Host "Error during copy operation: $_" -ForegroundColor Red } } function Move-FilesAndFolders { if ([string]::IsNullOrEmpty($global:sourcePath) -or [string]::IsNullOrEmpty($global:destinationPath)) { Write-Host "Please set both source and destination paths first." -ForegroundColor Red return } Write-Host "`nMoving Files and Folders..." -ForegroundColor Yellow try { Move-Item -Path $global:sourcePath -Destination $global:destinationPath -Force Write-Host "Move operation completed successfully." -ForegroundColor Green } catch { Write-Host "Error during move operation: $_" -ForegroundColor Red } } function Delete-FilesAndFolders { if ([string]::IsNullOrEmpty($global:sourcePath)) { Write-Host "Please set the source path first." -ForegroundColor Red return } $confirmation = Read-Host "Are you sure you want to delete the contents of $global:sourcePath? (Y/N)" if ($confirmation -eq "Y") { Write-Host "`nDeleting Files and Folders..." -ForegroundColor Yellow try { Remove-Item -Path $global:sourcePath -Recurse -Force Write-Host "Delete operation completed successfully." -ForegroundColor Green } catch { Write-Host "Error during delete operation: $_" -ForegroundColor Red } } else { Write-Host "Delete operation cancelled." -ForegroundColor Yellow } } function Rename-FilesAndFolders { if ([string]::IsNullOrEmpty($global:sourcePath)) { Write-Host "Please set the source path first." -ForegroundColor Red return } $newName = Read-Host "Enter the new name" Write-Host "`nRenaming File/Folder..." -ForegroundColor Yellow try { Rename-Item -Path $global:sourcePath -NewName $newName Write-Host "Rename operation completed successfully." -ForegroundColor Green $global:sourcePath = Join-Path (Split-Path $global:sourcePath -Parent) $newName } catch { Write-Host "Error during rename operation: $_" -ForegroundColor Red } } function Analyze-FolderStructure { if ([string]::IsNullOrEmpty($global:sourcePath)) { Write-Host "Please set the source path first." -ForegroundColor Red return } Write-Host "`nAnalyzing Folder Structure..." -ForegroundColor Yellow try { $analysis = Get-ChildItem -Path $global:sourcePath -Recurse | Measure-Object -Property Length -Sum $result = [PSCustomObject]@{ TotalFiles = $analysis.Count TotalSize = "{0:N2} MB" -f ($analysis.Sum / 1MB) Directories = (Get-ChildItem -Path $global:sourcePath -Directory -Recurse).Count } $result | Format-List return $result } catch { Write-Host "Error during folder analysis: $_" -ForegroundColor Red return $null } } function Search-Files { if ([string]::IsNullOrEmpty($global:sourcePath)) { Write-Host "Please set the source path first." -ForegroundColor Red return } $searchPattern = Read-Host "Enter the search pattern (e.g., *.txt)" Write-Host "`nSearching for Files..." -ForegroundColor Yellow try { $results = Get-ChildItem -Path $global:sourcePath -Recurse -Filter $searchPattern $results | Format-Table Name, LastWriteTime, Length -AutoSize return $results } catch { Write-Host "Error during file search: $_" -ForegroundColor Red return $null } } function Compare-Folders { if ([string]::IsNullOrEmpty($global:sourcePath) -or [string]::IsNullOrEmpty($global:destinationPath)) { Write-Host "Please set both source and destination paths first." -ForegroundColor Red return } Write-Host "`nComparing Folders..." -ForegroundColor Yellow try { $comparison = Compare-Object -ReferenceObject (Get-ChildItem -Path $global:sourcePath -Recurse) -DifferenceObject (Get-ChildItem -Path $global:destinationPath -Recurse) -Property Name, Length, LastWriteTime if ($comparison) { $comparison | Format-Table Name, Length, LastWriteTime, @{Label="Status"; Expression={if ($_.SideIndicator -eq "<=") {"Only in Source"} else {"Only in Destination"}}} -AutoSize } else { Write-Host "The folders are identical." -ForegroundColor Green } return $comparison } catch { Write-Host "Error during folder comparison: $_" -ForegroundColor Red return $null } } function Generate-FileHash { if ([string]::IsNullOrEmpty($global:sourcePath)) { Write-Host "Please set the source path first." -ForegroundColor Red return } Write-Host "`nGenerating File Hash..." -ForegroundColor Yellow try { $hash = Get-FileHash -Path $global:sourcePath -Algorithm SHA256 $hash | Format-List return $hash } catch { Write-Host "Error generating file hash: $_" -ForegroundColor Red return $null } } function Generate-HTMLReport { param([hashtable]$AllResults) Write-Host "`nGenerating HTML Report..." -ForegroundColor Yellow $reportContent = @" <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>File and Folder Analysis Report</title> <style> body { font-family: Arial, sans-serif; line-height: 1.6; color: #333; max-width: 1200px; margin: 0 auto; padding: 20px; } h1, h2, h3 { color: #0078D4; } table { border-collapse: collapse; width: 100%; margin-bottom: 20px; } th, td { border: 1px solid #ddd; padding: 8px; text-align: left; } th { background-color: #f2f2f2; } </style> </head> <body> <h1>File and Folder Analysis Report</h1> <p>Generated on: $(Get-Date)</p> <p>Source Path: $global:sourcePath</p> <p>Destination Path: $global:destinationPath</p> <h2>Folder Structure Analysis</h2> $($AllResults.FolderAnalysis | ConvertTo-Html -Fragment) <h2>File Search Results</h2> $($AllResults.FileSearch | ConvertTo-Html -Fragment) <h2>Folder Comparison</h2> $($AllResults.FolderComparison | ConvertTo-Html -Fragment) <h2>File Hash</h2> $($AllResults.FileHash | ConvertTo-Html -Fragment) </body> </html> "@ $reportContent | Out-File -FilePath $global:reportPath Write-Host "Report generated and saved to: $global:reportPath" -ForegroundColor Green } # Main program loop $allResults = @{} do { Show-Menu $choice = Read-Host "`nEnter your choice (1-12)" switch ($choice) { "1" { Set-SourcePath } "2" { Set-DestinationPath } "3" { Copy-FilesAndFolders } "4" { Move-FilesAndFolders } "5" { Delete-FilesAndFolders } "6" { Rename-FilesAndFolders } "7" { $allResults.FolderAnalysis = Analyze-FolderStructure } "8" { $allResults.FileSearch = Search-Files } "9" { $allResults.FolderComparison = Compare-Folders } "10" { $allResults.FileHash = Generate-FileHash } "11" { Generate-HTMLReport -AllResults $allResults } "12" { Write-Host "Exiting program..." -ForegroundColor Yellow; break } default { Write-Host "Invalid choice. Please try again." -ForegroundColor Red } } if ($choice -ne "12") { Read-Host "`nPress Enter to continue..." } } while ($choice -ne "12")
This File and Folder Manipulation Toolkit includes:
- A menu-driven interface for easy navigation.
- Functions to perform various file and folder operations:
- Setting source and destination paths
- Copying files and folders
- Moving files and folders
- Deleting files and folders
- Renaming files and folders
- Analyzing folder structure
- Searching for files
- Comparing folders
- Generating file hashes
- HTML report generation for easy sharing and viewing of results.
Key features:
- Flexible path setting for source and destination
- Basic file operations (copy, move, delete, rename)
- Folder structure analysis with file count and total size
- File search functionality with wildcard support
- Folder comparison to identify differences
- File hash generation for integrity checking
- Comprehensive HTML report generation
This tool is particularly useful for:
- System administrators managing file systems
- IT professionals performing file and folder operations in bulk
- Anyone needing to analyze, compare, or manipulate file structures
To use this script effectively:
- Run PowerShell with appropriate permissions to access and modify the file system
- Set the source and destination paths before performing operations
- Use caution with delete operations, as they are irreversible
- Review the generated HTML report for a comprehensive overview of the analysis results
This script provides a versatile set of tools for file and folder manipulation and analysis, helping users to efficiently manage and understand their file systems. It’s designed to be user-friendly while offering powerful functionality for both simple and complex file operations.
Leave a Reply
Want to join the discussion?Feel free to contribute!