0

Im in the middle of a small programming assignment right now, and am stumped. What needs to be done is->

Create a batch file to perform the following steps ( written the best I can in pseudocode )

IF USERDOMAIN==COMPUTERNAME 
THEN
CreateFile cake.txt
cake.txt = %USERNAME% + " " + %COMPUTERNAME% + " " + nslookup(www.disney.com)

How far I've gotten on the other hand is so=

IF USERDOMAIN == COMPUTERNAME GOTO Text

:Text
echo.>"C:\cake.txt"
rem Saved in C:\cake.txt
echo USERNAME >> cake.txt
echo COMPUTERNAME >> cake.txt

I only know C#, and using stackoverflow I have found this similar question: Batch script for loop & nslookup

But, the syntax in that question is just of nslookup in general, I dont understand the windows batch syntax at all, I got this far through a couple hours of searching.

Matapatapa
  • 3
  • 1
  • 4

1 Answers1

0

It should be:

@echo off
rem case insensitive string comparison
if /i %USERDOMAIN% == %COMPUTERNAME% (
   rem clear file
   break>"C:\cake.txt"
   rem output all commands in following block to file
   (
      echo %USERNAME%
      echo %COMPUTERNAME%
      nslookup www.disney.com
   ) >> "C:\cake.txt"
)

if you want to filter the output of nslookup you have to do it differently but as you don't tell about how the output should look like this mabe should be enough.

I dont understand the windows batch syntax at all

If you would be more specific about your problems it would be possible to explain you the things you didn't get.
A good reference about batch scripting is the site SS64. Every command is very well explained there and completed by examples.


Why did you use @echo off? in the start, and use echo later on? Isn't it just a way to output status info of how the code is progressing? The purpose of the /i is also unknown to me, and finally, why did you break before handling the file? I read that a single > is for removing and clearing text, so why would that need a break of some sort?

1. @echo off and echo:

The @echo off command is used so that the commands itself are not displayed during the batch processing, consider the following example:

@echo off
echo Hello world with echo off!
@echo on
echo Hello world with echo on!

In your command prompt it would print something like:

Hello world with echo off!

C:\Users\...\Desktop>echo Hello world with echo on!
Hello world with echo on!

The echo command is just used to output something to the console.

2. if /i:

The /i will compare the strings case insensitive so that "ABC" == "abc" or "abC" == "AbC" will be true.

3. break and > or >>:

To clear a file you used echo.>"C:\cake.txt". This is not completely wrong but that will save a new line into the file and not clear it as echo. means new line. The break command will generate no output (see here on SO), if that will be redirected to a file with > the file will be completely empty.
You differ between > and >> when talking about redirection. A single > means overwriting the existing file if it already exists and a double >> means append to file or create a new file if it doesn't exists.

Andre Kampling
  • 5,476
  • 2
  • 20
  • 47
  • 1
    I would suggest, that you consider using `IF /I "%USERDOMAIN%"=="%COMPUTERNAME%" (` instead. – Compo Aug 03 '17 at 12:26
  • Okay; I was doing some googling and I sorta understand the echo command. But; the rem command, does that create a file there? Why does it say "rem clear file" – Matapatapa Aug 03 '17 at 12:26
  • @Compo: Thank you right... Case insensitive is better here! – Andre Kampling Aug 03 '17 at 12:29
  • @Matapatapa: `rem` is used to comment, everthing after rem is not interpreted by the command shell. – Andre Kampling Aug 03 '17 at 12:29
  • Okay; I understand it now, but I still have several questions. Why did you use "@echo off"? in the start, and use echo later on? Isn't it just a way to output status info of how the code is progressing? The purpose of the "/i" is also unknown to me, and finally, why did you break before handling the file? I read that a single ">" is for removing and clearing text, so why would that need a break of some sort? – Matapatapa Aug 03 '17 at 12:57
  • @Matapatapa: I extended my answer, maybe now everthing is clear. But as I said [SS64](https://ss64.com/nt/) is a good reference. Also using commands with `/?` will provide much helpful informations. – Andre Kampling Aug 03 '17 at 13:18
  • Okay; I just went over it one more time. It all makes sense now. Thanks for the help! – Matapatapa Aug 04 '17 at 03:29