USB Auto Update GuideGuest_Jim_* - May 2, 2013
» Discuss this article (4)
Enabling Multiple Computers and Multiple USB Drives (Part 1):
Typically a batch file is a relatively simple set of instructions that are executed from beginning to end. This is not a typical situation though, so we are going to be using a batch file that jumps around itself, and there are reasons for using such a file.
The primary reason I found myself having to do this is that Windows will forget what drive letter it gave a USB drive, so what was J may become K, and that will mess up the updating script. To fix that, we have to use something that will check multiple drive letters. There may be a more elegant way of doing it than I am using, but this works:
set /a var=%var%+1
if %var% EQU 1 goto plug1
if %var% EQU 2 goto plug2
if %var% EQU 3 goto plug3
if %var% EQU 4 goto plug4
Yes, that is a lot of syntax to throw at you, but here is what it means. The '@echo off' command suppresses the batch file from throwing up information I don't want to see. You do not have to use it if you do not want to.
'set var=0' creates the variable 'var' and gives it the initial value of 0. The ':plugcheck' and anything else with a single colon in front of it is a label, which a 'goto' command can send the system to. The 'goto:EOF' command sends the system to the end of the file, effectively exiting the script. The 'if' commands operate as you would expect; if the first thing is true, do this. In this case it is if the variable 'var' equals 1, 2, 3, or 4, goto labels 'plug1,' 'plug2,' 'plug3,' or 'plug4.' At those labels, another variable is created and defined; the 'Drive' variable is set to a drive letter, and we are sent to another label, 'driveexist,' which I will explain next.