Jump to content











Photo
- - - - -

VHD_Mount

vhd tools shell win7

  • Please log in to reply
37 replies to this topic

#1 sambul61

sambul61

    Gold Member

  • Advanced user
  • 1568 posts
  •  
    American Samoa

Posted 13 January 2012 - 08:07 AM

This new VHD_Mount project aims to improve VHD_MNT batch by chenall, posted as open source. I adapted this code for Win7 32/64-bit English, it currently allows to add shell commands: Attach / Detach VHD and Create Dif VHD from selected Parent via File Context Menu in Win Explorer.

@ECHO OFF

REM VHD_Mount is edited English version of chenall's VHD_MNT batch for Win7 32/64 bit.

REM It adds 2 shell commands to Registry: "Attach/Detach VHD" and

REM "Create Dif VHD". To attach or detach a VHD, doubleclick its file.

REM To Create a Dif VHD, select this command from RMC. The child VHD will be

REM created in parent's directory and assigned relative to the parent name.

REM If default vhd file association (key value HKEY_CLASSES_ROOT.vhd) is set in Registry

REM (for example to "Windows.VirtualPC.HD"), you need to replace in :install and :uninstall subs below

REM "HKCR.vhd..." with "HKCRWindows.VirtualPC.HD..." in all commands

REM Doubleclick the batch to install & uninstall



IF "%~n1"=="" GOTO :install

SET "diskpart=echo exit|%ComSpec% /k prompt %%dsc%%$_|diskpart"

IF "%2"=="PARENT" GOTO :PARENT



:attach

TITLE Autodetect....

SET dsc=list$Svdisk

%diskpart%|find /i "%~1" > nul && goto :detach

TITLE Attach VHD %*

SET dsc=select$Svdisk$Sfile="%~f1" $_attach$Svdisk$S%2$_

GOTO :diskpart



:detach

TITLE Detach VHD %*

SET dsc=select$Svdisk$Sfile="%~f1"$_detach$Svdisk$_

GOTO :diskpart



:PARENT

SHIFT /2

FOR /l %%i in (1,1,99) do IF not EXIST "%~dpn1_Dif%%i.vhd" (set "file=%~n1_Dif%%i.vhd" & GOTO :create)

EXIT /b



:create

TITLE Create Dif VHD from Parent %1

echo.

echo. VHD_MNT will create a Differencing VHD Image File

echo. from Parent: %1

rem echo.Enter below a new Dif VHD file name, for example: %file%

rem echo. Note: don't enter full path.

echo.

rem SET /p file=Enter new Dif VHD name:

SET dsc=create$Svdisk$Sfile="%~dp1%file%"$SPARENT="%~f1"$_

TITLE Created Dif VHD %file%



:diskpart

%diskpart%

rem PAUSE

EXIT /b



:install

reg query "HKCR.vhdshellAttach/Detach VHDcommand" /ve 1>nul 2>nul && GOTO :uninstall

reg add "HKCR.vhdshellAttach/Detach VHDcommand" /d "vhd_mnt.cmd "%%1" /f >nul

reg add "HKCR.vhdshellCreate Dif VHDcommand" /d "vhd_mnt.cmd "%%1" PARENT" /f >nul

reg add "HKCR.vhdDefaultIcon" /t REG_EXPAND_SZ /d "%%SystemRoot%%system32shell32.dll,8" /f >nul

COPY /y "%~f0" "%SystemRoot%system32vhd_mnt.cmd" >nul

echo.

echo. VHD_Mount has been installed.

echo.

echo. Now you can doubleclick a VHD file to Attach/Detach,

echo. or Create a Differencing VHD from selected Parent VHD

echo.

echo. Based on VHD_MNT by chenall 2011-04-29

PAUSE

EXIT



:uninstall

echo.

reg delete "HKCR.vhdshell" /f >nul

DEL /f "%SystemRoot%system32vhd_mnt.cmd"

echo.

echo. VHD_Mount has been uninstalled.

echo.

echo. Based on VHD_MNT by chenall 2011-04-29

PAUSE

EXIT

The batch adds convenience compare to using Disk Management or Diskpart - it's handy basic shell VHD tool. Read REMarks in the code for more usage details. It will be improved to add more common ops with VHDs. For example, it doesn't auto assign a drive letter to an existing VHD volume when attached, if its default drive letter is used at the moment by another drive or was never assigned, or removed.

You're welcome to suggest improvement ideas, for example a fix to check if a newly attached VHD got a drive letter & get mounted (without checking its content that may be unknown), and if not, check available drive letters, and auto assign one of these to the VHD Volume? :confused1: What would be the shortest way to do it?

Attached is the latest VHD_Mount version (it may differ from the code printed above).

Attached Files


  • TheHive likes this

#2 TheHive

TheHive

    Platinum Member

  • .script developer
  • 4198 posts

Posted 13 January 2012 - 09:06 AM

Seems like a good start.

#3 Wonko the Sane

Wonko the Sane

    The Finder

  • Advanced user
  • 16066 posts
  • Location:The Outside of the Asylum (gate is closed)
  •  
    Italy

Posted 13 January 2012 - 09:42 AM

What would be the shortest way to do it?

Using the output of MOUNTVOL (and/or using Registry Entries and/or using diskpart or fsutil)
A number of related routines/checks etc. can be found in the batches/vbs here:
http://www.msfn.org/...ty-for-windows/
http://www.msfn.org/...-drive-letters/

or if a third party utility is allowed, dd --list is another suitable way:
http://reboot.pro/8219/
or directly running showdrive (but this may mount some other "hidden" partition/volume"):
http://reboot.pro/10169/

If using MOUNTVOL, and needing to check contents, be aware of the issues with IF EXIST and card readers with no media:
http://www.msfn.org/...e/page__st__117

See if this fits:
@ECHO OFF

SETLOCAL ENABLEDELAYEDEXPANSION

SET M=0

FOR /F "tokens=1 delims= " %%? IN ('MOUNTVOL^|FIND "\"') DO SET /A M+=1&SET L_!M!=%%?

FOR /L %%? IN (1,1,%M%) DO CALL :do_ %%?

GOTO :EOF

:do_

SET /A N=%1+1

IF "!L_%1:~0,4!"=="\\?\" IF "!L_%N%:~-2,2!"==":\" (ECHO !L_%1! mounted as !L_%N%:~-3,3!) ELSE (ECHO !L_%1! NOT MOUNTED)



:cheers:
Wonko

#4 sambul61

sambul61

    Gold Member

  • Advanced user
  • 1568 posts
  •  
    American Samoa

Posted 13 January 2012 - 02:53 PM

Thanks Wonko!

I know you love batches, and spend some time in the past on similar tasks. Will try to study all suggested approaches. Since Diskpart is already used, its desirable to avoid any other utilities, but will check MOUNTVOL. No need to check VHD content that generally is unknown. What does your code do: assign drive letters to all unmounted volumes, or just check if there are unmounted?

May be simpler approach will do: Diskpart Automount? It needs ONLY work for last attached VHD disk, not other existing unmounted disks. Any possible side effects? Or, possibly along these lines below, or by using WMI:

select vdisk (already selected by user)

detail disk (check if online)

attributes volume (check if drive letter exist - I guess WMI is needed to check & use a selected volume attributes in a script)


There are some other issues that need resolution:

- the CMD runs slow due to slow Diskpart start. It only runs fast, when another Console is open with Diskpart running. On the other hand, its unknown upfront, how often it will be used during a session, so leaving Diskpart running might not be the best. How to make it run faster?

- auto select the correct Registry key to add shell commands to

- for Mount / Dismount ops there's no need to show Console. Using code below allows to run it without showing Console, but it requires a second CMD. Is there a way to fit this features inside the 1st CMD?

@echo off

start /B VHD_Mount.cmd



OR



@echo off

start /MIN VHD_Mount.cmd

- add Create New VHD shell command that would only show up active in Context Menu, when clicking on empty space in a directory, and opens: Diskpart Create Vdisk with filled PATH in Console

- add Repair Dif Link shell command to repair broken link between a parent and its child when moved

- add Add to Boot Menu shell command that would add a selected native boot VHD entry to Windows Boot Menu of a mounted drive or another VHD, which drive letter is entered by user in Console. For booting Dif VHDs, both Child and Parent VHDs must be accessible at boot time in unchanged locations

- add Detach Disk to Disk Context Menu

- group all VHD_Mount commands under a cascading VHD_Mount menu item in Context Menu

#5 Wonko the Sane

Wonko the Sane

    The Finder

  • Advanced user
  • 16066 posts
  • Location:The Outside of the Asylum (gate is closed)
  •  
    Italy

Posted 13 January 2012 - 03:07 PM

What does your code do: assign drive letters to all unmounted volumes, or just check if there are unmounted?

Neither, it simply filters the output of the MOUNTVOL command in a friendly manner (in a way that you can use to set variables so that drive letters are coupled to mounted volumes).
Just copy/paste and run it in a command console, it doesn't alter anything if not a few local variables.

You can use any of the various console hiding apps, or you can use the actual START command in the Registry entry.

:cheers:
Wonko

#6 sambul61

sambul61

    Gold Member

  • Advanced user
  • 1568 posts
  •  
    American Samoa

Posted 13 January 2012 - 03:20 PM

Like these examples of using Start command? It might work with VHD_Mount shell commands in Registry allowing to hide Console. No extra apps pls... :)

Another thing is, I can't make it work when adding Default expandable string value in Reg for a shell command, like

%systemroot%system32vhd_mnt.cmd "%1"

It only works with explicit paths or no path at all - may be syntax is wrong? It looks like "no path" presumes current system32 folder, or search for VHD_Mount.cmd on the system disk?

#7 Wonko the Sane

Wonko the Sane

    The Finder

  • Advanced user
  • 16066 posts
  • Location:The Outside of the Asylum (gate is closed)
  •  
    Italy

Posted 13 January 2012 - 04:31 PM

Like these examples of using Start command? It might work with VHD_Mount shell commands in Registry allowing to hide Console. No extra apps pls... :)

Yep, but not to "hide" only to run minimized.

Another thing is, I can't make it work when adding Default expandable string value in Reg for a shell command, like

%systemroot%\system32\vhd_mnt.cmd "%1"

It only works with explicit paths or no path at all - may be syntax is wrong? It looks like "no path" presumes current system32 folder, or search for VHD_Mount.cmd on the system disk?

Open a command prompt window and run in it:
SET PATH
if the .cmd is in any of the directories in path, it will be found.

BUT, compare ATTENTIVELY these three lines (from the batch you posted):
reg add "HKCR\.vhd\shell\Attach/Detach\command" /d "vhd_mnt.cmd %%1" /f >nul

reg add "HKCR\.vhd\shell\Create Dif VHD\command" /d "vhd_mnt.cmd %%1 PARENT" /f >nul

reg add "HKCR\.vhd\DefaultIcon" /t REG_EXPAND_SZ /d "%%SystemRoot%%\system32\shell32.dll,8" /f >nul

with the EXACT, COMPLETE reg add command line you used, or post it (instead of it's contents):
%systemroot%\system32\vhd_mnt.cmd "%1"
(in order to check whether you used a wrong syntax)

:cheers:
Wonko

#8 sambul61

sambul61

    Gold Member

  • Advanced user
  • 1568 posts
  •  
    American Samoa

Posted 13 January 2012 - 05:25 PM

The above batch was given several "minor" (once identified) fixes (apart from being translated) that switched it from "Doesn't work in Win 64-bit Eng" state to hopefully "Just works" state. :) As you know, available Batch Debuggers can check syntax, but can't ID errors in more complex cases.

I used this in Win7 64-bit running from Console:

reg add "HKCR.vhdshellCreate Dif VHDcommand" /t REG_EXPAND_SZ /d "^%SystemRoot^%system32vhd_mnt.cmd %1 PARENT" /f >nul

It adds the subkey, but gives the error when running the CMD even as Admin: "Windows can't access the specified device, path or file. You may not have appropriate permissions to access the item". Tried also %%systemroot%%..., @%%systemroot%%.., etc. - don't work, while @ %%systemroot%% opens Open With box with lost 2-nd command parameter. :dubbio: When no path or explicit path is given in Reg, it works OK running as a User.

Any way to group VHD_Mount commands in an expandable menu (folder like) item in Context Menu via batch, like 7zip context menu?

Any examples of using in a batch script volume attributes (like Drive Letter) after they're identified (or found missing) by Diskpart Atrributes Volume command for a selected volume - without human input?

#9 Wonko the Sane

Wonko the Sane

    The Finder

  • Advanced user
  • 16066 posts
  • Location:The Outside of the Asylum (gate is closed)
  •  
    Italy

Posted 13 January 2012 - 06:19 PM

As you know, available Batch Debuggers can check syntax, but can't ID errors in more complex cases.

Actually never heard about (let alone used) a "Batch Debugger". :w00t:
Can you name a few of them (with links)?

But I said compare ATTENTIVELY.

When you use % in a batch (and you want the variables NOT expanded in the output), ALL instances of it need to be doubled (technically escaped).

The line you posted does not conform to this.

The lines in the originally posted batch seemingly do:

reg add "HKCR\.vhd\shell\Attach/Detach\command" /d "vhd_mnt.cmd %%1" /f >nul
reg add "HKCR\.vhd\shell\Create Dif VHD\command" /d "vhd_mnt.cmd %%1 PARENT" /f >nul
reg add "HKCR\.vhd\DefaultIcon" /t REG_EXPAND_SZ /d "%%SystemRoot%%\system32\shell32.dll,8" /f >nul


What happens with:

reg add "HKCR\.vhd\shell\Create Dif VHD\command" /t REG_EXPAND_SZ /d "%%SystemRoot%%\system32\vhd_mnt.cmd %%1 PARENT" /f >nul

:unsure:

Any way to group VHD_Mount commands in an expandable menu (folder like) item in Context Menu via batch, like 7zip context menu?

I am not sure to understand the question, can you provide a screenshot sample?

Any examples of using in a batch script volume attributes (like Drive Letter) after they're identified (or found missing) by Diskpart Atrributes Volume command for a selected volume - without human input?

This I really cannot understand, I thought I initially gave you links to some of such examples.
Or post what you would run on command line and maybe it can be translated in batch.

:cheers:
Wonko

#10 sambul61

sambul61

    Gold Member

  • Advanced user
  • 1568 posts
  •  
    American Samoa

Posted 13 January 2012 - 06:33 PM

Merely followed this example, because %%systemroot%% approach doesn't work here for CMD, as said above (same error):

REG ADD HKLMSoftwareMyCo /v Path /t REG_EXPAND_SZ /d ^%systemroot^%

Example of expandable (cascading) 7zip context menu from another forum (not mine):

Posted Image


Some Batch Debugger links personally for Wonko (The Finder): :)

Runnings Steps

DrBatcher

#11 Wonko the Sane

Wonko the Sane

    The Finder

  • Advanced user
  • 16066 posts
  • Location:The Outside of the Asylum (gate is closed)
  •  
    Italy

Posted 13 January 2012 - 07:25 PM

But that is from command line, not from batch.

And I presume that IF the mentioned three lines work for you AND your line does not is because you used %1 instead of %%1. :whistling:

Copy/paste, save as testperc.cmd and run this:
@ECHO OFF

SETLOCAL

CALL :do_ parameter

GOTO :EOF



:do_

ECHO Hi, I am Systemrroot- %%Systemroot%%

ECHO Hi, I am Systemrroot- %Systemroot%

ECHO Hi, I am Systemrroot- ^%Systemroot^%

ECHO.

ECHO Hi, I am parameter- %%1

ECHO Hi, I am parameter- %1

ECHO Hi, I am parameter- ^%1^%

ECHO.

Believe me (and the evidence of the three lines) that percentage is escaped by doubling it in batches.

Guess why this is perfectly OK on command line:
@ECHO OFF&FOR /L %A IN (1,1,5) DO ECHO %A
but you need to write it as:
@ECHO OFF&FOR /L %%A IN (1,1,5) DO ECHO %%A
if you write it in a batch?

Hmmm :dubbio::
http://www.drbatcher.com/

Are you searching for software to create batch files that will help you to make them without studying their syntax?

A nice shortcut, only seemingly longer/leading to nowhere.
:rofl:


:cheers:
Wonko

#12 sambul61

sambul61

    Gold Member

  • Advanced user
  • 1568 posts
  •  
    American Samoa

Posted 13 January 2012 - 07:42 PM

You don't like my favorite Batch Debugger? :realmad: I have to agree, DrBatcher shown limited usability in making the batch actually work, but it helps to check syntax. :lol:

As to the above 3 commands from original batch, they don't work for me either. I have to manually add "" around each parameter in Reg for the CMD to work, like "%1", but how to do it from batch - the "" are dropped at import? Posted above "as is", since expect it to be open source project, not mine personal. This shell app will actually be quite handy, when finished. :)

I'd like to believe you my dear but that's what I get as a result:

This works in Reg: vhd_mnt.cmd "%1" "PARENT"
BUT how to put it in from batch?

This also works in Reg:

%systemroot%system32vhd_mnt.cmd "%1" "PARENT"

Any ideas? :dubbio:

#13 Wonko the Sane

Wonko the Sane

    The Finder

  • Advanced user
  • 16066 posts
  • Location:The Outside of the Asylum (gate is closed)
  •  
    Italy

Posted 13 January 2012 - 08:03 PM

So you posted a NOT working batch?

Can you run the small snippet I posted and post it's output?
Here is what I get here:
Hi, I am Systemrroot- %Systemroot%

Hi, I am Systemrroot- C:WINDOWS

Hi, I am Systemrroot-

ECHO.

Hi, I am parameter- %1

Hi, I am parameter- parameter

Hi, I am parameter- parameter

ECHO.

I don't think you will get different results. :dubbio:

:cheers:
Wonko

#14 sambul61

sambul61

    Gold Member

  • Advanced user
  • 1568 posts
  •  
    American Samoa

Posted 13 January 2012 - 08:14 PM

I got the same result in Console as you, but it means nothing for updating Reg in terms of adding "" to params. :dubbio: As to %%, it seems to works from batch, but doesn't as console command. :)

#15 MedEvil

MedEvil

    Platinum Member

  • .script developer
  • 7771 posts

Posted 13 January 2012 - 08:28 PM

Don't know, if this discussion is purely academical. If not, you might be interested in the attachement. It is from a russian member on this board, nick Stea.

:cheers:

Attached Files



#16 sambul61

sambul61

    Gold Member

  • Advanced user
  • 1568 posts
  •  
    American Samoa

Posted 13 January 2012 - 09:18 PM

MedEvil

Thanks for pointing to this app. I tested it previous version that used several .exe from another open source tool. Current version uses multiple batch files, drawing some ideas from the chenall's single batch. This tool doesn't really offer a lot more extra features compare to chenall's script, but adds more complexity. In addition, it doesn't work in Win7 64-bit, as just tested.

VHD_Mount discussed in this thread appears to use more elegant single small batch approach, and its features will be expanded. Still, I'll look through some code in its batches. Some questions we discussed above in large part are generic, and no working solutions were suggested so far. :) May be you can suggest some - just look at previous posts. :confused1:

#17 sambul61

sambul61

    Gold Member

  • Advanced user
  • 1568 posts
  •  
    American Samoa

Posted 14 January 2012 - 01:41 AM

Thanks Wonko,

I found the right format for the task: :)

reg add "HKCR.vhdshellCreate Dif VHDcommand" /t REG_EXPAND_SZ /d "%%SystemRoot%%system32vhd_mnt.cmd "%%1" "PARENT"" /f >nul

Updated the attachment in 1st post.

Yep, but not to "hide", only to run ("Start" from Registry) minimized.

Can you give an example of such Reg entry (a screenshot) that actually (in fact) works for you when TESTED in WinXP (not in your lovely DrBatcher)? Say, Start from Reg your own batch that would open a selected in WE .txt file in Notepad? :dubbio:


Regarding Cascading Menus: here's Creating Cascading Context Menus Tutorial for Win7. Fast Explorer 2008 offers an easy way to automate this process.

#18 Wonko the Sane

Wonko the Sane

    The Finder

  • Advanced user
  • 16066 posts
  • Location:The Outside of the Asylum (gate is closed)
  •  
    Italy

Posted 14 January 2012 - 10:18 AM

As to %%, it seems to works from batch, but doesn't as console command.


Yep, you were just told how batch is different from command line when it comes to %. (and a few other things)

And about the line you just posted,sometimes reading the syntax does help:
http://ss64.com/nt/reg.html

To include a quote mark (") in the data, prefix it with the escape character () e.g. "Here is " a quote"

Though I cannot see why PARENT should be inside double quotes.



Can you give an example of such Reg entry ...

A Registry entry to start minimized a batch (passing to it the target file path) would be:

cmd.exe /c start "Title here" /min "path to some batch here" "%1"


but you will have anyway a quick flash of the command window.

:cheers:
Wonko

#19 sambul61

sambul61

    Gold Member

  • Advanced user
  • 1568 posts
  •  
    American Samoa

Posted 14 January 2012 - 03:53 PM

Wonko

Yes, PARENT works without quotes.

This code works from Registry:

cmd /c start "" /min %SystemRoot%system32vhd_mnt.cmd "%1" PARENT

I had to remove /b switch from :diskpart subroutine. Any idea, why chenall added it? But without /b CMD seems to execute longer in loop or repeat use. Its sad, /b doesn't work from Reg. :) BUT...how to add this value to Reg from a batch - below command gives Reg Add error? :confused1:

reg add "HKCR.vhdshellCreate Dif VHDcommand" /t REG_EXPAND_SZ /d "cmd /c start "" /min %%SystemRoot%%system32vhd_mnt.cmd "%%1" PARENT" /f >nul


Regarding vdisk mount, WMIC would directly check, if DriveLetter attribute exists for a selected by user vdisk. I wonder, if WMIC can check available drive letters too... :dubbio:

#20 Wonko the Sane

Wonko the Sane

    The Finder

  • Advanced user
  • 16066 posts
  • Location:The Outside of the Asylum (gate is closed)
  •  
    Italy

Posted 14 January 2012 - 05:45 PM

Well, Exit, again strangely enough, has a syntax:
http://ss64.com/nt/exit.html
chenall seemingly used it to terminate a subroutine :dubbio: but he used GOTO instead of CALL (which is not IMHO the "right" approach).

About windows 7 wmic cannot say, but you can list all assigned drive letters and remove from the list those not assigned, the trouble is with something else, like network drives, drives that do not show in mountvol, and the like, hence the complexity (and AFAICR not yet 100% reliability of the batches I originally pointed you to).


:cheers:
Wonko

#21 sambul61

sambul61

    Gold Member

  • Advanced user
  • 1568 posts
  •  
    American Samoa

Posted 14 January 2012 - 06:00 PM

This works OK:

reg add "HKCR.vhdshellCreate Dif VHDcommand" /t REG_EXPAND_SZ /d "cmd /c start /min %%SystemRoot%%system32vhd_mnt.cmd "%%1" PARENT" /f >nul


Also, without /b switch the batch get frozen at times (Autodetect gets sow), while with it Console remains open after executing the batch via Start. Any code suggestions - I'll see if CALL works instead of GOTO for that purpose, when having time... At this point the attached in 1st post batch works OK with its original features. :)

#22 sambul61

sambul61

    Gold Member

  • Advanced user
  • 1568 posts
  •  
    American Samoa

Posted 16 January 2012 - 02:41 PM

Can someone suggest what's wrong with this syntax:

set "str=echo list vdisk | diskpart | find /i "%~1""

echo %str%

for /f "tokens=3" %%a in ("%str%") do set d=%%a

echo %d%

The string echo list vdisk | diskpart | find /i "%~1" works, but %str% prints error, and I need to separate Disk # (i.e. 7), not the whole Vdisk # string:

VDisk 0 Disk 7 Attached not open Fixed C:Sample.vhd


Since Diskpart is already running for other batch needs, its time efficient to use it.

#23 Wonko the Sane

Wonko the Sane

    The Finder

  • Advanced user
  • 16066 posts
  • Location:The Outside of the Asylum (gate is closed)
  •  
    Italy

Posted 16 January 2012 - 03:22 PM

You have to escape pipe symbols inside a FOR loop with a caret:

^


When using "tokens=" is always advised to use "delims=" explicitly.
Disk# should be token #4
Moreover if you want the result of a command parsed in a FOR /F you need to enclose it in single quotes.
http://www.robvander...ntfortokens.php

Example:

SETLOCAL ENABLEEXTENSIONS

FOR /F "tokens=1,2,3,4,* delims= " %%A IN ('echo list vdisk ^| diskpart ^| find /i "%~1"') DO (

ECHO %%A

ECHO %%B

ECHO %%C

ECHO %%D

ECHO %%E

)


:cheers:
Wonko

#24 sambul61

sambul61

    Gold Member

  • Advanced user
  • 1568 posts
  •  
    American Samoa

Posted 16 January 2012 - 05:49 PM

Thanks Wonko (The Batcher). :)

