Help with batch file: run only if K: label is XX ?

gridsystems

New member
Member
Local time
1:09 AM
Messages
31
Hi,
I have three external USB backup disks with some overlapping content. I only plug one at a time and they are all mapped to K: for bacKup. That's just the way I prefer it.

I use three different robocopy batch files to mirror files to the respective drive, and would like to remove the possibility of accidentally launching backup commands for drive 1 when I have drive 2 connected.

So the drives are all on K: but they have different labels. Is there a way to make a conditional statement that checks the label of the connected drive and only continues to the robocopy command if it matches a predefined label?

Or is this not doable in batch files?
 

My Computer My Computer

At a glance

Windows 7 Pro 64-bitIntel i7 95018GBGigabyte Radeon HD7700
Computer Manufacturer/Model Number
built it myself
OS
Windows 7 Pro 64-bit
CPU
Intel i7 950
Motherboard
Asus Sabertooth X58
Memory
18GB
Graphics Card(s)
Gigabyte Radeon HD7700
Sound Card
onboard Realtek HD audio chip
Monitor(s) Displays
2 x Dell U2412M
Screen Resolution
1920x1200
Hard Drives
3 SSD
4 Western Digital Caviar Green
PSU
SeaSonic x650
Case
Fractal Design R3 case
Cooling
Magehalems rev. B CPU cooler
Hi Gridsystems,

Testing a volume label is very do-able in batch. Vol is the command you're looking for.

The Vol command displays a disk's volume label and serial sequence.
E.g.
Code:
Vol K:


To isolate the information you need (i.e. the label only) from this command, wrap it in a for loop. Like this:
Code:
for /f "tokens=6" %%I in (' Vol K: ') do ( set label=%%I)

Edit: [
Or if in case the label has spaces in it, the following is more reliable:
Code:
for /f "tokens=5,*" %%A in (' Vol D: ') do ( set label=%%B& goto :break)
:break
(Note: Yes, volume labels can and will have spaces if they are typed during creation/renaming. Windows Explorer appears to strip them out, but they are actually there should a user input any during volume naming. The above For loop will capture volume labels correctly if spaces in the labels are present.)]

After that, you'd want to set up a bunch of conditional statements to test the label acquired by vol (stored in the variable %LABEL%) and run some command (preferably a goto) for the specific USB label that is detected.
E.g.
Code:
REM Identify volume label and goto batch label accordingly
if %LABEL%==ExampleUsbLabel_1 ( goto :CommandsToRunFor_ExampleUsbLabel_1 )
if %LABEL%==ExampleUsbLabel_2 ( goto :CommandsToRunFor_ExampleUsbLabel_2 )
if "%LABEL%"=="Example Usb Label With Spaces" ( goto :CommandsToRunFor_ExampleUsbLabel_3 )
 
REM Exit if label not identified
echo Invalid label: The label specified does not match the whitelisted criteria
exit /b 1
 
Last edited:

My Computer My Computer

At a glance

Windows 10, Windows 8.1 Pro, Windows 7 Profes...
Computer type
PC/Desktop
OS
Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan
A simpler fix would be to put the batch files on the flash drive then run them from there. No chance of a screw up that way.
 

My Computer My Computer

At a glance

Windows 10 Pro X64Intel Quad Core i7-4770 @ 3.4Ghz16.0GB PC3-12800 DDR3 SDRAM 1600 MHzIntel Integrated HD Graphics
Computer type
PC/Desktop
Computer Manufacturer/Model Number
Lenovo IdeaCenter 450
OS
Windows 10 Pro X64
CPU
Intel Quad Core i7-4770 @ 3.4Ghz
Memory
16.0GB PC3-12800 DDR3 SDRAM 1600 MHz
Graphics Card(s)
Intel Integrated HD Graphics
Sound Card
Realtek HD Audio
Monitor(s) Displays
HP 22" LCD
Screen Resolution
1680 x 1050
Hard Drives
250GB Samsung EVO SATA-3 SSD
2TB Seagate ST2000DM001 SATA-2
1.5TB Seagate ST3150041AS SATA
Keyboard
Dell USB
Mouse
Lenovo USB
Internet Speed
Cable via Road Runner 3MB Upload, 30MB Download
Antivirus
Windows Defender, MBAM Pro, MBAE
Browser
Seamonkey
Other Info
UEFI/GPT
PLDS DVD-RW DH16AERSH
Back
Top