Reputation:
I have this assignment from my teacher in regards to creating a batch file that does something. its pretty huge assignment, but one part i stuck. it asks: If the value of the userdomain variable is equal to the value of the computername variable then Store into a file called output.txt the following information: 1. The current username 2. The current computername
So im thinking, my username(domain username) would never be same as a computer name anywhere in the world! lol So that would NEVER happen! in this case i should do nothing!
So im confused. What am i missing?!
Upvotes: 0
Views: 1685
Reputation: 49187
It is possible to give the computer the name of the user account in a domain. That is not forbidden. That your computer has a different name than your user account in domain or locally does not mean it is not possible.
@echo off
setlocal EnableExtensions DisableDelayedExpansion
:RunNameCompare
if /I "%COMPUTERNAME%" == "%USERNAME%" (
setlocal EnableDelayedExpansion
echo User name: !USERNAME!>output.txt
echo Computer name: !COMPUTERNAME!>>output.txt
endlocal
) else (
echo/
echo Computer name and user account name are different.
echo/
%SystemRoot%\System32\choice.exe /N /M "Simulate identical names [Y/N]: "
if not errorlevel 2 set "COMPUTERNAME=%USERNAME%" & goto RunNameCompare
)
endlocal
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
choice /?
echo /?
endlocal /?
goto /?
if /?
set /?
setlocal /?
See also the following pages and articles:
The outer SETLOCAL and ENDLOCAL are used to create a local environment on running this batch file so that even after taking the simulate identical names option, the predefined environment variable COMPUTERNAME
has its original value after running the batch file from within a command prompt window as recommended on debugging and verifying a batch file in development.
The inner SETLOCAL and ENDLOCAL are used to be able to output correct into file output.txt
even computer and user account names containing command line critical characters like &()[]{}^=;!'+,`~|<>
or ending with a space and a number in range 1
to 9
without enclosing both names in double quotes.
Upvotes: 1