Advertisement
Python253

win_env_var_explorer2

Apr 19th, 2024
823
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.80 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # Filename: win_env_var_explorer2.py
  4. # Version: 1.0.1
  5. # Author: Jeoi Reqi
  6.  
  7. """
  8. This script allows users to explore and display Windows environment variables grouped by categories.
  9. Users can navigate through a menu to select a category of environment variables and then choose a specific variable
  10. within that category to display its current value. The script provides a user-friendly interface for interacting with
  11. environment variables, making it easier for users to manage and understand their Windows environment configuration.
  12.  
  13. Additionally, users can select all categories to display and save the output data from all variables within each category to a text file.
  14.  
  15. Requirements:
  16. - Python 3
  17. - Windows operating system
  18.  
  19. Usage:
  20. - Run the script using Python 3 in a Windows environment.
  21.  
  22. Additional Notes:
  23. - Ensure that Python 3 is installed on your system.
  24. - This script provides read-only access to environment variables and does not modify them.
  25. - Output data is saved to 'environment_output.txt' in the current working directory.
  26. """
  27.  
  28. import os
  29.  
  30. # Dictionary of environment variables by category
  31. variables = {
  32.     "System Information": {
  33.         1: "ALLUSERSPROFILE",
  34.         2: "COMPUTERNAME",
  35.         3: "NUMBER_OF_PROCESSORS",
  36.         4: "OS",
  37.         5: "PROCESSOR_ARCHITECTURE",
  38.         6: "SYSTEMDRIVE",
  39.         7: "SYSTEMROOT",
  40.         8: "WINDIR"
  41.     },
  42.     "User Information": {
  43.         1: "APPDATA",
  44.         2: "HOMEDRIVE",
  45.         3: "HOMEPATH",
  46.         4: "LOCALAPPDATA",
  47.         5: "USERDOMAIN",
  48.         6: "USERNAME",
  49.         7: "USERPROFILE"
  50.     },
  51.     "File Paths": {
  52.         1: "CD",
  53.         2: "CommonProgramFiles",
  54.         3: "COMMONPROGRAMFILES(x86)",
  55.         4: "ProgramData",
  56.         5: "ProgramFiles",
  57.         6: "ProgramFiles(x86)",
  58.         7: "ProgramW6432",
  59.         8: "PSModulePath",
  60.         9: "Public",
  61.         10: "TEMP",
  62.         11: "TMP"
  63.     },
  64.     "Network": {
  65.         1: "ClientName",
  66.         2: "LOGONSERVER",
  67.         3: "HOMESHARE",
  68.         4: "OneDrive",
  69.         5: "OneDriveCommercial",
  70.         6: "OneDriveConsumer",
  71.         7: "UserDnsDomain",
  72.         8: "%SessionName%"
  73.     },
  74.     "Command Interpreter": {
  75.         1: "COMSPEC",
  76.         2: "CMDEXTVERSION",
  77.         3: "CMDCMDLINE",
  78.         4: "PROMPT"
  79.     },
  80.     "Date and Time": {
  81.         1: "DATE",
  82.         2: "TIME"
  83.     },
  84.     "Miscellaneous": {
  85.         1: "ERRORLEVEL",
  86.         2: "PATHEXT",
  87.         3: "RANDOM"
  88.     }
  89. }
  90.  
  91. # Function to save environment output to a text file
  92. def save_environment_output(output_data):
  93.     with open("environment_output.txt", "w") as f:
  94.         for category, data in output_data.items():
  95.             f.write(f"{category}:\n")
  96.             for variable, value in data.items():
  97.                 f.write(f"{variable}: {value}\n")
  98.             f.write("\n")
  99.  
  100. # Main loop
  101. while True:
  102.     # Display categories menu
  103.     print("\nChoose a category of environment variables (type '0' to exit):\n")
  104.     for num, category in enumerate(variables.keys(), 1):
  105.         print(f"{num:02}. {category}")
  106.     print(f"{len(variables) + 1:02}. Select all & save the output to file.")
  107.  
  108.     # Get user input for category choice
  109.     category_choice = input("\nEnter the number of the category you want to explore: ")
  110.  
  111.     # Exit if category choice is '0'
  112.     if category_choice == '0':
  113.         print("\nExiting...\tGoodBye!\n")
  114.         break
  115.  
  116.     # Validate category choice
  117.     if category_choice.isdigit() and 1 <= int(category_choice) <= len(variables) + 1:
  118.         category_choice = int(category_choice)
  119.        
  120.         # Initialize output data dictionary
  121.         output_data = {}
  122.        
  123.         if category_choice == len(variables) + 1:
  124.             # Select all categories
  125.             selected_categories = list(variables.keys())
  126.         else:
  127.             # Select single category
  128.             category_name = list(variables.keys())[category_choice - 1]
  129.             selected_categories = [category_name]
  130.  
  131.         # Iterate over selected categories
  132.         for category_name in selected_categories:
  133.             # Display variables submenu for selected category
  134.             print(f"\n{category_name} variables:\n")
  135.             category_variables = variables[category_name]
  136.             category_output = {}
  137.             for num, var in sorted(category_variables.items()):
  138.                 value = os.environ.get(var)
  139.                 category_output[var] = value
  140.                 print(f"{num:02}. {var}: {value}")
  141.            
  142.             output_data[category_name] = category_output
  143.        
  144.         # Save environment output to text file
  145.         save_environment_output(output_data)
  146.         print("\nEnvironment output saved to 'environment_output.txt'.")
  147.     else:
  148.         print("\nInvalid category choice!\n")
  149.  
  150.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement