Problems working a script

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Tai
Posts: 2
Joined: 05 Nov 2023 07:10

Problems working a script

#1 Post by Tai » 05 Nov 2023 07:27

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

ShadowThief
Expert
Posts: 1162
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Problems working a script

#2 Post by ShadowThief » 05 Nov 2023 18:11

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.

ShadowThief
Expert
Posts: 1162
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Problems working a script

#3 Post by ShadowThief » 05 Nov 2023 18:21

https://pastebin.com/Lh3gEWhL if you want answers rather than general guidance

Batcher
Posts: 74
Joined: 16 Apr 2009 10:36

Re: Problems working a script

#4 Post by Batcher » 05 Nov 2023 20:14

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
)

ShadowThief
Expert
Posts: 1162
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Problems working a script

#5 Post by ShadowThief » 05 Nov 2023 22:22

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.

Tai
Posts: 2
Joined: 05 Nov 2023 07:10

Re: Problems working a script

#6 Post by Tai » 06 Nov 2023 01:42

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!

Squashman
Expert
Posts: 4470
Joined: 23 Dec 2011 13:59

Re: Problems working a script

#7 Post by Squashman » 10 Nov 2023 15:59

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

Post Reply