Dr. Mark Humphrys

School of Computing. Dublin City University.

Home      Blog      Teaching      Research      Contact

My big idea: Ancient Brain


CA216      CA249      CA318

CA400      CA651      CA668

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.

Feeds      w2mind.org      ancientbrain.com

On the Internet since 1987.