0

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?!

  • 1
    Asking your instructor to clarify the assignment if you're confused. – Ken White Nov 29 '17 at 17:43
  • I did, she said, reread the assignment! so thats why i paste it here. maybe im reading it wrong! –  Nov 29 '17 at 17:57
  • Then I suspect you're overthinking the problem, and you're expected to write the code just as described without worrying about whether it could ever happen or not. The point is most likely an exercise in figuring out how to write the code. – Ken White Nov 29 '17 at 17:58
  • Don't over think it. I answered your question on [DosTips](https://www.dostips.com/forum/viewtopic.php?f=3&t=8254) as well. – Squashman Nov 29 '17 at 19:49

1 Answers1

1

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.

Mofi
  • 46,139
  • 17
  • 80
  • 143