• #eSolia Scripting Notes

    This set of notes is to help a beginner at batch scripting get a basic level of understanding and competence in the topic.

    Rick Cogley, 25 Nov 2014

  • Utilities
  • eSolia Scripting - Part I

    This section is intended to help get a beginning batch scripter started.

  • How can I loop?

  • Put date in output
    Loop
    Read file
    Get help
    Prove it worked
    Change
    Prompt
    Whoami
    Rights
    Cacls
    Drive map
    Pwd
    What’s running ps
    Net session
    Start and stop
    Restart
    Run batch from cmd line or, start menu
    User input
    http://www.instructables.com/id/5-Cool-Batch-Files/

  • CSS:

  • Try a gist:

  • What is scripting?

  • What is a batch file?

  • Where should I store batch files?

  • How do I edit a batch file?

  • How do I run a batch file?

  • How do I suppress console output?

  • How can I put comments into a batch file to describe what it is doing?

  • How can I cause the batch to display text as it runs?

  • How can I display a blank line between commands?

  • How do I get help with commands?

  • How can I capture the output from the script in a text file?

  • How can I use a variable instead of a static filename for output?

  • How can I set my ping target at run time?

  • How do I echo the date and time into a filename?

  • for in do

    • Scripting generally describes the programming one does when working with batch or script files, and often is used in systems admininistration work, or website design and build work.

      See Scripting Language on Wikipedia for a lot of information on this topic.

    • A “batch script” is a text file with commands which are executed line by line, by the OS’s command interpreter, cmd.exe on Windows, and a shell like BaSH or Z Shell on *nix systems.

      A batch file has an extension of either .bat or .cmd.

      On Windows, it’s usually called a batch file or “batch script”. On Unix and Mac, it’s usually called a script or “shell script”.

      This outline addresses batch scripts in a Windows environment.

    • Store your batch files in a folder somewhere: either a project folder, or a common location such as c:¥eSolia.

    • Use a good “programmer’s editor” such as the following:

      • On Windows
        • Sublime Text
        • Notepad++
      • Mac
        • Sublime Text
        • BB Edit (or their free Text Wrangler)

      Note: the free editors that come with the OS, such as Notepad or メモ帳 on Windows or TextEdit on Mac, are often too basic to be useful. It is a worthwhile investment to purchase a good text editor to work with, as an IT professional.

    • To run a batch file, simply enter the name of the batch file at the command prompt. For example, if your batch file is called mybat.bat, just type “mybat.bat” (without the quotes) and press enter.

      When I say “command prompt” in the context of this outline, I am talking about the command line program in Windows that “prompts” you to enter a command with its > symbol. On other operating systems it could also of course mean the Terminal program on a *nix-like system.

      Win cmd.exe prompt screenshot

    • When you run a batch for the first time, you will notice that all commands are repeated on the console. To suppress the repetitive display, enter this at the top of your batch file:

      echo off

      You will notice that the echo off line will itself be displayed twice when you run the batch. Enter a @ in front of that line, to supress the extra echo:

      @echo off

      Once you do that, now each line’s output only will be output on the command line.

      Use @echo off at the beginning of every Windows batch file.

    • Engineers seem to dislike documentation, but we should do it to help our brothers and sisters out, when they take over maintenance of programs and systems.

      It is a convention to make your scripts (and indeed any programming code) self-documenting, and you do this by adding “commented out” lines to describe what is going on, in your batch.

      Use REM or # followed by a space, at the front of any line to “comment it out”, or, cause the batch interpreter to ignore it.

    • If you want to have your batch display text as it runs, for status messages and so on, use the echo command, followed by the text you want to display, in parentheses.

      echo "this will be displayed"

      You can also use system variables with echo. Use the “set” command to see a list of the variables, then reference them between percentage signs, like so:

      echo "Computer: %computername%"

    • It begs the question, then, how do you echo a blank line? Simple: use echo followed immediately by a period, with no space.

      echo.

    • You see a lot of commands being used here, but how do you get help on their usage and syntax? You can Google them, of course, but the OS has information about them which you can access from the command prompt.

      Just use /? after the command you want to act upon, to view its help. For example, get help on echo and set.

      >set /?

      Some commands use the syntax --help for this, so you should try both.

      >thecommand --help

      The net command in Windows has an unusual help syntax which uses neither /? nor --help. Get help like this:

      >net help use

      Many commands have multilingual help. If you do mycommand /? and get an unexpected language, use the chcp command to change code pages. For instance:

      >chcp 437

      See this page for more info on code pages.

    • If you want to create a file from the results of the commands inside your batch, then use > and >> to redirect their output to a file.

      • The former, >, creates the file.
      • The latter, >>, appends to the file.

      set > myoutput.txt

      It’s common to use > at the top, and >> for the rest of the commands that put their result into your output file.

    • Instead of repeatedly entering results.txt to capture output, you can create a variable to use, and then set the variable to any string.

      A variable has a name and a value, and you use the set command to set one.

      >set myvar=thevalue

      Notice the difference between setting and calling a variable.

      >set favoritedrink=coffee
      >echo %favoritedrink%

      Notice there are no spaces around the equals sign.

    • So far, our batch has a fixed value to ping, but if you were using this batch file at various support client sites, for instance, you would want to be able to vary the ping target each time.

      You can use batch “arguments” to do that. Arguments are strings you enter after the batch name when you run it, which are then referenced inside the batch. Here’s an example with a single argument:

      >mybatch.bat www.microsoft.com

      Inside mybatch.bat you would reference the ping target like so:

      @echo off
      ping %1 > %computername%.txt
    • Next, let’s figure out how to get the date or time into the filename.
      first test it. Outside a batch do:

      time /t
      ?date /t
      oops
      date /t
      then try:
      echo %date%
      echo %time%
      then try what’s called “substring” parameters on the echo commands. for example:
      Hitomi
      Thank you. I will do it from now.
      Rick
      echo %time:~0,2%
      the :~0,2 part means:
      start at character 0 and show 2
      first char = 0th char, in terms of computer language
      try echoing, changing :~N,M as needed, to get the parts of the time or date strings you want
      then assemble them using set and echo the result
      the goal is to have an output file like - eSolia-MyName-MyComputerName-201412xxxxxx.txt
      …showing the date and time at the end
      You can substitute spaces for zeroes or any other char, this way -
      set somevar=%somevar: =0%
      It means “take the space and replace with 0”
      16:55
      you’d set somevar first, then set it again, replacing space with 0, or another char. Try it.

    • Example:

    • Try:

      You can create your storage folder with Explorer, or, using the cd and mkdir commands on the command line.

    • Try:

      Get a programmer’s editor, and create a batch file, saving it in c:¥eSolia. Be sure to save it with the correct extension.

    • Try:

      Confirm whether you need to type the .bat or .cmd extension, or, whether you can simply type the filename without the extension, when you run a batch.

      c:¥eSolia>mybatch
      c:¥eSolia>mybatch.bat

      When it does run, what do you observe?

    • Try:

      Using your simple batch as an example, trying it with echo off or @echo off at the top.

    • Example:

    • Try:

      Edit your batch to include both REM and # comments. When you run it, do you see them on the command line?

    • Example:

    • Example:

      C:\Windows\system32>echo /? 
      メッセージを表示したり、コマンド エコーの ON と OFF を切り替えます。
      
        ECHO [ON | OFF]
        ECHO [メッセージ]
      
      現在のエコー設定を表示するには、パラメーターを指定せずに ECHO と入力してください。
    • Example:

    • Try:

      Of course you can use your editor to view the contents of the output file. However, also try using the type command.

      >type results.txt

    • Try:

      Edit your batch file to replace results.txt with a variable. Try changing the variable value, and rerunning the batch. Does the filename of the file containing the output change as expected?

      Set a variable inside a batch. Can you echo the variable on the command prompt, after the batch finishes?

      Set a variable outside a batch and use echo to display its contents. Does the variable persist after you restart the command prompt?

    • Example:

    • Try:

      Edit your batch file to replace the fixed ping target with the first batch argument.

      Edit the batch file to process two arguments as ping targets.

      >mybatch.bat www.microsoft.com www.google.com

    • Example:

      @echo off
      # Author: Rick Cogley
      # Last Update: 1 Nov 2014
      # Description: this batch is a test, to show how to output command results to a text file, which filename is defined by a variable.
      set myresults=results.txt
      echo "== STARTING BATCH =="
      echo "Results from %computername%:" > %myresults% 
      echo "Pinging..." >> %myresults%
      ping -n 4 %1 >> %myresults%
      echo. >> %myresults%
      echo "Running SET..." >> %myresults%
      set >> %myresults%
      echo. >> %myresults%
      echo "PC info..." >> %myresults%
      echo "Computer: %computername%" >> %myresults%
      echo "Domain: %computerdomain%" >> %myresults%
      echo "== ENDING BATCH =="
    {"cards":[{"_id":"4c83dc9409d3a0a04b000014","treeId":"4c83dc7109d3a0a04b000012","seq":1247683,"position":1,"parentId":null,"content":"#eSolia Scripting Notes\n\nThis set of notes is to help a beginner at batch scripting get a basic level of understanding and competence in the topic.\n\n<span class=\"glyphicon glyphicon-pencil\"></span> Rick Cogley, 25 Nov 2014\n"},{"_id":"4ccc460264c9a78bcf000039","treeId":"4c83dc7109d3a0a04b000012","seq":1157493,"position":0.5,"parentId":"4c83dc9409d3a0a04b000014","content":"##eSolia Scripting - Part I\n\nThis section is intended to help get a beginning batch scripter started. "},{"_id":"4c83dfd509d3a0a04b000015","treeId":"4c83dc7109d3a0a04b000012","seq":1157494,"position":1,"parentId":"4ccc460264c9a78bcf000039","content":"###What is scripting?"},{"_id":"4c840b8109d3a0a04b000021","treeId":"4c83dc7109d3a0a04b000012","seq":1145283,"position":1,"parentId":"4c83dfd509d3a0a04b000015","content":"Scripting generally describes the programming one does when working with batch or script files, and often is used in systems admininistration work, or website design and build work.\n\nSee [Scripting Language](https://en.wikipedia.org/wiki/Scripting_language) on Wikipedia for a lot of information on this topic. "},{"_id":"4c84086e09d3a0a04b000017","treeId":"4c83dc7109d3a0a04b000012","seq":1157496,"position":2,"parentId":"4ccc460264c9a78bcf000039","content":"###What is a batch file?"},{"_id":"4c840c1709d3a0a04b000022","treeId":"4c83dc7109d3a0a04b000012","seq":1156015,"position":1,"parentId":"4c84086e09d3a0a04b000017","content":"A \"batch script\" is a text file with commands which are executed line by line, by the OS's command interpreter, cmd.exe on Windows, and a shell like BaSH or Z Shell on *nix systems. \n\nA batch file has an extension of either .bat or .cmd.\n\nOn Windows, it's usually called a batch file or \"batch script\". On Unix and Mac, it's usually called a script or \"shell script\".\n\nThis outline addresses batch scripts in a Windows environment. \n\n\n"},{"_id":"4c8415ab09d3a0a04b000030","treeId":"4c83dc7109d3a0a04b000012","seq":1247674,"position":1,"parentId":"4c840c1709d3a0a04b000022","content":"_Example_:\n\n<iframe src=\"https://pastebin.esolia.com/view/embed/40959ed3\" style=\"border:none;\"></iframe>"},{"_id":"4c84090e09d3a0a04b000019","treeId":"4c83dc7109d3a0a04b000012","seq":1157497,"position":3,"parentId":"4ccc460264c9a78bcf000039","content":"###Where should I store batch files?"},{"_id":"4c840eef09d3a0a04b000025","treeId":"4c83dc7109d3a0a04b000012","seq":1122363,"position":1,"parentId":"4c84090e09d3a0a04b000019","content":"Store your batch files in a folder somewhere: either a project folder, or a common location such as `c:¥eSolia`."},{"_id":"4c92d9759c98c0d4dc00002c","treeId":"4c83dc7109d3a0a04b000012","seq":1130084,"position":1,"parentId":"4c840eef09d3a0a04b000025","content":"_<span class=\"glyphicon glyphicon-fire\" style=\"color:aqua\"></span> Try_: \n\nYou can create your storage folder with Explorer, or, using the cd and mkdir commands on the command line. "},{"_id":"4c8408e809d3a0a04b000018","treeId":"4c83dc7109d3a0a04b000012","seq":1157508,"position":4,"parentId":"4ccc460264c9a78bcf000039","content":"###How do I edit a batch file?"},{"_id":"4c840e6809d3a0a04b000024","treeId":"4c83dc7109d3a0a04b000012","seq":1155776,"position":1,"parentId":"4c8408e809d3a0a04b000018","content":"Use a good \"programmer's editor\" such as the following:\n\n* On Windows\n * Sublime Text \n * Notepad++\n* Mac\n * Sublime Text\n * BB Edit (or their free Text Wrangler)\n\n_Note_: the free editors that come with the OS, such as Notepad or メモ帳 on Windows or TextEdit on Mac, are often too basic to be useful. It is a worthwhile investment to purchase a good text editor to work with, as an IT professional. "},{"_id":"4c92c9849c98c0d4dc00002b","treeId":"4c83dc7109d3a0a04b000012","seq":1130082,"position":1,"parentId":"4c840e6809d3a0a04b000024","content":"_<span class=\"glyphicon glyphicon-fire\" style=\"color:aqua\"></span> Try_: \n\nGet a programmer's editor, and create a batch file, saving it in `c:¥eSolia`. Be sure to save it with the correct extension."},{"_id":"4c84094809d3a0a04b00001a","treeId":"4c83dc7109d3a0a04b000012","seq":1157498,"position":5,"parentId":"4ccc460264c9a78bcf000039","content":"###How do I run a batch file?"},{"_id":"4c840f1e09d3a0a04b000026","treeId":"4c83dc7109d3a0a04b000012","seq":1130073,"position":1,"parentId":"4c84094809d3a0a04b00001a","content":"To run a batch file, simply enter the name of the batch file at the command prompt. For example, if your batch file is called `mybat.bat`, just type \"mybat.bat\" (without the quotes) and press enter. \n\nWhen I say \"command prompt\" in the context of this outline, I am talking about the command line program in Windows that \"prompts\" you to enter a command with its `>` symbol. On other operating systems it could also of course mean the Terminal program on a *nix-like system.\n\n![Win cmd.exe prompt screenshot](https://dl.dropbox.com/s/i3986kwhbbvdy22/JRC%20-%20Windows%20cmd.exe%202014-11-26_08-39-23.png?dl=0)"},{"_id":"4c92ddd49c98c0d4dc00002d","treeId":"4c83dc7109d3a0a04b000012","seq":1130109,"position":1,"parentId":"4c840f1e09d3a0a04b000026","content":"_<span class=\"glyphicon glyphicon-fire\" style=\"color:aqua\"></span> Try_: \n\nConfirm whether you need to type the .bat or .cmd extension, or, whether you can simply type the filename without the extension, when you run a batch. \n\n~~~\nc:¥eSolia>mybatch\nc:¥eSolia>mybatch.bat\n~~~\n\nWhen it _does_ run, what do you observe?"},{"_id":"4c84096e09d3a0a04b00001b","treeId":"4c83dc7109d3a0a04b000012","seq":1157499,"position":6,"parentId":"4ccc460264c9a78bcf000039","content":"###How do I suppress console output?"},{"_id":"4c840f5109d3a0a04b000027","treeId":"4c83dc7109d3a0a04b000012","seq":1130104,"position":1,"parentId":"4c84096e09d3a0a04b00001b","content":"When you run a batch for the first time, you will notice that all commands are repeated on the console. To suppress the repetitive display, enter this at the top of your batch file:\n\n`echo off`\n\nYou will notice that the `echo off` line will itself be displayed twice when you run the batch. Enter a @ in front of that line, to supress the extra echo: \n\n`@echo off`\n\nOnce you do that, now each line's output only will be output on the command line. \n\nUse `@echo off` at the beginning of every Windows batch file.\n"},{"_id":"4c92f4ba9c98c0d4dc00002e","treeId":"4c83dc7109d3a0a04b000012","seq":1130116,"position":1,"parentId":"4c840f5109d3a0a04b000027","content":"_<span class=\"glyphicon glyphicon-fire\" style=\"color:aqua\"></span> Try_: \n\nUsing your simple batch as an example, trying it with `echo off` or `@echo off` at the top. "},{"_id":"4c840fcb09d3a0a04b000028","treeId":"4c83dc7109d3a0a04b000012","seq":1157500,"position":7,"parentId":"4ccc460264c9a78bcf000039","content":"###How can I put comments into a batch file to describe what it is doing?"},{"_id":"4c84107f09d3a0a04b000029","treeId":"4c83dc7109d3a0a04b000012","seq":1122406,"position":1,"parentId":"4c840fcb09d3a0a04b000028","content":"Engineers seem to dislike documentation, but we should do it to help our brothers and sisters out, when they take over maintenance of programs and systems. \n\nIt is a convention to make your scripts (and indeed any programming code) self-documenting, and you do this by adding \"commented out\" lines to describe what is going on, in your batch. \n\nUse `REM` or `#` followed by a space, at the front of any line to \"comment it out\", or, cause the batch interpreter to ignore it. \n\t\n"},{"_id":"4c841eb309d3a0a04b000031","treeId":"4c83dc7109d3a0a04b000012","seq":1157513,"position":1,"parentId":"4c84107f09d3a0a04b000029","content":"_Example_:\n\n<iframe src=\"https://pastebin.esolia.com/view/embed/15687865\" style=\"border:none;width:100%;height:100%\"></iframe>"},{"_id":"4c92fac89c98c0d4dc00002f","treeId":"4c83dc7109d3a0a04b000012","seq":1155994,"position":2,"parentId":"4c84107f09d3a0a04b000029","content":"_<span class=\"glyphicon glyphicon-fire\" style=\"color:aqua\"></span> Try_: \n\nEdit your batch to include both REM and # comments. When you run it, do you see them on the command line? "},{"_id":"4c8409c709d3a0a04b00001c","treeId":"4c83dc7109d3a0a04b000012","seq":1157502,"position":8,"parentId":"4ccc460264c9a78bcf000039","content":"###How can I cause the batch to display text as it runs?"},{"_id":"4c84111809d3a0a04b00002a","treeId":"4c83dc7109d3a0a04b000012","seq":1122391,"position":1,"parentId":"4c8409c709d3a0a04b00001c","content":"If you want to have your batch display text as it runs, for status messages and so on, use the echo command, followed by the text you want to display, in parentheses. \n\n`echo \"this will be displayed\"`\n\nYou can also use system variables with echo. Use the \"set\" command to see a list of the variables, then reference them between percentage signs, like so:\n\n`echo \"Computer: %computername%\"`"},{"_id":"4c840a2109d3a0a04b00001d","treeId":"4c83dc7109d3a0a04b000012","seq":1157501,"position":9,"parentId":"4ccc460264c9a78bcf000039","content":"###How can I display a blank line between commands?"},{"_id":"4c84116409d3a0a04b00002b","treeId":"4c83dc7109d3a0a04b000012","seq":1122399,"position":1,"parentId":"4c840a2109d3a0a04b00001d","content":"It begs the question, then, how do you echo a blank line? Simple: use echo followed immediately by a period, with no space.\n\n`echo.`\n\t"},{"_id":"4c841ee809d3a0a04b000032","treeId":"4c83dc7109d3a0a04b000012","seq":1170249,"position":1,"parentId":"4c84116409d3a0a04b00002b","content":"_Example_:\n\n<iframe src=\"https://pastebin.esolia.com/view/embed/85f21942\" style=\"border:none;width:100%;height:100%\" ></iframe>"},{"_id":"4c8a54665ae50c51560000f1","treeId":"4c83dc7109d3a0a04b000012","seq":1157503,"position":10,"parentId":"4ccc460264c9a78bcf000039","content":"###How do I get help with commands? "},{"_id":"4c8a55815ae50c51560000f2","treeId":"4c83dc7109d3a0a04b000012","seq":1122940,"position":1,"parentId":"4c8a54665ae50c51560000f1","content":"You see a lot of commands being used here, but how do you get help on their usage and syntax? You can Google them, of course, but the OS has information about them which you can access from the command prompt. \n\nJust use `/?` after the command you want to act upon, to view its help. For example, get help on echo and set. \n\n`>set /?`\n\nSome commands use the syntax `--help` for this, so you should try both. \n\n`>thecommand --help`\n\nThe net command in Windows has an unusual help syntax which uses neither `/?` nor `--help`. Get help like this: \n\n`>net help use`\n\nMany commands have multilingual help. If you do `mycommand /?` and get an unexpected language, use the `chcp` command to change code pages. For instance: \n\n`>chcp 437`\n\nSee [this page](https://en.wikipedia.org/wiki/Code_page) for more info on code pages. "},{"_id":"4c8a79465ae50c51560000f3","treeId":"4c83dc7109d3a0a04b000012","seq":1122922,"position":1,"parentId":"4c8a55815ae50c51560000f2","content":"_Example_:\n\n~~~\nC:\\Windows\\system32>echo /? \nメッセージを表示したり、コマンド エコーの ON と OFF を切り替えます。\n\n ECHO [ON | OFF]\n ECHO [メッセージ]\n\n現在のエコー設定を表示するには、パラメーターを指定せずに ECHO と入力してください。\n~~~"},{"_id":"4c840a6009d3a0a04b00001e","treeId":"4c83dc7109d3a0a04b000012","seq":1157504,"position":11,"parentId":"4ccc460264c9a78bcf000039","content":"###How can I capture the output from the script in a text file?"},{"_id":"4c84118e09d3a0a04b00002c","treeId":"4c83dc7109d3a0a04b000012","seq":1155902,"position":1,"parentId":"4c840a6009d3a0a04b00001e","content":"If you want to create a file from the results of the commands inside your batch, then use `>` and `>>` to redirect their output to a file. \n\n* The former, `>`, creates the file. \n* The latter, `>>`, appends to the file. \n\n`set > myoutput.txt`\n\nIt's common to use `>` at the top, and `>>` for the rest of the commands that put their result into your output file. \n\t"},{"_id":"4c841eff09d3a0a04b000033","treeId":"4c83dc7109d3a0a04b000012","seq":1157136,"position":1,"parentId":"4c84118e09d3a0a04b00002c","content":"_Example_:\n\n<iframe src=\"https://pastebin.esolia.com/view/embed/196ee4d7\" style=\"border:none;width:100%\" height=450px></iframe>"},{"_id":"4cc843c364c9a78bcf000035","treeId":"4c83dc7109d3a0a04b000012","seq":1156030,"position":2,"parentId":"4c84118e09d3a0a04b00002c","content":"_<span class=\"glyphicon glyphicon-fire\" style=\"color:aqua\"></span> Try_: \n\nOf course you can use your editor to view the contents of the output file. However, also try using the `type` command. \n\n`>type results.txt`"},{"_id":"4cc81e7b64c9a78bcf000031","treeId":"4c83dc7109d3a0a04b000012","seq":1157505,"position":12,"parentId":"4ccc460264c9a78bcf000039","content":"###How can I use a variable instead of a static filename for output?"},{"_id":"4cc81fec64c9a78bcf000032","treeId":"4c83dc7109d3a0a04b000012","seq":1156056,"position":1,"parentId":"4cc81e7b64c9a78bcf000031","content":"Instead of repeatedly entering `results.txt` to capture output, you can create a variable to use, and then set the variable to any string. \n\nA variable has a name and a value, and you use the `set` command to set one. \n\n`>set myvar=thevalue`\n\nNotice the difference between setting and calling a variable. \n\n`>set favoritedrink=coffee`\n`>echo %favoritedrink%`\n\nNotice there are no spaces around the equals sign. \n\n "},{"_id":"4cc833f664c9a78bcf000034","treeId":"4c83dc7109d3a0a04b000012","seq":1156085,"position":0.5,"parentId":"4cc81fec64c9a78bcf000032","content":"_<span class=\"glyphicon glyphicon-fire\" style=\"color:aqua\"></span> Try_: \n\nEdit your batch file to replace results.txt with a variable. Try changing the variable value, and rerunning the batch. Does the filename of the file containing the output change as expected? \n\nSet a variable inside a batch. Can you echo the variable on the command prompt, after the batch finishes? \n\nSet a variable outside a batch and use echo to display its contents. Does the variable persist after you restart the command prompt? "},{"_id":"4cc8268b64c9a78bcf000033","treeId":"4c83dc7109d3a0a04b000012","seq":1157140,"position":1,"parentId":"4cc81fec64c9a78bcf000032","content":"_Example_:\n\n<iframe src=\"https://pastebin.esolia.com/view/embed/466312e1\" style=\"border:none;width:100%\" height=450px></iframe>"},{"_id":"4c840abb09d3a0a04b00001f","treeId":"4c83dc7109d3a0a04b000012","seq":1157506,"position":13,"parentId":"4ccc460264c9a78bcf000039","content":"###How can I set my ping target at run time? "},{"_id":"4c8411c009d3a0a04b00002d","treeId":"4c83dc7109d3a0a04b000012","seq":1156069,"position":1,"parentId":"4c840abb09d3a0a04b00001f","content":"So far, our batch has a fixed value to ping, but if you were using this batch file at various support client sites, for instance, you would want to be able to vary the ping target each time. \n\nYou can use batch \"arguments\" to do that. Arguments are strings you enter after the batch name when you run it, which are then referenced inside the batch. Here's an example with a single argument:\n\n`>mybatch.bat www.microsoft.com`\n\nInside `mybatch.bat` you would reference the ping target like so: \n\n~~~\n@echo off\nping %1 > %computername%.txt\n~~~\n\n"},{"_id":"4cc883ea64c9a78bcf000037","treeId":"4c83dc7109d3a0a04b000012","seq":1156101,"position":0.5,"parentId":"4c8411c009d3a0a04b00002d","content":"_<span class=\"glyphicon glyphicon-fire\" style=\"color:aqua\"></span> Try_: \n\nEdit your batch file to replace the fixed ping target with the first batch argument. \n\nEdit the batch file to process two arguments as ping targets. \n\n`>mybatch.bat www.microsoft.com www.google.com`"},{"_id":"4cc87add64c9a78bcf000036","treeId":"4c83dc7109d3a0a04b000012","seq":1171240,"position":1,"parentId":"4c8411c009d3a0a04b00002d","content":"_Example_:\n\n~~~\n@echo off\n# Author: Rick Cogley\n# Last Update: 1 Nov 2014\n# Description: this batch is a test, to show how to output command results to a text file, which filename is defined by a variable.\nset myresults=results.txt\necho \"== STARTING BATCH ==\"\necho \"Results from %computername%:\" > %myresults% \necho \"Pinging...\" >> %myresults%\nping -n 4 %1 >> %myresults%\necho. >> %myresults%\necho \"Running SET...\" >> %myresults%\nset >> %myresults%\necho. >> %myresults%\necho \"PC info...\" >> %myresults%\necho \"Computer: %computername%\" >> %myresults%\necho \"Domain: %computerdomain%\" >> %myresults%\necho \"== ENDING BATCH ==\"\n~~~\n\n<iframe src=\"https://pastebin.esolia.com/view/embed/79b7ef2c\" style=\"border:none;width:100%\" height=450px></iframe>"},{"_id":"4d3586073928e9e6d0000095","treeId":"4c83dc7109d3a0a04b000012","seq":1237345,"position":14,"parentId":"4ccc460264c9a78bcf000039","content":"###How do I echo the date and time into a filename?"},{"_id":"4d3586c53928e9e6d0000096","treeId":"4c83dc7109d3a0a04b000012","seq":1237347,"position":1,"parentId":"4d3586073928e9e6d0000095","content":"Next, let’s figure out how to get the date or time into the filename.\nfirst test it. Outside a batch do: \n>time /t\n?date /t\noops\n>date /t\nthen try:\n>echo %date%\n>echo %time%\nthen try what’s called “substring” parameters on the echo commands. for example:\nHitomi\nThank you. I will do it from now.\nRick\n>echo %time:~0,2%\nthe :~0,2 part means:\nstart at character 0 and show 2\nfirst char = 0th char, in terms of computer language\ntry echoing, changing :~N,M as needed, to get the parts of the time or date strings you want\nthen assemble them using set and echo the result\nthe goal is to have an output file like - eSolia-MyName-MyComputerName-201412xxxxxx.txt\n…showing the date and time at the end\nYou can substitute spaces for zeroes or any other char, this way -\n>set somevar=%somevar: =0%\nIt means “take the space and replace with 0\"\n16:55\nyou’d set somevar first, then set it again, replacing space with 0, or another char. Try it."},{"_id":"4c840aeb09d3a0a04b000020","treeId":"4c83dc7109d3a0a04b000012","seq":1118311,"position":11,"parentId":"4c83dc9409d3a0a04b000014","content":"##How can I loop?"},{"_id":"4c8411e409d3a0a04b00002e","treeId":"4c83dc7109d3a0a04b000012","seq":1118187,"position":1,"parentId":"4c840aeb09d3a0a04b000020","content":"for in do"},{"_id":"4c8412fb09d3a0a04b00002f","treeId":"4c83dc7109d3a0a04b000012","seq":1118190,"position":12,"parentId":"4c83dc9409d3a0a04b000014","content":"Put date in output\nLoop\nRead file\nGet help\nProve it worked\nChange\nPrompt\nWhoami\nRights\nCacls \nDrive map\nPwd \nWhat's running ps \nNet session\nStart and stop\nRestart\nRun batch from cmd line or, start menu\nUser input\nhttp://www.instructables.com/id/5-Cool-Batch-Files/"},{"_id":"4c850ee11ae31b5c69000051","treeId":"4c83dc7109d3a0a04b000012","seq":1247687,"position":1.5,"parentId":null,"content":"#####<span class=\"glyphicon glyphicon-cog\"></span> Utilities <span class=\"glyphicon glyphicon-chevron-right\"></span>"},{"_id":"4c850f271ae31b5c69000052","treeId":"4c83dc7109d3a0a04b000012","seq":1122154,"position":1,"parentId":"4c850ee11ae31b5c69000051","content":"<script type=\"text/javascript\" src=\"https://www.dropbox.com/static/api/2/dropins.js\" id=\"dropboxjs\" data-app-key=\"g8vslfgsfr3v7ty\"></script>\n<button onclick=\"getChooser()\">Image from Dropbox</button>\n\n<script>\ngetChooser = function(){\n Dropbox.choose({\n success: function(files) {\n alert(\"![](\"+files[0].link.replace('dl=0','dl=1')+\")\");\n }\n });\n}\n</script>"},{"_id":"4d0ae8511ffa1344a2000058","treeId":"4c83dc7109d3a0a04b000012","seq":1247689,"position":1.5,"parentId":"4c850ee11ae31b5c69000051","content":"CSS: \n\n<style>\nbody {\nbackground: grey;\n}\n.card h1, .card h2, .card h3{\n font-family: Georgia;\n}\n</style>"},{"_id":"4c85f0476e6f00dcb5000027","treeId":"4c83dc7109d3a0a04b000012","seq":1246337,"position":2,"parentId":"4c850ee11ae31b5c69000051","content":"Try a gist: \n\n<script async src=\"https://gist.github.com/RickCogley/cf819594a09e9141a6a8.js\"></script>\n\n\n\n"}],"tree":{"_id":"4c83dc7109d3a0a04b000012","name":"eSolia Scripting Notes","publicUrl":"esolia-scripting-notes"}}