USB Auto Update GuideGuest_Jim_* -
» Discuss this article (4)
Enabling Multiple Computers and Multiple USB Drives (Part 2):
if exist "%Drive%\USB Update.bat" goto existYes
if exist "%Drive%\NO USB Update.bat" goto existNo
set filename=USB Update
set filename=NO USB Update
There is actually quite a lot going on in this segment. First the script checks if there is a 'USB Update.bat' on the USB drive. This is something I had not explained before, because it did not need to be explained. My USB drives contain the file 'USB Update.bat,' which contains the instructions of what files and folders to update. They have different names on my computers, which I will get to in a moment. This is actually a remnant of how I first created this updating system, where the file on the USB drive was simply run and done, so having them all named the same simplified things.
Now, however, the system is different as I keep the update instructions on my computers as well. This is actually useful for redundancy (if your drive malfunctions you still have the instruction set to rebuild it) and for editing, as you can then edit the instructions without the drive attached. Of course, if you edit the instructions, you will need to be able to update the file on the drive, which I will explain soon.
The different filename variable values, 'USB Update' and 'NO USB Update' are to allow me to still automatically update the instruction file on a USB drive without having to run the instruction set. Updating the instruction set on the USB device is what the next bit of code does, and it is both important and complicated, so I am going to break it up into three parts within this guide.
First, the segment that gets the USB device's name, which is stored as a comment within its update-instruction set.
for /F "usebackq delims=::" %%B in ("%Drive%\%filename%.bat") do (
That 'for' command is currently configured to read the 'USB Update.bat' or 'NO USB Update.bat' on the USB drive, depending on which file is indeed on the drive, which was determined earlier. The name of the device, such as 'My 4GB,' is commented at the beginning of the instruction by having two colons in front of it, like this '::My 4GB.' (Of course you do not want to include the period in the comment.) Because the delimiter is set to be two colons, when the command reads that comment, it does not include the colons when it sets variable 'B' to the device name it just read.
The 'echo %%B' command causes the batch file to display the name of the device it is reading. This command is not needed, but can be useful so I have it there. As I do not want the next commands shown, I turn echo off again.
The 'device' variable is able to take its value outside of the 'for' command, unlike the 'B' variable, which is why it is used.
Next, the 'batupdate' label, which updates the instruction set on the USB device:
xcopy /y /d "%Drive%\%filename%.bat" "%DataDrive%\USB Items\%device% USB Update.bat"
xcopy /y /d "%DataDrive%\USB Items\%device% USB Update.bat" "%Drive%\%filename%.bat"
Here we use the XCopy command instead of ROBOCOPY just because. We could use ROBOCOPY if we wanted, but XCopy works fine here. The '/y' flag answers yes to the question of if we want to replace a file when we copy. The '/d' flag is like the '/xo' flag for ROBOCOPY and makes sure the newer version of the file is copied over the older, and not the other way around. As this is a process I may want to monitor, I turned 'echo' on to see it, and then off again when it has completed.
Now we get to actually running the update instructions:
echo Updating %device%
if exist "%Drive%\USB Update.bat" (
call "%DataDrive%\USB Items\%Device% USB Update.bat" Drive
First we see that it will tell us what device is being updated. Next, if the drive contains the file 'USB Update.bat' it will call the same file located on my computer. (It could just as easily call the file from the USB drive, but I'm funny and wanted it to call the one from the computer.) The 'call' command allows one batch file to execute another and will wait for it to complete before continuing itself. It is extremely important to include the 'Drive' at the end of the call command because that passes the 'Drive' variable to the called batch file. This way the update instruction knows the correct destination of the files to be copied to the USB drive.
After updating the USB drive, it returns to the 'plugcheck' label explained earlier. This way if multiple USB drives are connected, it will update each of them instead of just the first one it finds.
That's it! That is how you automatically update files on one USB drive of many you own, from any computer you own, even if files are in different places on the different computers. Now, do me a favor and never ask me how long it took to get all of this to work right.
To illustrate what the program goes through, I have embedded an annotated graphic on the next page. It does not follow all of the intricacies of the script, but should help make clear what the general process is through the file.