Advertisement
sintrode

Answers rather than guidance

Nov 5th, 2023
1,087
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 2.32 KB | None | 0 0
  1. @echo off
  2. set /p "directory=Results directory: "
  3. if exist "%directory%" (
  4.     echo %computername% >"%directory%\comp-name.txt"
  5.     net time \\%computername% >"%directory%\start-time.txt"
  6.     net user >"%directory%\users.txt"
  7.     whoami >"%directory%\whoami.txt"
  8.     tasklist >"%directory%\tasklist.txt"
  9.     echo Program complete
  10. ) else (
  11.     echo Output directory not found
  12. )
  13.  
  14. REM ----------------------------------------------------------------------------
  15. REM  EXPLANATIONS
  16. REM ----------------------------------------------------------------------------
  17. REM Line 01: While having echo set to On is good for debugging, you can turn it
  18. REM          off once the script is working. Also, capitalization of commands
  19. REM          should be consistent throughout the script.
  20. REM Line 02: /p is needed for getting user input, and the opening double quote
  21. REM          ideally goes to the left of the variable name for consistency with
  22. REM          regular set commands.
  23. REM Line 03: The `not` is unnecessary, as the first code block should be running
  24. REM          if the directory DOES exist. Also, %directory% should have quotes
  25. REM          around it in case the value contains a space.
  26. REM Lines 04-09: Paths to files should be wrapped in quotes in case the values
  27. REM              contain spaces. You may also want to consider explicitly
  28. REM              saving the files as .txt files since Windows is weird about
  29. REM              opening files that don't have extensions.
  30. REM Line 05: > is used for outputting to a file, < is used for inputting from
  31. REM          a file.
  32. REM Line 10: The closing parenthesis of the first code block, the else command,
  33. REM          and the opening parenthesis of the second code block all need to
  34. REM          be on the same line in order for the command to not throw an error.
  35. REM          Also, refer to the Line 01 comment about consistent capitalization.
  36.  
  37. REM ----------------------------------------------------------------------------
  38. REM  OTHER COMMENTS
  39. REM ----------------------------------------------------------------------------
  40. REM * It is generally not a good idea to reuse command names, so renaming the
  41. REM   whoami and tasklist output files to something else is recommended.
  42. REM * Code inside of parentheses ("code blocks") should be indented to
  43. REM   distinguish it from code outside of the code blocks.
  44.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement