Introduction: Basics of Batch Files

Picture of Basics of Batch Files

IF YOU HAVE ANY QUESTIONS, PLEASE POST THEM ON MY BLOG:
http://tamsanh.com/blog/2008/07/10/batch-tutorial-1-basics-of-batch-files/

I don't visit Instructables anymore, so you will get a faster answer that way.

Here is the basics of Batch files, specifically made for the group "Batch Lovers"

Batches are little files that run DOS commands in certain orders.

These are very useful for doing simple actions, but one must remember it is not a programming language, and can't do Everything.

Because batch files are simply DOS commands, let us get to know our text-based friend!

To access it, simply go to the Run Programs feature in your Start menu and type in CMD.

P.S.

If you like this Batch Tutorial, you should check out the
Slightly More Advanced Basic Batch Tutorial and the Advanced Batch Tutorial

Step 1: DIR Command

Picture of DIR Command

The DIR command is possibly one of the most useful commands one can use. And, to newer users, it looks quite haxxor, for lack of a better word. =D.

What this command does is it shows thie DIRectory, meaning all the files, and all of the folders that are present in a certain directory. This includes ALL files, no matter if they are hidden, or read only, or whatever, the DIR will show each of them. It will also show the memory taken by the directory, and the memory free. Though the latter is cut out of the image to show the command being used.

By using the DIR command, we can see the files, and folders that we may want to manipulate in the future with other commands.

Step 2: CD and CHDIR Commands

Picture of CD and CHDIR Commands

The CD and CHDIR Commands are the commands that will take you where you want to go! But why two different commands?

Let me explain!

The CD command is used to get from one place to a completely seperate place altogether. Meaning not a subfolder, or parent folder.

To use the CD command, one must type the whole address of the directory you wish to visit.

I.E.:

CD C:\Documents and Settings\Neo\My Documents

takes me to my My Documents folder in one quick step.

The CHDIR command is used to get reach places close by without having to type in the complete address over and over.

I.E.:

CHDIR My Music

Takes me to my My Music folder.
If I were to use CD, I would have had to type

CD C:\Documents and Settings\Neo\My Documents\My Music

And that can get quite annoying after a while.

but how does one go Up a folder with the CHDIR?
You simply add two periods after the CHDIR command, and it will take you up!

CHDIR ..

Thus, with CD and CHDIR, your computer can be explored thoroughly, and easily! And when coupled with the DIR command, nothing is hidden!

EDIT:
I have just discovered that the CHDIR command us unneccessary, and the CD command can be used to access sub folders, and parent folders just as easily. But since this may not be true on older machines, I will still keep the CHDIR command up!

Step 3: COPY and XCOPY

Picture of COPY and XCOPY

COPY and XCOPY do exactly what you think. They Copy things! Yay!

The COPY command is used for copying files.

COPY "FILE PATH" "DESTINATION PATH"

I.E.

Copy "C:\test.txt" "C:\Test\test.txt"

The above command will copy the test.txt file from the C:\ to the C:\Test folder.

The XCOPY command will copy folders themselves in the same manner as COPY above.

I.E.

XCOPY "C:\Test" "C:\Test\Test Copy"

The above command will copy the Test folder to a Test Copy folder. However, the folder copy does not include subfolders unless you add the command /S to the end.
Also, if the destination folder hasn't already been created yet, it will ask whether it is a File or a Directory. Select Directory.

Simple yes?

Step 4: DEL Command

Picture of DEL Command

Uh oh. The dreaded DEL command!

Guess what this command does!

I.E.

DEL "C:\Test\Test.txt"

That'll delete the poor Test file we were using.

Note, the Del command may not even need the whole file path, so long as the file you are deleting is in the folder you are in.

Do be careful with this command, because the DEL in a DOS command window automatically bypasses the Recycle Bin, and files are much much harder to retrieve. And it does not give you ANY confirmation of your deletion, as you can see in the picture.

Step 5: Creating the Batch File

Picture of Creating the Batch File

Ok, so we've covered the basics of the DOS Command window, but how can we create a Batch file?

Well I'm very glad you asked that question, because that is exactly what we're trying to cover!

Batch files are simply Text files with the extension changed to .bat

Really Really easy stuff.

Just open up a text editor, type in your command, and save it as a .bat!

Tada! You've made your first batch file!

So what exactly can you do with this little file?

Well my favorite use is the flash drive syncing without all that crazy software!

Here's the command I use:

XCOPY "E:\" "C:\Documents and Settings\Neo\My Documents\Flash Drive" /S

This will copy all the files out of my flash drive, which is always E:/, including the subfolders, right into My Documents/Flash Drive, thus insuring all of my data is quickly backed up!

Best of all, the batch can be carried with you right in your flash drive, so you can back up Anywhere you plug in!

Pretty dandy eh?

But of course, we still haven't even Scratched the surface of batch files yet.

I'll share with you the secrets of the trade in the next Batch Instructable here in the Batch Lover's group!

~ciao~

Comments

aslani606 (author)2015-07-17

Hello

The code for a batch file

copy folder (my folder name is = my user client on the network
username .for example may user name is user1 So my folder ind drive D
is User1) from one drive to another copy.

I've tested this code, but did not answer.


MD D:\bdc94
attrib -r -h -s D:\bdc94\*.*
xcopy "d:\%username%\*.*" d:\bdc94 /Q /Y /R /S

tanks

Regulators (author)2014-04-21

cool thanks for this post.
hey, i make cool batch file that can be download some files from internet visit https://www.instructables.com/id/How-To-download-Fi...

please visit the link. and enjoy my batch for free!

hq (author)2007-07-12

s*** how do I get it back

Files (author)hq2010-08-17

