Batch file to switch active partitions

Monopoly money comes in many flavors: 7, Vista, XP, 2K, ME, 98, etc.

Moderators: Flying Fox, Ryu Connor

Batch file to switch active partitions

Postposted on Tue Apr 30, 2013 8:54 am

I want to create two batch files to automate seting one of two primary partitions. I appreciate if anyone would help me.
What the batch file needs to replace is the following series of manual commands:

diskpart
list disk
select disk 0
list partition
select partition 1
active
churin
Gerbil XP
 
Posts: 410
Joined: Wed Nov 28, 2007 3:38 pm
Location: Atlanta/U.S.A.

Re: Batch file to switch active partitions

Postposted on Tue Apr 30, 2013 10:25 am

Checkout Technet on diskpart.

batchfile.bat:
Code: Select all
diskpart /s scriptname.txt


Scriptname.txt:
Code: Select all
list disk
select disk 0
list partition
select partition 1
active
Flatland_Spider
Gerbil Elite
 
Posts: 721
Joined: Mon Sep 13, 2004 7:33 pm
Location: The 918/539

Re: Batch file to switch active partitions

Postposted on Tue Apr 30, 2013 11:28 am

Totally no help to you, but you might as well get rid of the two 'list' commands. Don't see any reason for them in a batch file.
If we cannot be free, at least we can be cheap! FZ.
satchmobob
Gerbil Team Leader
 
Posts: 212
Joined: Tue Oct 15, 2002 1:42 pm
Location: Worcester, UK

Re: Batch file to switch active partitions

Postposted on Tue Apr 30, 2013 2:08 pm

Flatland_Spider wrote:Checkout Technet on diskpart.

batchfile.bat:
Code: Select all
diskpart /s scriptname.txt


Scriptname.txt:
Code: Select all
list disk
select disk 0
list partition
select partition 1
active

I am not sure what statements are supposed to be in the batch file. I need further help.
churin
Gerbil XP
 
Posts: 410
Joined: Wed Nov 28, 2007 3:38 pm
Location: Atlanta/U.S.A.

Re: Batch file to switch active partitions

Postposted on Tue Apr 30, 2013 3:59 pm

Make a file called batchfile.bat

Inside of this file, put the following line:

diskpart /s scriptname.txt

Now save the file and close it. Now, make another file called scriptname.txt

Inside of this file, put the following lines:

list disk
select disk 0
list partition
select partition 1
active

Now save the file and close it. As long as both files are in the same location, when you run batchfile.bat it will work.
Scrotos
Graphmaster Gerbil
 
Posts: 1009
Joined: Tue Oct 02, 2007 11:57 am
Location: Denver, CO.

Re: Batch file to switch active partitions

Postposted on Tue Apr 30, 2013 5:35 pm

Scrotos wrote:Make a file called batchfile.bat

Inside of this file, put the following line:

diskpart /s scriptname.txt

Now save the file and close it. Now, make another file called scriptname.txt

Inside of this file, put the following lines:

list disk
select disk 0
list partition
select partition 1
active

Now save the file and close it. As long as both files are in the same location, when you run batchfile.bat it will work.

I think I tried as suggested but it did not change active partition. The following shows how the files I created look.
Image
The following is a command session to switch active partition from partition 1 to partition 2, which shows exactly what I want to do.
Image
churin
Gerbil XP
 
Posts: 410
Joined: Wed Nov 28, 2007 3:38 pm
Location: Atlanta/U.S.A.

Re: Batch file to switch active partitions

Postposted on Tue Apr 30, 2013 5:40 pm

You named the file scriptname..txt instead of scriptname.txt

Maybe that's the issue?
Scrotos
Graphmaster Gerbil
 
Posts: 1009
Joined: Tue Oct 02, 2007 11:57 am
Location: Denver, CO.

Re: Batch file to switch active partitions

Postposted on Tue Apr 30, 2013 6:35 pm

Scrotos wrote:You named the file scriptname..txt instead of scriptname.txt

Maybe that's the issue?

Yes, I have found it be the issue. I corrected it as is pointed out and now the batch file does exactly what I wanted to do. Thank you very much for your help.

To use the batch file as is I have to create two different versions of batch files and scriptname files, one set for activating partition 1 and another set for partition 2.

Now, I wonder if there is a way to combine these two sets but I am aware the process cannot be all automatic but requires manual user input to specify which of the two partitions to activate.
churin
Gerbil XP
 
