Page 1 of 1

Problems working a script

Posted: 05 Nov 2023 07:27
by Tai
I'm tasked with making this script run flawlessly. Unfortunately, I'm a total newbie in this area. I'm supposed to find approximately 5 errors and display the correct answer:
The task is:

The following batch script contains (at least) five errors. List these errors and give a
brief explanation of how to fix them. Note that your answer should refer to the line
numbers below. The purpose of the script is to ask the user to input a directory
name, and then store some system information in that directory. The information
includes: the computer name; current date / time; the running processes; and the
current username.

The script is below, and my corrections would be: 1. Best practice in a script is to set @ECHO OFF
2. set /P directory="Results directory: "
3. No correction
4. echo %computername% > %directory%\hostname
5. net time \\%computername% < %directory%\time /t \date /t
6. net user > %directory%\query user
7. whoami > %directory%\whoami
8. tasklist > %directory%\tasklist
9. echo "Program complete"
10. ) ELSE
11. echo "Output directory not found"
12. )

1. @ECHO ON
2. set /X directory="Results directory: "
3. if not exist %directory% (
4. echo %computername% > %directory%\comp-name
5. net time \\%computername% < %directory%\start-time
6. net user > %directory%\users
7. whoami > %directory%\whoami
8. tasklist > %directory%\tasklist
9. echo Program complete
10. ) ELSE
11. echo Output directory not found
12. )

Its probably fairly easy, but I just cant crack it. Hope someone has the time to help me along, since I've been spending a lot of time on it.

Thank You, Tai

Re: Problems working a script

Posted: 05 Nov 2023 18:11
by ShadowThief
The fact that you're saying that you haven't gotten it suggests to me that you have some way of validating the solution, and so this feels a bit like homework. Depending on your teacher's standards and whether or not you just didn't format the code when you pasted it in here, I could argue that every single line other than 12 has something that violates best practices, with lines 2 and 4-8 containing multiple errors. (Of course, if lines 4-9 and 11 are indented in your actual code, ignore this.)

The main thing that I would look at is to consider what would happen if %directory% contains a space. Also, check the logic on line 3 and compare it to what the script is actually doing.

Re: Problems working a script

Posted: 05 Nov 2023 18:21
by ShadowThief
https://pastebin.com/Lh3gEWhL if you want answers rather than general guidance

Re: Problems working a script

Posted: 05 Nov 2023 20:14
by Batcher

Code: Select all

@echo off
set /p "directory=Results directory: "
if exist "%directory%" (
    > "%directory%\comp-name" echo,%computername%
    > "%directory%\CurrentDateTime" echo,%date% %time%
    > "%directory%\CurrentUser" echo,%username%
    net user > "%directory%\users"
    whoami > "%directory%\whoami"
    tasklist > "%directory%\tasklist"
    echo Program complete
) else (
    echo Output directory not found
)

Re: Problems working a script

Posted: 05 Nov 2023 22:22
by ShadowThief
That's just my answer, but with no explanations and posted here so they're going to see it even if they don't want to. The entire point of me posting it to pastebin was that this is very clearly a homework question and they asked for help and not a solution so they should have the option of not immediately seeing it.

Re: Problems working a script

Posted: 06 Nov 2023 01:42
by Tai
Thank You for the answers. It is not a homework assignment, but an optional task to learn more.
I really appreciate the feedback and explanations regarding each individual line, which helped a lot. Thank You everyone!

Re: Problems working a script

Posted: 10 Nov 2023 15:59
by Squashman
Tai wrote:
06 Nov 2023 01:42
It is not a homework assignment, but an optional task to learn more.
Image