Dr. Mark Humphrys

School of Computing. Dublin City University.

Home      Blog      Teaching      Research      Contact

My big idea: Ancient Brain


CA114      CA170

CA668      CA669      Projects

Windows batch files

You can write scripts on Windows command line.
You put your commands in a "batch file" with a name like PROG.BAT, and then to run it type PROG.

@echo off
rem the above line prevents the script echoing back the commands
rem is a comment


Some features

Demos using the Shakespeare files as before.

  1. Pipes:
     dir | find "string" | sort
    Go into henryiv_1
    find "Scot" act*.html
    find "Scot" act*.html | find "Scots"
    find "Scot" act*.html | find /v "Scots"

  2. Redirection:
     dir > file

  3. IF and command-line arguments and string compare:
    if '%1'=='' echo no arg  
    if '%1'=='' ( echo no arg ) else ( echo arg )

  4. FOR loops and CALL of another batch file:
      for %%i in (*.html) do call secondprog %%i

  5. Environment variables:
    set myvar=string
    echo myvar is %myvar%  
      echo path is %path%
      copy %1 %homepath%\backup
  6. multiple statements in block:
    for %%i in (*.*) do (
      echo %%i
      dir %%i
    IF EXIST "C:\Program Files\Mozilla Firefox" (
      echo Firefox exists
    ) ELSE (
      echo Firefox missing
    Cannot change this layout around too much.

  8. We can get around the lack of directory wildcards with a FOR loop:
    @echo off
    for /d %%d in ("r*") do find "Ireland" %%d\*html
    This is the equivalent of:
    grep Ireland r*/*html  

The BAT scripting language is generally simpler than UNIX/Linux Shell, and the standard utilities are fewer, and have less options, than on UNIX/Linux.

But there are more powerful Windows command-lines.

ancientbrain.com      w2mind.org      humphrysfamilytree.com

On the Internet since 1987.

Wikipedia: Sometimes I link to Wikipedia. I have written something In defence of Wikipedia. It is often a useful starting point but you cannot trust it. Linking to it is like linking to a Google search. A starting point, not a destination. I automatically highlight in red all links to Wikipedia and Google search and other possibly-unreliable user-generated content.