Author Topic: Batch Scripting  (Read 7605 times)

Batch Scripting
on: January 13, 2012, 01:33:34 AM

This guide is to show you some of the things that are possible in batch scripting.


THIS GUIDE IS IN PROGRESS

Batch scripting is basically a way to automate command line commands and can be used to carry out repetitive commands, set-up computers, diagnose issues all using logic driven

steps. Batch scripting is however not multitasking and has been superseded by vbScripting, however batch scripting is preferred by some due to it's simplicity.

Batch scripts can be run manually, as scheduled tasks or at log-on/log-off as a few examples

PLEASE NOTE: wherever { } is used below, you are expected to remove this and replace with the value relevant to you so {password}, requires you to replace the brackets and change the text in between with your relevant password, this guide is a starter for batch scripting I wont cover every element of each command, further information is available on the Microsoft website.



Contents


1. The Basics

   1.1 How to create a batch file
   1.2 Commenting
   1.3 Echo command
   1.4 Clearing the screen
   1.5 Pausing (User input or time delay)
   1.6 Useful variables
   1.7 Calling another batch file

2. Logic

   2.1 GOTO command
   2.2 IF command

3. Simple Commands

   3.1 Directory Navigation
   3.2 File Commands
   3.3 Output to File

4. Advanced Commands

   4.1 Ping (checking presence of another device)
   4.2 Mapping Network Drives
   4.2 RAS Connections (Dial-up / VPN)

5. Using Programs And Switches

   5.1 SyncToy - Sync data
   5.2 PSShutdown - remotely shutdown a computer

6. Links

   6.1 Other batch scripting resources
   6.1 Example practical scripts

7. Example Mini Scripts




1. The Basics of Batch Scripting


1.1 - How to create a batch file

Creating a batch file is simple, essentially it's a text file with and extension of .bat, so open notepad and save the file {filename}.bat it is always recommended not to use

spaces and stick to 8 characters or less, this will save you some headaches especially on older systems, but is not essential if wish to create yourself some more work.


1.2 - Commenting

Commenting is very useful for keeping track of what different commands are doing, and allowing other people to understand what you having been doing.

The REM command is used which stands for Remark, it's not used by the system at run time, just a method of commenting the code.

Example:

REM Hello world!


1.3 - Echo command

Echo is used to display something on screen, by default all commands run are echoed on screen, so usually my first line of a batch file is:

Code: [Select]
echo off
Then if you want to echo a line of text, in this case "Hello World":

Code: [Select]
echo Hello World
If you want to insert a blank line, to make the on screen presentation better, then use (with the full stop):

Code: [Select]
echo.

1.4 - Clearing the screen

This is only really useful for presentation purposes, but as I like to make the presentation as useful ass the code itself, this is always my second line (to hide the command

echo off from on screen display, and is then used after each activity is completed, before the next echo command. All yo uneed to clear the screen is:

Code: [Select]
cls

1.5 - Pausing (User input or time delay)

The basic pause command will pause until any other button is pressed:

Code: [Select]
pause
A timed pause delay is not supported as a native command, but you have a few options, one is to call an external pause program and pass it a duration, but I'm going to cover a

workaround without having to use another program, because you then have to manage multiple files and their location.

Essentially what I do is ping a non existent IP address 1.1.1.1 and have a wait between sending packets and set the result as not required, the below in ms giving a 10 seconds

delay.

Code: [Select]
ping 1.1.1.1 /n 1 /w 10000 > NUL

1.6 - Variables

Variables are environment or user defined values that you can call and use in your scripts, system defined variables are specific to the current user and system set-up, user defined

variables can be referenced at the start of a batch file and re-used multiple times, so any changes only need to be reflected:

User defined variables are specified by using: set {variable}={value}

An example of a variable called "text1" being set with "Hello World" and then called using echo

Code: [Select]
set text1=Hello World
echo stored text is %text1%

This would display on screen:

stored text is hello world

Below I have listed some environment variables that are useful, and can be called using other commands

%SystemDrive% - System Drive
%SystemRoot% - System Root
%WinDir% - The windows directory
%SystemDirectory% - The system32 directory
%programfiles%    - Program Files folder
%Temp% - The temp directory
%HOMEPATH% - Home folder e.g. Documents and settings
%OS% - The OS that is being used on the host system, can be useful for using different commands using the IF command
%USERNAME% - The current users user name
%USERPROFILE% - The current users user profile folder


1.7 - Calling Another Batch File

One way of saving the effort of scripting and maintaining batch scripts, is to call another batch script.

The first way is to call the script and not return, to do that you just add the file name of the script on it's own line i.e. 'script2.bat'

However using the below you can make the script return to the same point of the original script, which is usually much more useful.

Code: [Select]
call script2.bat


2. Logic


2.1 - Goto

The goto command will take you to any point in the script.

To specify a point to goto use a colon at the start then you can state to goto that point.

Code: [Select]
goto POINTA

:POINTA


2.2 - IF commands

The IF command is used the same sense as any other if command, there are 3 variations below, the first is checking the existance of a file, the second is comparing two strings (the /I flag removes case sensitive checks) and the third actions based on an error, and we'll use that later in the ping command.

Code: [Select]
IF [NOT] EXIST {filename} (command) ELSE (command)

IF [/I] [NOT] item1==item2 command

IF [NOT] ERRORLEVEL number command




3. Simple Commands


3.1 - Directory Navigation

(In progress)
cd
mkdir
%~dp0 - present working directory


3.2 - File commands

del
copy/xcopy
ren
changing drive


3.3 - Output to a file

>
>>




4. Advanced Commands


Still to come

Ping
Mapping Network Drives
RAS


5. Using External Programs


Still to come

SyncToy
shutdown
Last Edit: January 13, 2012, 21:56:47 PM by XEntity #187;

Re: Batch Scripting
Reply #1 on: January 13, 2012, 01:33:55 AM
Reserved in case I need it...

It's still a work in progress, and I'm happy for any suggestions as I'm adding to it as I remember things...

    • Tekforums.net - It's new and improved!
  • Offline Clock'd 0Ne

  • Clockedtastic
  • Posts: 10,887
  • Administrator
  • Hero Member
Re: Batch Scripting
Reply #2 on: January 13, 2012, 09:21:36 AM
I think at the end we should have a useful programs and scripts section with a few examples of short scripts as timesavers for people, e.g. I have a simple Robocopy backup script I use to periodically backup my work recursively.

Re: Batch Scripting
Reply #3 on: January 13, 2012, 09:43:47 AM
No worries I'll use the reserved post for that then ;)

Unless there is a limit on how much I can fit in to one post, then may need to do it as another thread

    • Tekforums.net - It's new and improved!
  • Offline Clock'd 0Ne

  • Clockedtastic
  • Posts: 10,887
  • Administrator
  • Hero Member
Re: Batch Scripting
Reply #4 on: January 13, 2012, 09:48:13 AM
There is a 20,000 character limit on posts, I found this out doing the unRAID guide.

Re: Batch Scripting
Reply #5 on: February 07, 2012, 22:04:08 PM
Hi Guys, just to let you know I haven't entirely forgotten about this I've been busy with other things..

.. so in the mean time anyone visiting, I've found this reference page which has a lot of useful commands and more than happy to answer any questions!

And in this guide I'm hoping to provide some more context around these commands.

http://ss64.com/nt/

Re: Batch Scripting
Reply #6 on: February 08, 2012, 13:00:03 PM
Just noticed this, looks good so far, one thing that might be of help to people for environmental variables is if you type set, it lists the current Variables and what they're set to, and any of them can be surrounded by % signs and used in scripting.  Some of them do need quotes around them as well if there are spaces in the paths etc.

I can do some basic AutoIT scripting so might see if I can put something together when I get a chance as AutoIT's quite handy, but I'll start a new thread for that one.

Re: Batch Scripting
Reply #7 on: February 08, 2012, 18:38:47 PM
Just noticed this, looks good so far, one thing that might be of help to people for environmental variables is if you type set, it lists the current Variables and what they're set to, and any of them can be surrounded by % signs and used in scripting.  Some of them do need quotes around them as well if there are spaces in the paths etc.

I can do some basic AutoIT scripting so might see if I can put something together when I get a chance as AutoIT's quite handy, but I'll start a new thread for that one.

Section 1.6 above has started to cover variables, environment and non environment :)

AutoIT would be good as another guide as that has a slightly different application to batch scripting...


0 Members and 1 Guest are viewing this topic.