Some batch commands that I need help with

By bielius
Jun 27, 2012
Post New Reply
  1. Hey, I have to do this project at school, I have to make some .bat files that execute different things.
    what I have to do:
    4. make a batch file called show_info.bat that

    clears the screen,
    then displays the current System version,
    then displays the date followed by the time,
    then displays the computer name,
    then displays the user name,
    then displays the user home directory,

    This is what I have so far:

    CLS
    VER
    %date% %time%
    %computername%
    %username%
    %homedirectory%
    cmd

    I know that this might not(IS NOT) be right. But when for example cmd executes %username% it displays the username and tries to use my username as a command, which is not recognised. My question is, how do I just display those things, instead of "pasting" them in and using as a command?
    oh and what is the command which displays user home directory?
    Thank you so much!
  2. LNCPapa

    LNCPapa TS Special Forces Posts: 4,196   +228

    You need an echo in front of those to just display the result - and it's %homepath% . You can see your variables to work with by typing "set" at a command prompt.
  3. cliffordcooley

    cliffordcooley TechSpot Paladin Posts: 5,062   +1,179

    Running cmd will open another instance of command-line. If you want cmd to automatically do anything when it opens, you will need to give cmd parameters.

    LNCPapa is correct, if you want to display a specific environment variable to screen, you will need to use the echo with the variable as a parameter.

    Example:
    ECHO %PATH%
    CMD /K "C:\RUNTHIS.BAT"

    Type "HELP" for a list of commands. If you want help with a specific command such as ECHO, type "HELP ECHO".
  4. bielius

    bielius TechSpot Booster Topic Starter Posts: 224   +14

    But if I do not type in CMD at the end, the whole cmd window will open up and dissapear immediately.
  5. cliffordcooley

    cliffordcooley TechSpot Paladin Posts: 5,062   +1,179

    Replace cmd with pause. The pause command will hold the windows open till you press any key to continue.
  6. bielius

    bielius TechSpot Booster Topic Starter Posts: 224   +14

    LNCPapa, this is what I am at right now:
    CLS
    ECHO
    VER
    ECHO
    %date% %time%
    ECHO
    %computername%
    ECHO
    %username%
    ECHO
    %homedirectory%
    ECHO
    %homepath%

    pause

    And it still gives me those ugly "is not recognized as an internal or external command, operable program or batch file." lines.
    Now I have tried with ECHO, ECHO ON and ECHO OFF and is still does not make any difference. What I need is just lines with the answer without anything else.
    Sorry, I am still a newb at this, maybe I should go and read some guides. On that topic, maybe you guys know of a good guide on command line commands and such?
  7. cliffordcooley

    cliffordcooley TechSpot Paladin Posts: 5,062   +1,179

    The command and the parameter for the command needs to be on the same line.

    Example:
    dir /s *.avi

    The command dir shows the directory listings of the current folder. The /s parameter includes all subfolders and the *.avi parameter only list the files that end with avi.

    "Echo" by it self is an incomplete command.
    "Echo." with a period behind echo will display an empty line. This will separate text, much like the empty line between paragraphs.
    "Echo" followed by any text or variable will display the text or variable value.

    If the very first line of your bat file is "Echo off", the only lines you will see are the ones you echo to screen.
  8. LNCPapa

    LNCPapa TS Special Forces Posts: 4,196   +228

    Being that you say this is a school project I'm trying my hardest to not just hand you the completed code... perhaps you should use the help command a bit more. For instance, you could have typed in "help echo" at a command prompt to get proper syntax info about that command. I personally feel everyone who uses computers for more than just surfing the web and typing up papers should learn how to create simple batch files. Nothing too complicated, but enough to have a decent foundation and not be scared off when they see a bit of code.
  9. bielius

    bielius TechSpot Booster Topic Starter Posts: 224   +14

    Okay, So I got this. the problem now being is, that I get the answer f.ex. "28.06.2012" as the %Date%, but it still says Is not recognised as an internal... bla bla bla. I really do not know what to do next. If you could give me one working line of the code, I would work my way with it and actually learn what is going on.

    Echo off


    VER

    %date%

    %time%

    pause
  10. LNCPapa

    LNCPapa TS Special Forces Posts: 4,196   +228

    Code:
    @echo off
    ver
    echo %date%
    echo %time%
    pause
    The echo command tells it to display the result to the screen (or if you are redirecting it, elsewhere).
  11. wrt54gs7

    wrt54gs7 TechSpot Enthusiast Posts: 131

    Code:
    @echo off
    cls
    ver
    echo Current Date and Time: %date% %time%
    echo Computer Name: %COMPUTERNAME%
    echo User Name: %USERNAME%
    echo User Home Directory: %USERPROFILE%
    pause
  12. Zilpha

    Zilpha TechSpot Enthusiast Posts: 349

    I think your confusion is coming from not actually understanding what you are typing. The items surrounded by % are environment variables. They store data in a way that makes it convenient for us to access and use.

    cmd, VER, CLS, those are actual commands that tell the computer we want it to *do* something.

    So we need to tell the computer to *do* something *with* those environment variables. You've been given the ECHO command already, and it would benefit you greatly to look it up and learn what it tells the computer to *do*. Then you should be able to figure out how to use it with the environment variables to present the data in a meaningful script.

    Just coming to a forum and asking for code is not going to help anyone - not you, not your school, and certainly not your future employer when they realize you didn't learn basic scripting because someone on a forum gave you the answer. Not trying to be mean, but we have a lot of "paper" diplomas out in the wild and it's a shame.


Add New Comment

TechSpot Members
Login or sign up for free,
it takes about 30 seconds.
You may also...


Get complete access to the TechSpot community. Join thousands of technology enthusiasts that contribute and share knowledge in our forum. Get a private inbox, upload your own photo gallery and more.