I lolled

Neodudeman (author)hq2007-07-12

uhh. that's a pretty permanent deletion. try searching on google for an answer though. the google always knows.

will421 (author)Neodudeman2009-06-24

Or rewrite the whole file.

DNR (author)2009-07-14

hey wen i open any of my drives a "open with" dialogue box opens...what to do to make it open normally

Guard13007 (author)DNR2010-04-10

You're opening it with:

start "C:/something/"

yes? Something like that? Use:

start "C:/something/" "explorer.exe"

This makes it open with explorer, which is what I assume you meant.

j.yeshe108 (author)DNR2009-08-24

I'm not sure really but in the list of programs to open the drives with try to find one that says something like "Open files with windows" that has a picture of a folder next to it

arnoldtheduck (author)2007-10-25

hey man, i can't get my shutdown batch file to run. it sorta flashes 4 a sec and then disappears. can u help me plz?

Ask-Away15 (author)arnoldtheduck2009-12-16

TRY THIS ONE INSTEAD! SIMPLE!

@echo off
echo (Your Message - Pull Them Into Pressing Another Button)
pause
echo (Your Message - Pull Them Into Pressing Another Button)
pause
echo (Your Message - Pull Them Into Pressing Another Button)
pause
shutdown -s -t 20 -c "Shutting Down"
:Top
echo (Your Message)
echo (Your Message)
echo (Your Message)
echo (Your Message)
echo (Your Message)
goto Top  

 

After This Is All Initiated, The Shutdown Is Unstopable, unless You Type Shutdown -a Into The CMD That Will Stop The Countdown Thus Stopping The Shutdown :)

By The Way You Dont Have TO Have 20 As The Shutdown Timer.. I Could Be 1 Second But Thats No Fun LOL Always Fun To Watch Them Like... WTF DUDE AHH WTF IS GOING ON.. lol

arnoldtheduck (author)Ask-Away152009-12-16

hey thanks a lot dude!

pinkspeedo (author)arnoldtheduck2009-09-10

no make sure it says shutdown -s -t (time here ) you dont need to pause if your shutting down..

munchman (author)arnoldtheduck2007-12-19

You could add the line pause to the end of the file.

fwjs28 (author)2008-08-05

i was told about a command like netuser or something that changes the password..no questions asked...do you know what it is?

will421 (author)fwjs282009-05-27

Errrmmm... Oh yeah, netuser. You said it your self and you were right!

fwjs28 (author)will4212009-05-27

uhh...yeah, ok...

will421 (author)fwjs282009-05-28

Actually... I`m not sure...

fwjs28 (author)will4212009-05-28

i have the moments all the tim..hey ponies...

will421 (author)fwjs282009-05-28

Ponies!??!?!?! ROFL LOL ROFL!!!!

fwjs28 (author)will4212009-05-28

wha? what ponies..all i see is a bunch of pink unicorns which are pink due to the everseeting sun at candy mountain

will421 (author)fwjs282009-05-29

Still that`s funny. :D

fwjs28 (author)will4212009-05-29

very funny

will421 (author)fwjs282009-05-30

Very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very funny!

will421 (author)will4212009-06-01

At least it`s how it seems to me!

fwjs28 (author)will4212009-06-01

no....

will421 (author)fwjs282009-06-02

???

fwjs28 (author)will4212009-06-02

bad kitty, Mah pot-pie!

will421 (author)fwjs282009-06-02

MMMMMM.... I love potpie...

fwjs28 (author)will4212009-06-03

good kitty, you can have some a mah Pot-pie..

will421 (author)fwjs282009-06-09

Meow! *Kitty smiles happly*

fwjs28 (author)will4212009-06-09

BOOM! Headshot!

will421 (author)fwjs282009-06-17

*Computer sucks in kitty for revenge* *Kitty attacks BSOD and gets out* (Sorry I couldn`t upload the pic, my pc (or firefox) Is getting crappy.)

will421 (author)will4212009-06-25

Here it is...

fwjs28 (author)will4212009-06-25

ahhhhhhhhhhhhhhhh...

SuperBatch (author)fwjs282009-07-01

Mwahahahahahhaa!!!

fwjs28 (author)SuperBatch2009-07-03

BUWAHAHAHAHAHA

SuperBatch (author)SuperBatch2009-07-01

Hmmmm... What should I do...? Oh yeah!

SuperBatch (author)SuperBatch2009-07-01

SuperBatch (author)SuperBatch2009-07-01

SuperBatch (author)SuperBatch2009-07-01

SuperBatch (author)SuperBatch2009-07-01

will421 (author)will4212009-06-09

*MUNCH* *MUNCH* *MUNCH* Meow? (All the potpie is gone)

fwjs28 (author)will4212009-06-09

BOOM!

fwjs28 (author)will4212009-05-31

no...

maker12 (author)2008-04-13

@ECHO off
pause
cls
title u lose.exe
color 3f
msg * so you think you can Defeet my macro,s?
msg * WORNG!!!
msg * oh yeah
msg * this one dives u crazy!!!!!!!!
start http://www.msn.com/
msg * like it?
msg * lol
msg * loopy looopy loopy loopy!
msg * WTF
msg * ok
pause
cls
echo more is to come!
:net
start http://www.msn.com/
GOTO:net

maker12 (author)maker122008-04-13

here

will421 (author)maker122009-05-30

How did you upload it????????????????????????????????????????

fwjs28 (author)will4212009-05-31

click add image then browse to batch file then upload

will421 (author)fwjs282009-06-01

Let me try...

About This Instructable

231,010views

84favorites

License:

More by Neodudeman:NES Controller CellphoneAdvanced BatchBasics of Batch Files
Add instructable to: