Windows Command Line

Introduction

The exercises which follow form an introduction to using the windows command line. To follow the exercises it is recommended that you use an empty USB thumb drive (any size will do). Plug the drive into an available port and find the drive letter. In the screen shots the drive letter is F: - your drive may be different.

Opening the command prompt

Open the windows command prompt by selecting Accessories→Command Prompt from the Program menu. Alternatively click start→run and type 'cmd' in the 'open' box. Click 'OK' and the command window should open.

Changing Drive

Type the drive letter followed by a colon. Now hit enter to change to the thumb drive.

echo

The first command we'll try is echo. Type 'echo hello world' at the command line. Hit 'enter' and the words 'hello world' will be displayed. To redirect the output type 'echo hello world > hello.txt' and hit enter. The words 'hello world' will be output to a new text file called 'hello.txt'.

type

To view the contents of the text file type 'type hello world' and hit enter.

cls

To clear the screen type 'cls' and hit enter.

typing more than one command

To enter multiple commands on a single line separate commands using an ampersand '&'. The command 'echo goodbye > goodbye.txt & type goodbye.txt'[enter] will echo the word goodbye to a new text file 'goodbye.txt' then display the word 'goodbye' at the command prompt;

dir

To make a list of files type 'dir'[enter]. To list file names only type 'dir /b'[enter]. The letter 'b' after a forward slash is a 'switch'. To display a list of only text files type 'dir /b *.txt'[enter].

help

To find out more about a command type the command followed by a forward slash and a question mark (e.g. 'dir /?').

md

To make a new directory ('folder') use the command md. Here we make two new directories 'hello' and 'goodbye' using the commands 'md hello'[enter] and 'md goodbye'[enter].

move

Move the text files into the directories using the command 'move' by typing 'move hello.txt hello'[enter] and 'move goodbye.txt goodbye'[enter].

subdirectories

List files in subdirectories using 'dir' with the '/s' switch.

for

In the previous examples we have used the commands 'md' and 'move' several times. More efficiently we can make the same command apply more than once using the command 'for' (e.g. 'for %a in (hello,goodbye) do md %a'[enter] or 'for %a in (hello,goodbye) do move %a.txt %a'[enter]). To apply the command recursively use the switch '/r'.

As an alternative to using the command 'dir' we can list the files by typing 'for /r %a in (*.txt) do @echo %~nxa'[enter]. Note the use of the merchant mark '@' to stop command echoing. Note also the use of the tilde followed by the letters 'na'. This tells cmd to echo the txt file name ('n') followed by the file extension ('x').

To display the date/time of the file use '%~ta' and to display the file size in bytes use '%~az'.

csv output

To output file data to a spreadsheet we can use comma separated values (csv). We'll start with some column headings. Type 'echo txtfile,date,size > txtfiles.csv'[enter]. To output file data type 'for /r %a in (*.txt) do echo %~nxa,%~ta,%~za >> txtfiles.csv'[enter]. Note the use of '>>' to append data instead of overwriting.

View txtfiles.csv by typing 'type txtfiles.txt'[enter] or open with Microsoft Excel

renaming files

rename 'goodbye.txt' to 'the long goodbye.txt' by typing the command 'rename goodbye.txt "the long goodbye.txt"'. Note the use of quotes - the new file name contains blank spaces. To avoid using quotes replace blank spaces with an underscore '_' (e.g. 'the_long_goodbye.txt') or use camel case (e.g. 'theLongGoodbye.txt').

deleting files

Delete all txt files in the directory 'goodbye' by typing the command 'del goodbye\*.txt'[enter]. To delete all files in the directory hello type 'del hello'[enter]. Note that this second command asks us if we want to delete all the files in the directory. Files deleted from the command line cannot be retrieved from the recycle bin. There is no safety net. The command 'for /r %a in (c:\*.*) do del "%a"' would permanently delete every file on the c: drive including the operating system and all other files. Dangerous commands like 'del *.*' will flag up a warning. Be careful how you answer.

removing directories

To remove directories use the command 'rd'. To remove the directory 'hello' type 'rd hello'. To remove the directory 'goodbye' type 'rd goodbye'.

remove the remaining csv file

Delete txtfiles.csv with the command 'del *.csv'[enter].

Bibliography

The commands listed above are only a small sample of the many commands that are available from the windows command line. The books listed below will give a more complete description of working with the Windows command line.

    • Windows Xp Command Line [Paperback]
    • Carolyn Z Gillay (Author)
    • Paperback: 928 pages
    • Publisher: Franklin, Beedle & Associates Inc (17 Oct 2002)
    • Language English
    • ISBN-10: 9781887902823
    • ISBN-13: 978-1887902823
    • Windows Command-Line Administrators Pocket Consultant [Paperback]
    • William R. Stanek (Author)
    • Paperback: 416 pages
    • Publisher: MICROSOFT PRESS; 1 edition (1 Mar 2004)
    • Language English
    • ISBN-10: 9780735620384
    • ISBN-13: 978-0735620384

Links

  1. Windows CMD Commands
  2. Microsoft Windows XP - Command-line reference A-Z
  3. Windows Command Line Cheat Sheet [PDF]

Mike Hirst
15:26 23 May 2011