Any suggestions on how to simplify or make faster this sequence - it looks for a FileName of a selected in WE Disk Panel vdrive:

@echo off

rem SETLOCAL ENABLEEXTENSIONS

SET "diskpart=echo exit|%ComSpec% /k prompt %%dsc%%$_|diskpart"

set dsc=list$Svdisk$

FOR /F "tokens=1,2,3,4,* delims= " %%A IN ('%%diskpart%% ^| find /i "Vdisk 0"') DO (set dsk=%%D)

set dsc=select$Sdisk$S%dsk%$_detail$Sdisk$_

FOR /F "tokens=1,2,3,4,* delims= " %%A IN ('%%diskpart%% ^| find /i "Partition"') DO (SET name=%%C)

ECHO %name%

pause

The problem is, I need to run similar sequence in loop for each mounted vdisk to find a matching (to selected by user) DriveLetter, and resulting VHD FileName, then use Diskpart to Detach the VHD, when its disk is selected from WE Disk Panel. This way I can use Diskpart instead of Mountvol (which also requires similar loop when attaching the same VHD later, since its DriveLetter will be removed).

#25 Wonko the Sane

Wonko the Sane

    The Finder

  • Advanced user
  • 16066 posts
  • Location:The Outside of the Asylum (gate is closed)
  •  
    Italy

Posted 16 January 2012 - 06:44 PM

Thanks Wonko (The Batcher). :)

Any suggestions on how to simplify or make faster this (working) sequence - it looks for a drive letter of just mounted vdisk:

What do you mean "simplify"? :dubbio:

You need to call anyway diskpart two times, only you could remove the completely unneeded other tokens request and leave just the one you use, as in your previous post.
The given example with 1,2,3,4,* was only to let you see which token was the one you needed (#4 and not #3).

Yep, this:

FOR /F "tokens=1,2,3,4,* delims= " %%A IN ('%%diskpart%% ^| find /i "Fixed"') DO (set disk=%%D)

will work OK only if a single vdisk is mounted (or last vdisk# will be given to disk variable) and this:

FOR /F "tokens=1,2,3,4,* delims= " %%A IN ('%%diskpart%% ^| find /i "Partition"') DO (SET ltr=%%C)

will work OK only if that single mounted vdisk has only one partition (or last drive letter will be given to ltr variable), right? :unsure:

You could alternatively adopt this technique/approach :dubbio: :
http://www.msfn.org/...le/page__st__16
http://www.msfn.org/...le/page__st__19
http://www.msfn.org/...le/page__st__25
Besides the (completely optional) embedding of diskpart commands in "clear", the snippets show how to build a list of disk numbers and drive letters.


:cheers:
Wonko





Also tagged with one or more of these keywords: vhd, tools, shell, win7

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users