説明なし

LZX-Compression-Automated.ps1 5.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. # Prompt the user to enter the type of drive they are planning to use this on
  2. $driveType = Read-Host "Please enter the type of drive you are planning to use this on (SSD or HDD)"
  3. # Validate the drive type input
  4. if ($driveType -eq "SSD" -or $driveType -eq "HDD") {
  5. # Proceed with the script
  6. } else {
  7. # Display an error message and exit the script
  8. Write-Error "Invalid drive type. Please enter either SSD or HDD."
  9. Exit
  10. }
  11. # If the drive type is SSD, inform the user of the pros of compression
  12. if ($driveType -eq "SSD") {
  13. Write-Host "Compressing files and folders on a solid state drive (SSD) can have some benefits, such as:"
  14. Write-Host "- You can save some disk space by compressing files and folders that are not already compressed or optimized. This can help you store more data on your SSD or free up some space for other purposes."
  15. Write-Host "- You can improve the performance of your SSD by reducing the amount of data that needs to be read and written. This can lower the wear and tear of your SSD and extend its lifespan. It can also reduce the power consumption and heat generation of your SSD, which can benefit your battery life and system stability."
  16. Write-Host "- You can enhance the security of your data by making it harder for unauthorized users or programs to access or modify your files. Compression can also help you recover some data in case of corruption or deletion, as it may preserve some parts of the original file."
  17. Write-Host "However, there are also some cons of using this script on an SSD, such as:"
  18. Write-Host "- You may experience some CPU overhead when compressing and decompressing files, which can affect your system performance and responsiveness. This may not be noticeable on modern CPUs, but it can still have some impact on your multitasking and gaming experience."
  19. Write-Host "- You may encounter some compatibility issues with some applications or games that do not support compressed files or folders. This can cause errors, crashes, or performance degradation. You may need to decompress some files or folders before using them with certain programs."
  20. Write-Host "- You may lose some data quality or functionality when compressing some types of files, such as audio, video, or document files. Compression can reduce the resolution, bitrate, or features of these files, which can affect their appearance, sound, or behavior."
  21. Write-Host "Therefore, you should weigh the pros and cons of using this script on an SSD before deciding to do so. You should also backup your important data before running the script, in case something goes wrong. You can read more about how compression works on SSDs in [this article](^3^) or this question."
  22. }
  23. # If the drive type is HDD, inform the user of the benefits of compression
  24. if ($driveType -eq "HDD") {
  25. Write-Host "Compressing files and folders on a hard disk drive (HDD) can improve performance by reducing disk space usage, increasing data transfer speed, and reducing seek time. However, compression also increases CPU usage and may affect some applications or games. You can read more about the pros and cons of compression on HDDs in [this article](^2^)."
  26. }
  27. # Prompt the user to enter a drive letter for compression
  28. $drive = Read-Host "Please enter a drive letter for compression (e.g. C, D, E)"
  29. # Validate the drive letter input
  30. if ($drive -match "^[A-Z]$") {
  31. # Append a colon to the drive letter
  32. $drive = $drive + ":"
  33. } else {
  34. # Display an error message and exit the script
  35. Write-Error "Invalid drive letter. Please enter a single uppercase letter from A to Z."
  36. Exit
  37. }
  38. # Define the file types to exclude from compression
  39. $exclude = @("*.zip", "*.mp4", "*.mp3", "*.flv", "*.jpg", "*.png", "*.gif", "*.pdf", "*.rar", "*.7z", "*.tar", "*.gz", "*.wav", "*.aac", "*.ogg", "*.mkv", "*.avi", "*.docx", "*.xlsx", "*.pptx", "*.ttf", "*.otf", "*.woff")
  40. # Get all the files and folders on the drive
  41. $items = Get-ChildItem -Path $drive -Recurse
  42. # Loop through each item and check if it is compressible
  43. foreach ($item in $items) {
  44. # Skip the item if it is a directory or a symbolic link
  45. if ($item.PSIsContainer -or $item.LinkType) {
  46. continue
  47. }
  48. # Skip the item if it matches any of the excluded file types
  49. $skip = $false
  50. foreach ($ext in $exclude) {
  51. if ($item.Name -like $ext) {
  52. $skip = $true
  53. break
  54. }
  55. }
  56. if ($skip) {
  57. continue
  58. }
  59. # Compress the item using compact.exe with the /C, /I, /EXE:lzx and /EXCLUDE:exclude.txt options
  60. compact /C /I /EXE:lzx /EXCLUDE:exclude.txt $item.FullName
  61. }
  62. # Prompt the user if they wish to undo compression in case of compatibility reasons
  63. $undo = Read-Host "Do you wish to undo compression in case of compatibility reasons? (Y/N)"
  64. # Validate the undo input
  65. if ($undo -eq "Y" -or $undo -eq "N") {
  66. # Proceed with the script
  67. } else {
  68. # Display an error message and exit the script
  69. Write-Error "Invalid input. Please enter either Y or N."
  70. Exit
  71. }
  72. # If the user wishes to undo compression, run the command to uncompress the data
  73. if ($undo -eq "Y") {
  74. Write-Host "Uncompressing the data. This may take some time."
  75. compact.exe /CompactOS:never
  76. Write-Host "The data has been uncompressed. You may need to restart your system for the changes to take effect."
  77. }
  78. # If the user does not wish to undo compression, display a message and exit the script
  79. if ($undo -eq "N") {
  80. Write-Host "The data has been compressed. You may need to restart your system for the changes to take effect."
  81. }

Powered by TurnKey Linux.