USB Auto Update GuideGuest_Jim_* - May 2, 2013
» Discuss this article (4)
Creating the Update Instructions:
The first step we are going to do can be done at almost any point, so why not do it now? Also, it happens to be the simplest.
We are going to create the batch files with the update instructions. Batch files can be written by any text editor, such as Notepad, and have the '.bat' extension. The first thing to do is identify the files and folders you wish to update. For this guide I am going to use a folder of utility programs I keep on my USB drives and the folder in which I store all of my news items.
There are a few different copy commands that a batch file can use, and for this I recommend using the very powerful, ROBOCOPY, or Robust File Copy. Importantly it has the ability to copy just newer files. Other commands, such as XCopy also have this ability, but ROBOCOPY is better designed to work with directories than XCopy.
The flags we will be using with ROBOCOPY are /mir, /xo, /xf, /xd, and possibly /e. Before I get to those, the format of ROBOCOPY is:
robocopy "Source" "Destination"
That means that to copy over my utilities and news item folders the commands will look like this:
robocopy "%DataDrive%\USB Items\Utilities" "U:\Utilities"
robocopy "%JimContent%\Documents\OCC\News Articles" "U:\OCC\News Articles"
Remember how I mentioned my need for Environment Variables? This is their first appearance as both 'DataDrive' and 'JimContent' are Environment Variables. The '%' symbols tells the system to call the variable. I need the variables here because the location of the folders are on different drives between my laptop and desktop. As Environment Variables are local to the specific computer, the same variable name can have different values.
Truthfully, there is another variable I have to use in those commands, but I have not shown it here because we have not gotten to where I am comfortable explaining its use.
As the commands sit right now, they will instruct every file within the named folders to be copied to the USB drive, instead of just those that need to be updated, and no subfolder will be copied. To copy over subfolders we need to use the /e flag. To only copy over newer, updated files, instead of everything, we use the /xo command, which stands for 'exclude old.'
For my purposes, and likely yours, we are not going to use the /e flag, in this case. Instead we are going to use the /mir flag, which has the same effect, and more. This flag stands for 'mirror' and will make the destination folder mirror the source, by copying over subfolders and deleting anything at the destination but not at the source. As I want the USB drive to represent what is on the computer and not be an ever growing backup, I want the /mir flag to remove what I no longer need.
robocopy /mir /xo "%DataDrive%\USB Items\Utilities" "U:\Utilities"
robocopy /mir /xo "%JimContent%\Documents\OCC\News Articles" "U:\OCC\News Articles"
As I said earlier, I have multiple USB drives of different sizes. Let us say this is one of the smaller drives, so I want less to be copied onto it. In that case we will use the /xd and /xf flags which stand for 'exclude directory' and 'exclude file' respectively. These commands do recognize wild cards.
robocopy /mir /xo "%DataDrive%\USB Items\Utilities" "U:\Utilities" /xd "Defrag" "Uninstall"
robocopy /mir /xo "%JimContent%\Documents\OCC\News Articles" "U:\OCC\News Articles" /xf "*.wlx" /xd "*No USB"
I do not actually have directories labeled 'Defrag' and 'Uninstall' but I did not want to name the programs I am actually excluding on my smaller drive. As you can see though, we are able to just list what we want excluded one after another. The 'wlx' files were something you are unlikely to see again as the program that used them is no longer supported. Because there are no spaces in the names we do not need the quotation marks, but I use them out of habit.
At this point, you should be able to go through and generate the batch file(s) you will want to use. To test the batch file(s) I suggest inserting the USB drive you want to update, making sure the destination uses its drive letter, and run the batch file with the command prompt. This way you will be able to watch as the instructions are carried out, and see if any error occurs. Also, be sure you are using 'dummy' files and folders; if you accidentally setup the instructions to delete files, you will not lose anything important.