Batch Files and Scripts Why Should You Care?
Total Page:16
File Type:pdf, Size:1020Kb
CS140M Fall 2014 Why Should You Care? • Many easy‐to‐use, helpful little programs Batch Files and Scripts • Psychological satisfaction of not being totally under Windows control • Not for everybody but programming can be fun By Al Lake • Make you better prepared to avoid viruses Fall 2014 LBCC CS140M Fall 2014 CS140M Lake 2 General Program Types Common Scripts • Compiled • Batch files – Written in some coding language and then converted to binary – An old format with new power in XP – Executable that interacts with OS directly • VBScript – Examples of languages are C and Java – Related to visual basic and VBA • • Interpreted JavaScript (also JScript) – Common on Internet – Text files that require an interface – Not related to Java • Much slower but easy to write and edit • Generically called “scripts” • Special files with REG extension – Specifically to edit Registry Fall 2014 CS140M Lake 3 Fall 2014 CS140M Lake 4 What are Scripts Used For? What is a Batch File? • Repetitive operations • A text file with extension BAT containing a – System administration sequence of commands – Automation of common tasks • Interpreter is command.com in DOS/Windows 9x/Me • To carry out a series of operations in one step – Commands are DOS plus some additions for • To help with file management variables and branching • To make Registry changes • Interpreter is cmd.exe in XP – Available commands are more versatile than DOS and are 32‐bit Fall 2014 CS140M Lake 5 Fall 2014 CS140M Lake 6 Lake 1 CS140M Fall 2014 Example of Batch File Del Example Deletes one or more files. DEL [/P] [/F] [/S] [/Q] [/A[[:]attributes]] names • “Cleantmp.bat” ERASE [/P] [/F] [/S] [/Q] [/A[[:]attributes]] names names Specifies a list of one or more files or directories. – del C:\windows\temp\*.tmp Wildcards may be used to delete multiple files. If a • Note use of wildcard “*” directory is specified, all files within the directory will be deleted. • More sophisticated version in XP can have /P Prompts for confirmation before deleting each file. switches and long file names /F Force deleting of read‐only files. – del /s/q C:\windows\temp\*.tmp /S Delete specified files from all subdirectories. /Q Quiet mode, do not ask if ok to delete on global wildcard • Could add more lines /A Selects files to delete based on attributes – To clean Internet cache, etc attributes R Read‐only files S System files H Hidden files A Files ready for archiving I Not content indexed Files L Reparse Points ‐ Prefix meaning not Fall 2014 CS140M Lake 7 Fall 2014 CS140M Lake 8 For Backup Arguments (Placeholders) • Can use “xcopy” with various switches • Batch files can accept input • For example – Use placeholders (arguments) – xcopy folder1 folder2 /d:(date) • xcopy %1 %2 • Date format mm‐dd‐yyyy • If batch file called “backup.bat” – Or xcopy folder1 folder2 /u – Enter “backup.bat folder1 folder2” in command – Or xcopy folder1 folder2 /s/u line • Be careful about paths Fall 2014 CS140M Lake 9 Fall 2014 CS140M Lake 10 A Few of the Batch Commands For Help • attrib • assoc, ftype • Enter “help” in command line (not in • copy • findstr Windows Me) • del • pushd, popd • Enter “command /?” as in “xcopy /?” to obtain • dir • reg information on specific command • md • shutdown • In Windows XP Pro enter “hh ntcmds.chm” in • rd • for Start|Run for extensive command line help • ren • goto • xcopy • if Fall 2014 CS140M Lake 11 Fall 2014 CS140M Lake 12 Lake 2 CS140M Fall 2014 Scripts (VBS and JS) VBScripts vs Batch Files • Two interfaces • More oriented to user interaction – Windows Script Host – Can be graphical or command line • For scripts run locally • Has access to file system and Registry • Greater control of other programs – Browser • Has powerful file system functions • For scripts on Internet • Does not have access to file system • Can edit Registry • VBS not read by Netscape (newer version?) • Object oriented • Both can read and pass scripts to the scripting “engine” • Harder to learn and program Fall 2014 CS140M Lake 13 Fall 2014 CS140M Lake 14 Some Useful VBScripts VBScripts and Viruses • Increase icon cache • Some viruses are in form of VBScripts • Scandisk and defrag all drives automatically • Clean temp files • Anti‐virus programs may block use of scripts • Selectively clean “cookies” • Backup folders and files • IE may not allow downloading of attachments • Get product key for Windows with VBS (and other) extensions • Change registered owner • Carry out tasks at shutdown • File association fixes • Various edits to Registry • Add to Context Menu Fall 2014 CS140M Lake 15 Fall 2014 CS140M Lake 16 JavaScript and JScript • Extensively used on Web pages – Hit counters, today’s date, etc – Whole libraries of scripts available for buddingWebmasters • Not used as much for local computers Fall 2014 CS140M Lake 17 Fall 2014 CS140M Lake 18 Lake 3 CS140M Fall 2014 “REG” Files Questions? • Special text files with extension REG • Double‐clicking modifies a section of the Registry • Can add, delete, or modify keys and values • Can download useful Registry tweaks Fall 2014 CS140M Lake 19 Fall 2014 CS140M Lake 20 Assignment • Create a batch file that: • Create a batch file that: – Starts at the root level – Displays up to 4 C:\ programs ECHO Press 'A' to Start the 'A' Program – Changes into ECHO Press 'B' to Start the 'B' Program ECHO Press 'C' to Start the 'C' Program C:\Windows ECHO Press 'D' to Start the 'D' Program – Records all of the files in – Choose 4 Windows the C:\Windows\ into a programs (instead of A, text file. B, C, & D) – Puts the text file at the root level Lake 4.