Posts: 410
Joined: Wed Nov 28, 2007 3:38 pm
Location: Atlanta/U.S.A.

Re: Batch file to switch active partitions

Postposted on Tue Apr 30, 2013 8:05 pm

I used to have so much fun with batch files back in the dos days. The echo command, the @ sign for hiding certain lines to make clean menus. I even made a woman I was dating a multiple choice questions game with batch files.
confusedpenguin
Gerbil
 
Posts: 53
Joined: Mon Nov 14, 2011 11:50 pm
Location: Potato State

Re: Batch file to switch active partitions

Postposted on Wed May 01, 2013 9:15 am

Cute!

Ok, since this is now the #1 Google result for "check active partition batch" I thought I'd throw in some other stuff I found to be neat. You may or may not be able to use it.

http://social.technet.microsoft.com/For ... 0975c49d4/

Oberwald wrote this and I modified it slightly, changing "diskpart.scr" to "dpscript.txt". The batch file assumes that you have only one disk:
Code: Select all
@echo off
call :CreateScript
for /F "tokens=1-4" %%a in ('diskpart ^< dpscript.txt ^| find "*"') do echo %%b %%c Drive %%d:
goto :eof

:CreateScript
for /F %%a in ('mountvol.exe ^| find /c ":\"') do set PCount=%%a
set PNumber=1
echo > dpscript.txt select disk 0
:Loop
  echo >> dpscript.txt select partition %PNumber%
  echo >> dpscript.txt detail partition
  set /a PNumber=%PNumber%+1
  if %PNumber% LEQ %PCount% goto Loop
echo >> dpscript.txt exit



And this is a crazy work of art by TheOutcaste that I haven't really delved into:

http://forums.techguy.org/software-deve ... stion.html

Code: Select all
@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.
Set _FindLabel=External 50
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



For examples, here's what I have in my system now:

Code: Select all
DISKPART> list vol

  Volume ###  Ltr  Label        Fs     Type        Size     Status     Info
  ----------  ---  -----------  -----  ----------  -------  ---------  --------
  Volume 0     F                       DVD-ROM         0 B  No Media
  Volume 1         SYSTEM       NTFS   Partition    380 MB  Healthy    System
  Volume 2     C   OS           NTFS   Partition    229 GB  Healthy    Boot
  Volume 3     D   HP_RECOVERY  NTFS   Partition      8 GB  Healthy
  Volume 4         HP_TOOLS     FAT32  Partition    380 MB  Healthy    Hidden
  Volume 5     E   Storage      NTFS   Partition    465 GB  Healthy



And the output of the first batch:

C:\temp>test
Volume 1 Drive SYSTEM:
Volume 2 Drive C:
Volume 3 Drive D:
Volume 4 Drive HP_TOOLS:


And the output of the second batch:

C:\temp>test2

Please wait, gathering info on the installed drives


There is no drive connected that has the label of "External 50"
These are the currently connected volumes:
Volume 1 on Disk 0 has the Label NTFS Part
Volume 2 on Disk 0 has the Label OS
Volume 3 on Disk 0 has the Label HP_RECOVERY
Volume 4 on Disk 0 has the Label FAT32 Part
Volume 5 on Disk 1 has the Label Storage


Unfortunately for me it didn't work, but I have high hopes it could be tweaked to work. I'm on Win7 Pro x64.

There is no drive connected that has the label of "OS"
These are the currently connected volumes:
Volume 1 on Disk 0 has the Label NTFS Part
Volume 2 on Disk 0 has the Label OS
Scrotos
Graphmaster Gerbil
 
Posts: 1009
Joined: Tue Oct 02, 2007 11:57 am
Location: Denver, CO.

Re: Batch file to switch active partitions

Postposted on Wed May 01, 2013 11:28 am

Am I the only one wondering what possible reason you could have to do this?
TwistedKestrel
Gerbil Team Leader
 
Posts: 211
Joined: Mon Jan 06, 2003 3:29 pm

Re: Batch file to switch active partitions

Postposted on Wed May 01, 2013 11:36 am

No.

For me it's a "hey neat!" kinda thing. But practical use? Uh, no idea. Just use a bootloader or something, right? I'm guessing he wants to automatically switch where to boot every time he turns on his computer. Maybe. Got no idea.
Scrotos
Graphmaster Gerbil
 
Posts: 1009
Joined: Tue Oct 02, 2007 11:57 am
Location: Denver, CO.


Return to Windows

Who is online

Users browsing this forum: No registered users and 2 guests