Batch Files and Scripts
Vic Laurie
PPCUG
June 9, 2003
Why Should You Care?
Many easy-to-use, helpful little
programs
Psychological satisfaction of not
being totally under Windows control
Not for everybody but programming
can be fun
Make you better prepared to avoid
viruses
General Program Types
Compiled
Written in some coding language and then
converted to binary
Executable that interacts with OS directly
Examples of languages are C and Java
Interpreted
Text files that require an interface
Much slower but easy to write and edit
Generically called scripts
Common Scripts
Batch files
VBScript
Related to visual basic and VBA
JavaScript (also JScript)
An old format with new power in XP
Common on Internet
Not related to Java
Special files with REG extension
Specifically to edit Registry
What are Scripts Used For?
Repetitive operations
System administration
Automation of common tasks
To carry out a series of operations in
one step
To help with file management
To make Registry changes
What is a Batch File?
A text file with extension BAT
containing a sequence of commands
Interpreter is command.com in
DOS/Windows 9x/Me
Commands are DOS plus some additions
for variables and branching
Interpreter is cmd.exe in XP
Available commands are more versatile
than DOS and are 32-bit
Example of Batch File
Cleantmp.bat
del C:\windows\temp\*.tmp
Note use of wildcard *
More sophisticated version in XP can
have switches and long file names
del /s/q C:\windows\temp\*.tmp
Could add more lines
To clean Internet cache, etc
Another Example
(Windows 9X/Me)
cd C:\windows
deltree /y temp\
deltree /y tempor~1\
deltree /y cookies\
Use del and rd in XP with /s
Batch Files for Clean Up
Fred Langa at
www.langa.com/cleanup_bat.htm
www.langa.com/newsletters/2002/
2002-04-04.htm#2
For Backup
Can use xcopy with various
switches
For example
xcopy folder1 folder2 /d:(date)
Date format mm-dd-yyyy
Or xcopy folder1 folder2 /u
Or xcopy folder1 folder2 /s/u
Reference for Backup Files
Fred Langa at
www.langa.com/backups/backups.htm
Arguments (Placeholders)
Batch files can accept input
Use placeholders (arguments)
xcopy %1 %2
If batch file called backup.bat
Enter backup.bat folder1 folder2 in
command line
Be careful about paths
A Few of the Commands
attrib
copy
del
dir
md
rd
ren
xcopy
assoc, ftype
findstr
pushd, popd
reg
shutdown
for
goto
if
For Help
Enter help in command line (not in
Windows Me)
Enter command /? as in xcopy /?
to obtain information on specific
command
In Windows XP Pro enter hh
ntcmds.chm in Start|Run for
extensive command line help
Some References
http://www.microsoft.com/technet/t
reeview/default.asp?url=/TechNet/p
rodtechnol/winxppro/proddocs/batch
.asp
http://www.microsoft.com/technet/t
reeview/default.asp?url=/technet/p
rodtechnol/winxppro/proddocs/ntcmd
s.asp
Scripts (VBS and JS)
Two interfaces
Windows Script Host
For scripts run locally
Has access to file system and Registry
Browser
For scripts on Internet
Does not have access to file system
VBS not read by Netscape (newer version?)
Both can read and pass scripts to the
scripting engine
VBScripts vs Batch Files
More oriented to user interaction
Can be graphical or command line
Greater control of other programs
Has powerful file system functions
Can edit Registry
Object oriented
Harder to learn and program
Some Useful VBScripts
Increase icon cache
Scandisk and defrag all drives automatically
Clean temp files
Selectively clean cookies
Backup folders and files
Get product key for Windows
Change registered owner
Carry out tasks at shutdown
File association fixes
Various edits to Registry
Add to Context Menu
Some Sources of VBScripts
http://billsway.com/vbspage/
http://www.dougknox.com/
http://www.kellys-korner-xp.com/x
p_tweaks.htm
Over 200 tweaks for XP
'ScanFragAll.vbs - ScanDisk and DeFrag
on all hard drives.
' Bill James - billjames.geo@yahoo.com
- rev 10 Oct 1999
Option Explicit
Dim WshShell
Set WshShell =
WScript.CreateObject("WScript.Shell")
WshShell.Run "scandskw /all /n
/silent",1,true
WshShell.Run "defrag /all
/noprompt",1,true
VBScripts and Viruses
Some viruses are in form of
VBScripts
Anti-virus programs may block use
of scripts
OE 6 may not allow downloading of
attachments with VBS (and other)
extensions
JavaScript and JScript
Extensively used on Web pages
Hit counters, todays date, etc
Whole libraries of scripts available for
buddingWebmasters
Not used as much for local
computers
Bookmarklets
http://www.bookmarklets.com/
Very short JavaScripts that can be
added to Favorites
Add useful functions to browser
REG Files
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
REGEDIT4
[HKEY_LOCAL_MACHINE\Software\Micro
soft\Windows\CurrentVersion\explor
er]
"Max Cached Icons"="4096"
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes
\AllFilesystemObjects\shellex]
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes
\AllFilesystemObjects\shellex\ContextMenuHandlers]
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes
\AllFilesystemObjects\shellex\ContextMenuHandlers\Copy To]
@="{C2FBB630-2971-11D1-A18C-00C04FD75D13}"
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes
\AllFilesystemObjects\shellex\ContextMenuHandlers\Move To]
@="{C2FBB631-2971-11D1-A18C-00C04FD75D13}"
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes
\AllFilesystemObjects\shellex\ContextMenuHandlers\Send To]
@="{7BA4C740-9E81-11CF-99D3-00AA004AE837}"
Sources of REG Files
http://www.aumha.org/regfiles.htm
http://home.satx.rr.com/badour/htm
l/reg_files.html
http://www.kellyskorner.com/registry_fixes.htm