Week 3 - Tools - Windows Scripting to Automate Tools (Windows CLI) Flashcards
List some basic windows commands re the file system
CD (change directory)
MD (make directory)
RD (remove directory)
Dir (list contents of a directory)
List some basic windows commands re file manipulation
-Copy (copy file)
-Xcopy (copy complete directory trees)
-Del (delete)
-Move (move)
-Type (prints the contents of a file to the screen)
-More (cuts output of command into pages for better readability)
List some other useful windows commands
Commands:
-Echo (prints text to screen)
-Type (prints content of a file to the screen)
-Set (prints list of environment variables but you can also define own variables using this)
-Cls (clear screen)
-Doskey (/history prints command history)
Special commands & characters:
- /? (prints help)
- | (inserts the output of the first command into the second)
- > (redirects the output of a command to a text file - always creates a new output file)
-»_space; (as above but appends to the end of a file if it exists
Summarise the two main ways to script in Windows
- Batch scripting.
You create a text file with a list of commands to be executed sequentially.
- can be windows commands
- or external utilities
- basic logic & variables
Easy to learn, straightforward
Text file (e.g.notepad)
batch files are saved as
.bat extension or
.cmd extension
Can start them by double clicking the file in windows explorer or in CL by type their name and press enter.
- Powershell
- Comprehensive programming environment
- uses windows API
- steeper learning curve
We are going to focus on batch scripting
Batch Scripting. EXAMPLE - helloworld.bat
In command line.
- write: echo @echo off > helloworld.bat
this tells the system to display only plain (the echo off part). The > is telling it to output to a new text file called helloworld.bat
- on next line write: echo echo Hello World»_space; helloworld.bat
This puts the text content ‘Hello World’ into the helloworld.bat file. - then use the ‘type’ command to show the contents of the file by writing: type helloworld.bat (it will display the words Hello World)
- then write helloworld.bat and enter and it will write ‘Hello World’ to the screen
Batch Scripting - Variables
Two relevant types of variables:
ENVIRONMENT variables
- ‘set’ command gives list of all environment variables that are on the system.
- set command can also be used to declare your own variables by putting the word ‘set’ then the variable name and = value you want to put in it.
- set /p variable=prompt (interactive)
- %variable% (echo %variable%)
COMMAND LINE PARAMETERS
- batchfile.bat param1 param 2
- param1 to %1 (to get value of parameter 1)
- param2 to %2 (to get value of parameter 2)
Batch scripting - Logic
Commands to bring logic / structureto your batch files
- If (makes the execution of the command depend on a certain condition)
- For (makes it possible to run a comman a specific number of times)
- Rem (remark - makes it possible to put text in your script that explains the logic & the commands that you used - important if you need to make changes later and can allow someone else to read it. )
-Goto (makes it possible to jump to another location in your batch file. that location has to be defined by a label which is :labelname
- Call (makes it possible to call another batch file from within your fiirst file)