Improve company productivity with a Business Account.Sign Up

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2965
  • Last Modified:

Single Diskpart script with multiple diskpart commands

Hello Everyone,

I found  the following batch script that runs diskpart on a Win 2k3 SP2 machine and looks for specific volume labels then returns the volume number that I use later in the script to assign drive letters to those volume numbers.

I have found a way to consistently output the data below to text and parse the text file for the variables I need  - the problem then becomes that I use the variables to then run diskpart again - of course at this point, the volume numbers change and the variables that I just created are no longer valid.

I would like to know if there is a way to run all of this so that I am only running a single diskpart command to keep the volume data from changing.

Here is the script (sorry - it's ugly):

set SCRIPTLOC=d:\scripts

echo Assigning Drive Letters
:: Create a script file to be used by the for loops
      echo list volume > %SCRIPTLOC%\Listvols.tmp

:: Parse the output from 'Diskpart> list volume' for drives
diskpart /s %SCRIPTLOC%\Listvols.tmp > %SCRIPTLOC%\diskpart.tmp 2>&1

      echo Checking for volume SysDB...
      FOR /F "tokens=1-4" %%a IN ('type %SCRIPTLOC%\diskpart.tmp') DO @IF /I "%%c" == "SysDB" @SET XDrive=%%b
      echo Checking for volume Quorum...
      FOR /F "tokens=1-4" %%a IN ('type %SCRIPTLOC%\diskpart.tmp') DO @IF /I "%%c" == "Quorum" @SET MDrive=%%b

      echo Checking for volume DbBLogs...
      FOR /F "tokens=1-4" %%a IN ('type %SCRIPTLOC%\diskpart.tmp') DO @IF "%%c" == "DbBLogs" @SET WDrive=%%b

      echo Checking for volume DbB...
      FOR /F "tokens=1-4" %%a IN ('type %SCRIPTLOC%\diskpart.tmp') DO @IF "%%c" == "DbB" @SET LDrive=%%b

      echo Checking for volume DbALogs...
      FOR /F "tokens=1-4" %%a IN ('type %SCRIPTLOC%\diskpart.tmp') DO @IF /I "%%c" == "DbALogs" @SET RDrive=%%b

:: Create a script file to be used by Diskpart
      echo. > %SCRIPTLOC%\ChangeDrive.tmp

      IF DEFINED XDrive echo select volume %XDrive% >> %SCRIPTLOC%\ChangeDrive.tmp
      IF DEFINED XDrive echo assign letter=X: >> %SCRIPTLOC%\ChangeDrive.tmp

      IF DEFINED MDrive echo select volume %MDrive% >> %SCRIPTLOC%\ChangeDrive.tmp
      IF DEFINED MDrive echo assign letter=M: >> %SCRIPTLOC%\ChangeDrive.tmp

      IF DEFINED WDrive echo select volume %WDrive% >> %SCRIPTLOC%\ChangeDrive.tmp
      IF DEFINED WDrive echo assign letter=W: >> %SCRIPTLOC%\ChangeDrive.tmp
      IF DEFINED LDrive echo select volume %LDrive% >> %SCRIPTLOC%\ChangeDrive.tmp
      IF DEFINED LDrive echo assign letter=L: >> %SCRIPTLOC%\ChangeDrive.tmp

      IF DEFINED RDrive echo select volume %RDrive% >> %SCRIPTLOC%\ChangeDrive.tmp
      IF DEFINED RDrive echo assign letter=R: >> %SCRIPTLOC%\ChangeDrive.tmp
:: Run diskpart using the new script file
      diskpart /s %SCRIPTLOC%\ChangeDrive.tmp

Thanks in advance!!!!
  • 3
1 Solution
If I understand you correctly the volume numbers change when you run your script and all volumes don't get the correct drive letter?

You should use multiple diskpart to get an accurate data just before changing the drive letter.

Give my adjusted script a try.
@echo off
set SCRIPTLOC=d:\scripts

set DriveAndLabel=X:SysDB M:Quorum W:DbBLogs L:DbB R:DbALogs

echo Assigning Drive Letters
:: Create a script file to be used by the for loops
echo list volume > %SCRIPTLOC%\Listvols.tmp

for %%y in (%DriveAndLabel%) do (
    for /F "tokens=1,2 delims=:" %%a in ('echo %%y') do (
        echo Checking for label %%b to assign letter %%a:
        :: Parse the output from 'Diskpart> list volume' for specific volume
        FOR /F "tokens=1-4" %%c IN ('diskpart /s %SCRIPTLOC%\Listvols.tmp ^| find /i " %%b "') do (
            if /i "%%a" == "%%e" (
                ECHO %%b is already assigned to %%a:
            ) else (
                ECHO Assigning %%b to %%a:
                echo. > %SCRIPTLOC%\ChangeDrive.tmp

                echo select volume %%d >> %SCRIPTLOC%\ChangeDrive.tmp
                echo assign letter=%%a: >> %SCRIPTLOC%\ChangeDrive.tmp

                :: Run diskpart using the new script file for current volume we're looking for
                diskpart /s %SCRIPTLOC%\ChangeDrive.tmp

Open in new window

james_higgsAuthor Commented:
Thanks very much for the reply AmazingTech...

I think the problem will be that it is running diskpart more than once so there is no guarantee that the disks associated with the volumes will remain constant (I remove drive letters in a previous step, so drive letters won't be associated prior - my apologies that I didn't mention) - so when you run the first diskpart command, it may parse volume with label Quorum as disk 3 - when the second diskpart command is run to assign disk 3 to the drive letter M, it is possible that now disk 3 is actually DbB (I hope this makes sense).  This behavior can be seen by opening diskpart, running the list volume command, taking note of the details, close window and then re-run diskpart again.

I have actually figured out a workaround - I open diskpart from the script (start diskpart)  before running my commands  - what this does is keeps the disk/volume information static for any subsequent diskpart commands - then when I'm done running all my diskpart commands, I use taskkill to close the original diskpart window.  Not pretty, but it works.

Thanks again for the effort and reply!!!!!!
james_higgsAuthor Commented:
Since there is a working workaround, I don't want anyone to spend any more time trying to resolve
james_higgsAuthor Commented:
solution does not answer question but is a workaround
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now