Windows 7 Forums
Welcome to Windows 7 Forums. Our forum is dedicated to helping you find support and solutions for any problems regarding your Windows 7 PC be it Dell, HP, Acer, Asus or a custom build. We also provide an extensive Windows 7 tutorial section that covers a wide range of tips and tricks.

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

13 Dec 2014   #1

Windows 7 Pro 64-bit
Help with batch file: run only if K: label is XX ?

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 System SpecsSystem Spec
13 Dec 2014   #2

Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan

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.
Vol K:

To isolate the information you need (i.e. the label only) from this command, wrap it in a for loop. Like this:
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:
for /f "tokens=5,*" %%A in (' Vol D: ') do ( set label=%%B& goto :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.
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
My System SpecsSystem Spec
14 Dec 2014   #3

Windows 10 Pro X64

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 System SpecsSystem Spec


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

Thread Tools

Similar help and support threads
Thread Forum
Batch File .bat to copy file from File Sharing & FTP Hosting
Hi all, Any idea how to write Batch File .bat to copy file from File Sharing & FTP Hosting to local directory. Current the File Sharing & FTP Hosting is SMARTFILE. I don't want to use WINSCP as the connector. Thanks
General Discussion
How to run a batch file at logoff & some batch scripting help please
Hiya, I'd just like to run a simple batch file at logoff to backup some settings. I plan to use Robocopy, which is what i use to more thorough backups. Two questions: 1. How make it run by clicking Start > Shutdown? (i realise i could make a shutdown batch file and run it instead, but i'd...
Backup and Restore
Batch File that grabs file names from folder to insert into batch file
Ok I'm totally at a loss on how to word this so I'm just going to write it out the best that I can... :confused: First I have a batch file that I'm writing that will convert mp4 files to mp3 files (I have a lot) for a buddies band using ffmpeg. Here's the code that I'm using: ffmpeg -i...
General Discussion
File Label program for Windows 7 ?
Hello, I'm looking for a program that can label files and folders just like in Mac. When you want some files or folders to be highlighted all the time, you set them colors, give them extra labels or anything that make them look different. Does anyone here know such a program? Thanks
How to create a batch file that will move files based on the file type
How to create a batch file that will move files based on the file type? My Requirement : I need a batch file to copy all .doc files in all my HardDisk to a removable disk say (Z: drive) . Can anyone make it possible using a batch file?Please...Its urgent...
General Discussion

Our Sites

Site Links

About Us

Find Us

Windows 7 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 7" and related materials are trademarks of Microsoft Corp.

Designer Media Ltd

All times are GMT -5. The time now is 16:33.
Twitter Facebook Google+ Seven Forums iOS App Seven Forums Android App