# Solved: Diskpart Batch // Script Question



## Unktone

I am completely new to script writing. I used to make simple batch files, and that is my only experience with them.

What I really want to do is create a batch file that will set my external driver (disk 2) as an active partition when I run it...also, I want to make one to make it inactive. I have been trying this.

In my .bat file i've got 

diskpart /s c:\diskpart.txt


"diskpart.txt" being where I have these commands:

select disk 2
select partition 1
active
exit


when I run it it just looks like it's repeating the "diskpart /s c:\diskpart.txt" over and over, and does nothing.

Since I am so ignorant with the way this has to work, if you could show me exactly what I have to write for it to work, that would be amazing. Any help would be wonderful.

thanks!

Rob


----------



## Unktone

Ok, I specified the directory of my script file in the bat file and ran it as an administrator, and it works!! I also made one to make the drive inactive. However, I can't seem to get the permissions to run the bat as admin all the time without having to right click on it. Also, I was thinking about putting the bats and scripts on the drive itself, but realized that my computers have different amounts of drives, so the commands to select drive 2 wouldn't work all the time. 

Any sujestions on how to make this more versatile and less clickies??


----------



## helpful

> when I run it it just looks like it's repeating the "diskpart /s c:\diskpart.txt" over and over, and does nothing.


you can not save the batch file as the same name as diskpart, otherwise you actually calling the script from within the script in an endless loop.

just rename the batchfile to diskpart_automated.bat (or other name) to resolve the issue


----------



## Unktone

Thank you!

That was one of my problems, stumbled away from it on accident, but your explanation makes it sound.

now the file is "Activate USB drive.bat" and it's calling c:\users\username\desktop\activate.txt
"Deactivate USB drive.bat" is calling c:\users\username\desktop\deactivate.txt


----------



## helpful

> Any sujestions on how to make this more versatile and less clickies??


Maybe label the partition you are trying to activate. That way you an parse the output of list volume to activate the correct partition on all systems.


----------



## Unktone

I can select volumes by drive letter, but not name, therefore, unfortunately the drive will only come up as a specific letter if I had previously configured it to that letter on that specific computer. It's almost looking like this batch and script are kind of redundant for the application of being able to have a portable hard drive be changed from bootable to non-bootable with as few clicks possible. I guess It is nice to have a shortcut on systems I use regularly, but on the systems I use regularly it is a rarity to need to boot from my USB drive  It would take a matter of seconds to open up diskpart and active/inactive if I was going to boot it up on another system at this point. but thank you for all your help! It was cool fiddling around with the batch and script stuff.

I am really new though so if I am missing something, and there might be a way to get a one/two click bat to do this action on multiple computers with multiple hard drive configurations, then let me know! 

Thanks,

Rob


----------



## TheOutcaste

Why do you need to keep changing it from Active to Inactive? Only reason I can think of is if you have multiple bootable USB drives connected at the same time and need some way to select which to boot from.

Unless you have USB Boot set as the first device in the BIOS, leaving it active won't make a difference. And most systems have a key you can press at boot to select the boot device, Dell often uses F12, my HP laptop uses F9. With this you can just leave it Active, and use the Boot Menu to select USB when needed.

To avoid having to right click and click Run as Administrator, create a shortcut to the batch file and set it to *Run as Administrator*, then use the shortcuts:
Right click the shortcut, click *Properties*
On the *Shortcut* tab, click the *Advanced* button
Check the box for *Run as Administrator*.

If you give the drive a label, you can read that in Diskpart to determine the disk number, then use that to select the drive.
To assign a label, right click the disk in Windows Explorer, click *Properties*.
On the *General* tab type a label into the box. Note that diskpart will only display the first 11 characters, so make sure they are unique. If that might be a problem, you can always check the drive type to make sure you are only selecting USB drives, or better yet, the Drive ID. You could have 2 USB drives with the same label, but the odds of having two with the same Drive ID is about 1 in 4,294,967,294.

For example, my external is labeled *External 500 GB USB*, and here's the output of list volume on my system:


Code:


DISKPART> list vol

  Volume ###  Ltr  Label        Fs     Type        Size     Status     Info
  ----------  ---  -----------  -----  ----------  -------  ---------  --------
  Volume 0     F                       DVD-ROM         0 B  No Media
  Volume 1     E   Vista        NTFS   Partition    483 GB  Healthy    System
  Volume 2     D   HP_RECOVERY  NTFS   Partition     12 GB  Healthy
  Volume 3     C   Windows 7    NTFS   Partition    100 GB  Healthy    Boot
  Volume 4     I   RBST_BACK8G  FAT32  Removable   7636 MB  Healthy
  Volume 5     L   [B][COLOR=Red]External 50[/COLOR][/B]  NTFS   Partition    443 GB  Healthy

DISKPART>

This batch file will find that label no matter what the disk number, select that disk and set partition 1 active. Just type the drive label on line 8
It also makes sure you are running in Admin account (XP/2K3) or an Elevated Command Prompt (Vista and later):


Code:


@Echo %dbg%Off
::
:: Find External drive and set it active
:: Lists Disk Information using Diskpart
::
SetLocal EnableDelayedExpansion
:: Type the frst 11 characters of the label of the drive here. 
[B]Set _FindLabel=[COLOR=Red]External 50[/COLOR][/B]
Call :_InitVars "%_FindLabel%"
"%SystemRoot%\system32\FSUTIL.exe">Nul 2>&1||Goto _NotAdmin
Echo.
Echo.Please wait, gathering info on the installed drives
Echo.
>"%_Dscr1%" Echo.List disk
For /F "Tokens=2" %%I In ('Diskpart /S "%_Dscr1%"^|Findstr /I /R /C:"Disk [0-9]"') Do (
(Echo.Select Disk %%I
Echo.Detail Disk)>>"%_OFile1%"
)
For /F "Tokens=1,2,3*" %%I In ('Diskpart /S "%_OFile1%"^|Findstr /I /R /C:"Disk [0-9]" /C:"Volume [0-9]"') Do (
  If /I %%I==Disk (
    Set _Tmp=%%J:
  ) Else (
    Set _Label=%%L
    Set _Label=!_Label:~,11!
    >>"%_OFile2%" Echo.!_Tmp!%%J:!_Label!
  ))
If Exist "%_OFile1%" Del "%_OFile1%"
For /F "Usebackq Tokens=1-3 Delims=:" %%I In ("%_OFile2%") Do (
Set _Label=%%K
Set _Label=!_Label:~,11!
If "!_Label!"=="%_FindLabel%" (Set _Disk=%%I) & (Set _Label=%%K) & Goto _FDisk
>>"%_OFile1%" Echo.Volume %%J on Disk %%I has the Label %%K
)
Echo.
Echo.There is no drive connected that has the label of "%_FindLabel%"
Echo.These are the currently connected volumes:
Type "%_OFile1%"
:_Exit
Echo.
Pause
Goto _Cleanup
:_FDisk
(Echo.Select disk %_Disk%
Echo.Select Partition 1
Echo.Active)>"%_Dscr1%"
Diskpart /S "%_Dscr1%"
:_Cleanup
For %%I In ("%_Dscr1%" "%_OFile1%" "%_OFile2%") Do Del %%I>Nul 2>&1
Goto :EOF
::::::::::::::::::::::::::::::::::::::::::::::::::::::
::           Subroutines
::::::::::::::::::::::::::::::::::::::::::::::::::::::
:_NotAdmin
Ver|Findstr /I /C:"Version 5">Nul
If %Errorlevel%==0 (Set _Tmp1=5) & (Set _Tmp=a Computer Administrator account)
Ver|Findstr /I /C:"Version 6">Nul
If %Errorlevel%==0 (Set _Tmp1=6) & (Set _Tmp=an Elevated Command Prompt)
Echo.
Echo.This program must be run from %_Tmp%.
If %_Tmp1%==6 Echo.Please Right click the file, then click Run as Administrator
Echo.Exiting program
Goto _Exit
:_InitVars
For /F "Tokens=1 Delims==" %%I In ('Set _ 2^>Nul') Do Set %%I=
Set _Dscr1=%temp%\dpscr1.txt
Set _OFile1=%temp%\_OFile1.txt
Set _OFile2=%temp%\_OFile2.txt
Set _FindLabel=%~1
Set _FindLabel=%_FindLabel:~,11%
Call :_Cleanup

You can use a 2nd file and just change line 45 so it is Inactive.
It's also possible to have it check the current state and just toggle it. Takes a bit more code of course


----------



## Unktone

> Why do you need to keep changing it from Active to Inactive? Only reason I can think of is if you have multiple bootable USB drives connected at the same time and need some way to select which to boot from.


It just seemed like it would be handy, if I went to a computer, plugged my drive in, and just clicked my batch to make it active (because if I forget that it's plugged into my computer at home, the BIOS is set to look for a bootable USB first, so it would bring up the windows 7 AIK instead of loading my OS when the USB is active). So I figured it would just be a little handy tool to have to turn it on and off.

Also, my professor said he would raise my homework grade from last week from a 95 to a 100 if I did it 

thank you for your answers, man. VERY informative!


----------



## TheOutcaste

Unktone said:


> ...the BIOS is set to look for a bootable USB first...


Most systems don't have USB as the first device, but if yours is set that way that explains it.


Unktone said:


> Also, my professor said he would raise my homework grade from last week from a 95 to a 100 if I did it
> 
> thank you for your answers, man. VERY informative!


Didn't know I was doing homework!

Just be sure you understand what it's doing; your professor may ask for a line by line description


----------



## Unktone

Yeah, haha, I guess it was special circumstances...I didn't have any dvd-r's around so I had to image win7 on my USB and install it that way (it had been a year since my first fresh installation of the OS, and I misplaced my disks), so I set it to that. 

haha, it definitely wasn't "homework", the class itself only covers windows 7 in a nutshell; no scripting or anything. I am kind of beyond it, but there was no test-out option, so just kind of jesting i asked if I could get that homework grade raised by 5 points (it was like 20 questions about windows easy transer, AIK, PE 3.0, and Readyboost stuff) if I made the script. Don't worry, I am going to show him what I made first, then show him the answer you gave me because he knows I am a low-level scripter  Thanks again for all of your help, I would call this solved!

-Rob


----------



## TheOutcaste

You're Welcome!

If your issue has been resolved you (and ONLY you) can mark this thread Solved by using the Mark Solved button at the Top Left of this thread (above the first post) 










Jerry


----------

