UCWare.com

Help topics, tutorials and reviews

How to check if volume has right volume label

I have not found how to check if a volume has right volume label in batch file (.bat) under Windows XP. I need this to automate backup to a flash drive.

So I have coded a tiny command-line utility chkvol.exe. It fetches a volume label for the volume specified in the first command-line argument, and compares the fetched label with string specified in the second command-line argument. Utility returns 0 error code if labels match, and nonzero otherwise.

Use case

set backupv=J
set vlabel=FLASH2G

echo * Trying to mount TrueCrypt volume to %backupv%:
call C:\Tools\TrueCrypt\_mounts.cmd "\Device\Harddisk2\Partition1" %backupv%

VOL %backupv%:
if not %errorlevel% LEQ 0 goto flasherr
echo * Flash drive found!

chkvol %backupv%:\ %vlabel%
if not %errorlevel% LEQ 0 goto labelerr
echo Volume label is '%vlabel%'

Help

usage: chkvol.exe <Root Path Name> <Required Volume Label>
example: chkvol.exe C:\ WINXP

Root Path Name should be a string that contains the root directory of the volume to be described. A trailing backslash is required. For example, you specify \\MyServer\MyShare as \\MyServer\MyShare\, or the C drive as C:\.

Also if /? in the Windows Command Prompt provides article "Performs conditional processing in batch programs". See "ERRORLEVEL" section.

Screenshots

chkvol.exe in use

Download

chkvol.zip ( 28k)

Zip archive contains:

  • chkvol.exe - 32-bit executable for Microsoft Windows operating systems;
  • chkvol.c - the source code in C;

The source code

/*

Copyright (c) 2009 UCWare.com, http://www.ucware.com/.
All rights reserved. Software written by Artem Kuroptev.

This software may be redistributed or modified in any form without 
restrictions as long as the following conditions are met:
1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.
2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.

*/

#include <stdio.h>
#include <windows.h>
#include <winuser.h>
#include <shlwapi.h>

int main(int argc, char *argv[])
{
    PCHAR lpRootPathName;
    PCHAR requiredVolLabel;

    CHAR lpVolumeNameBuffer[MAX_PATH+1];
    CHAR lpFileSystemNameBuffer[MAX_PATH+1];
    BOOL retVal;

    printf("chkvol command-line utility 1.0 (c) 2009 Artem Kuroptev of ucware.com\n\n");

    if(argc < 3)
    {
        printf("usage: chkvol.exe <Root Path Name> <Required Volume Label>\n");
        printf("example: chkvol.exe C:\\ WINXP\n");
        return 1;
    }

    lpRootPathName = argv[1];
    requiredVolLabel = argv[2];

    retVal = GetVolumeInformation(lpRootPathName,
                         lpVolumeNameBuffer,
                         sizeof(lpVolumeNameBuffer),
                         NULL,
                         NULL,
                         NULL,
                         lpFileSystemNameBuffer,
                         sizeof(lpFileSystemNameBuffer)
    );

    if(retVal == 0)
    {
        printf("GetVolumeInformation failed with error %i\n", GetLastError());
        return 2;
    }
    else
    {
        printf("GetVolumeInformation succeed\n");
        printf("Volume label for '%s' is '%s'\n", lpRootPathName, lpVolumeNameBuffer);
    }

    if(StrCmpI(requiredVolLabel, lpVolumeNameBuffer) != 0 )
    {
        printf("Volume label is NOT equial to '%s'. Return code is 3\n", requiredVolLabel);
        return 3;
    }
    else
    {
        printf("Volume label is equial to '%s'. Return code is 0\n", requiredVolLabel);
        return 0;
    }
}

April 26, 2009.
© Artem Kuroptev of UCWare.com, 2009.