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.