Advertisement
Jexal

27cf88a3-3f33-441d-8d2f-28faffde722f

Mar 6th, 2025
10
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.51 KB | None | 0 0
  1. ```
  2. import os
  3.  
  4.  
  5. def strip_quotes(path):
  6. return path.strip('"')
  7.  
  8.  
  9. def validate_paths(paths):
  10. valid_paths = []
  11. invalid_paths = []
  12.  
  13. for path in paths:
  14. path = strip_quotes(path)
  15. if os.path.exists(path):
  16. valid_paths.append(path)
  17. else:
  18. invalid_paths.append(path)
  19.  
  20. return valid_paths, invalid_paths
  21.  
  22.  
  23. def get_size(path):
  24. total_size = 0
  25. if os.path.isfile(path):
  26. total_size = os.path.getsize(path)
  27. else:
  28. for dirpath, dirnames, filenames in os.walk(path):
  29. for f in filenames:
  30. fp = os.path.join(dirpath, f)
  31. total_size += os.path.getsize(fp)
  32. return total_size
  33.  
  34.  
  35. def format_size(size):
  36. for unit in ['B', 'KB', 'MB', 'GB', 'TB']:
  37. if size < 1024:
  38. return f"{size:.2f} {unit}"
  39. size /= 1024
  40.  
  41.  
  42. def main(input_file):
  43. folder_sizes = {}
  44. file_sizes = {}
  45.  
  46. with open(input_file, 'r') as f:
  47. paths = f.read().splitlines()
  48.  
  49. valid_paths, invalid_paths = validate_paths(paths)
  50.  
  51. if invalid_paths:
  52. print("Invalid paths:")
  53. for path in invalid_paths:
  54. print(path)
  55.  
  56. total_size = 0
  57. total_folder_size = 0
  58.  
  59. if valid_paths:
  60. for path in valid_paths:
  61. size = get_size(path)
  62. total_size += size
  63. if os.path.isfile(path):
  64. file_sizes[path] = size
  65. elif os.path.isdir(path):
  66. folder_sizes[path] = size
  67. total_folder_size += size
  68.  
  69. if folder_sizes:
  70. if len(folder_sizes) == 1:
  71. folder, size = next(iter(folder_sizes.items()))
  72. print(f"Folder Size: {folder}: {format_size(size)}")
  73. else:
  74. print("Folder Sizes:")
  75. for folder, size in folder_sizes.items():
  76. print(f"{folder}: {format_size(size)}")
  77. print(f"Total Folder Size: {format_size(total_folder_size)}")
  78.  
  79. print() # This adds the new line separator for the 2 sections.
  80.  
  81. if file_sizes:
  82. if len(file_sizes) == 1:
  83. file, size = next(iter(file_sizes.items()))
  84. print(f"File Size: {file}: {format_size(size)}")
  85. else:
  86. print("File Sizes:")
  87. for file, size in file_sizes.items():
  88. print(f"{file}: {format_size(size)}")
  89.  
  90. if len(valid_paths) > 1:
  91. print(f"Total Size: {format_size(total_size)}")
  92. else:
  93. print("No valid paths found in the input file.")
  94.  
  95.  
  96. if __name__ == "__main__":
  97. input_file_path = r"C:\Scripts\Batch File Mover\Files to Be Moved.txt"
  98. main(input_file_path)
  99.  
  100. ```
  101. Implement these changes to this script:
  102. • Add a progress bar.
  103. • Add logging capabilities to log the script's activities, errors, and paths that were processed.
  104. • Use concurrent processing to handle large directories more efficiently by calculating sizes in parallel.
  105. • Use a configuration file to allow the user to choose whether the output is saved to a text file, CSV or JSON file in addition to printing it to the console.
  106. • Improve error handling for various scenarios such as inaccessible paths, permission issues, or broken symbolic links.
  107. • Allow users to set a size threshold using a configuration file to filter and display only items above or below a certain size.
  108. • Provide additional summary statistics like average file size, number of files, number of folders, etc.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement