Intro: Basics of Batch Files
IF YOU HAVE ANY QUESTIONS, PLEASE POST THEM ON MY BLOG:
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.
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
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
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.
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.
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!
Thus, with CD and CHDIR, your computer can be explored thoroughly, and easily! And when coupled with the DIR command, nothing is hidden!
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
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"
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.
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.
Step 4: DEL Command
Uh oh. The dreaded DEL command!
Guess what this command does!
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
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!