Web User - UK (2019-10-16)

(Antfer) #1
Weekend Project

Subscribe toWeb User atsubscribe.webuser.co.uk 16 - 29 October 2019 59

explore the full list of built-incommands
atbit.ly/scripts486, but here are some of
the morecommon ones.


@echo–@echo offhides the
commands as they run, while@echo
on itsown shows you theworkings of
your script, which is useful ifyou ha ve
to troubleshoot problems with it.


rem– this is shortfor ‘remark’ and lets
you add notesregardingyour commands.
They aren’texecuted or displayed when
the script runs and only appear whenyou
view the file in atext editor.


pause– we’ve used the pausecommand
in our MiniWorkshop, below. It
introduces a delay, usuallyto allow time
to read something on screen. ‘Press any
key to co ntinue...’ is displayed onscreen
when a pause isreached.


if– this is usedfor conditional
commands – essentially, ‘If Xexists/
doesn’texist, then do Y’.


mkdir– thiscommand makes a directory
and gives it a name ofyour choosing.For
example,mkdir Folder. You can use
themdcommand instead, ifyou prefer.


rmdir– thiscommandremoves
directories.You can userdinstead,
if you wish.


cls– this clears theCommand Prompt
window of anyexistingtext.


ipconfig– this displays information
aboutyour network, including the type
of adapter, IP addresses, default


1

Typenotepadin theWindows
search box and press Enter.The
WindowsNotepad opens,readyfor
you totype in thecommandsyou want
torun. Most batch filesstart with
@echo off,^1 whichstops the script
from displaying thecommands as
they run, including the‘echo off’
command itself.

2

You must enter each of the
commands in the batch file ona
separate line. Giveyour script a nameby
typingtitle, followed bya space and
then the title of the file.^1 Next, type
echofollowed bythe greeting or
statement thatyou wanttoappear
onscreen whenyou run it.^2 On a new
line, typepause.^3

3

You would usually now enter the
othercommandsyou wanttorun
but,for this basicexample,we’ll leave
it here.Savethe filebygoingtoFile,^1
SaveAs, and calling itfirstbatch
.bat(be suretoadd the ‘.bat’
extension).^2 Whenyou double-click
the saved file, the script will run ina
command window.

file in the folder
for %%a in (“.\*”) do (
rem Next we’ll identify if
the file has an extension (but
isn’t this script)
if “%%~xa” NEQ “” if
“%%~dpxa” NEQ “%~dpx0” (
rem Now we’ll check if
there’s a folder for that
extension, and create one if
not
if not exist “%%~xa” mkdir
“%%~xa”
rem Finally we’ll move the
file to its folder
move “%%a” “%%~dpa%%~xa\”
))

The beauty of a batch file is that it runs
in anyfolder you put it in, soyou can
move it to wherever it ’s needed.
This meanswe can save the filewe
crea ted to the desktop and run it there,
then move it to the Downloadsfolder and
run it again, tidying up both locations
with the same script.
In the MiniWorkshop on page61, we
showyou howto crea te a ba tch file that

MINIWORKSHOP|How to create a batch file

gatewaysand so on.

ping– thiscommand letsyou check that
your computer cancommunicate with
anothercomputer or deviceover
a network. It’s handyfor checking if
a website is up or not.

start– one of the more useful
commands,you can use thisto open
a file using its default application.

Use a script totidy your
desktop or downloads folder
The most useful batch filesyou can
crea te are the ones that perform the sort
of menial tasksyou needto tackle
regularly, such as tidying up a messy
desktop by sorting files into theirrelevant
folders.For this,we’ll use theif, mkdir
andmovecommands.
The script looks morecomplicated
than it actually is because of the inclusion
of the%%acommand, but all this means
is ‘all files in thefolder’.
Here’s how the batch file should look:
@echo off
rem First we’ll target every

Run this batch file and it automatically sorts allyour files intorelevant folders

1
1
2
3

1

2
Free download pdf