PCSTATS     
[X]   Directory of
Guides & Reviews

Beginners Guides
Motherboards by Brand
Weekly Newsletter
Archived Newsletters

+70 MORE Beginner GUIDES....
Beginners Guides: Understanding and Creating Batch Files
Beginners Guides: Understanding and Creating Batch Files - PCSTATS
Batch files can save time by automating actions down to one simple click. A good understanding of what they are, how they work, and how to create your own, is crucial to today's IT force.
Filed under: Beginners Guides Published:  Author: 
External Mfg. Website: PCSTATS Apr 04 2011   M. Dowler  
Home > Reviews > Beginners Guides > PCSTATS

Preparing for your second batch file

Now we are going to create a useful batch file that will copy all the files in your 'c:\testsource' directory into your 'c:\testbackup' directory each time you run it. We'll also make it so that the next time you run that batch file, it will only copy the files that have changed and new files, not every single one in the 'c:\testsource' directory. Ideally you'd use this batch file to copy your files onto a second hard drive or removable media like a USB key, but we'll leave it as it is for now. Feel free to change the target directory to something more useful later.

First we need to create a couple of dummy files in our c:\testsource directory to give us something to work with:

Navigate to your c:\testsource directory in Explorer and right click on the empty space inside. Select 'new\text document.' Call your new text file 'testdoc1'. Now right click again and create a new bitmap image. Call your image 'testbit1'. Your c:\testsource directory should now have the following contents:

Creating your second batch file

Now to create a batch file to backup these files into your c:\testbackup directory automatically. Open up notepad and type the following:

@echo off
xcopy c:\testsource c:\testbackup /m /e /y

The '@echo off' line tells the computer not to display anything onscreen when it runs this batch file.

The second line uses the xcopy command to copy all contents of the c:\testsource' directory to c:\testbackup the first time the batch file is run. The second time and all remaining times, it will only copy new files and files which have changed since it was last run. It will not copy unchanged files which it previously copied, even if you delete the copies it made from the c:\testbackup' directory.

Now save your batch file as 'testbackup.bat' on your desktop and double click it to run the script.

Check the contents of your c:\testbackup directory. It should now have copies of the two files you created in c:\testsource. Good stuff. Now open 'testdoc1' in your c:\testsource directory and add some text then save it.

Run your testbackup.bat batch file again, and go to the 'testdoc1' file in the c:\testbackup folder. It should have been updated with the changes you made in the other folder.

You've now created a useful backup utility with a simple two-line batch file that just takes a double click to run. Starting to see the potential usefulness of knowing your batch files yet?

< Previous Page © 2023 PCSTATS.com
Please respect the time and effort that went into creating each PCSTATS Beginners Guide, do not illegally copy. Thank you.
Next Page >

 

Contents of Article: PCSTATS
 Pg 1.  Beginners Guides: Understanding and Creating Batch Files
 Pg 2.  Creating a BATCH File
 Pg 3.  — Preparing for your second batch file
 Pg 4.  Anatomy of a batch file
 Pg 5.  Third trial batch file: getting fancy
 Pg 6.  Batch file error levels and the goto command

 
Hardware Sections 


 
PCSTATS Network Features Information About Us Contact
FrostyTech
PCSTATS Newsletter
Tech Glossary
Technology WebSite Listings
News Archives
(Review RSS Feed)
Site Map
PCstats Wallpaper
About Us
Privacy Policy
Advertise on PCSTATS

How's Our Driving?
© Copyright 1999-2023 www.pcstats.com All rights reserved. Privacy policy and Terms of Use.