Jump to content

Batch scripting diskpart


Recommended Posts

Alright so with commands like diskpart I've wondered how you could create a batch file to preform actions with x command. I know you couldn't just put say something like "list disk" right after diskpart in the actual batch, so I did end up finding a few sites on google on the topic. Here's a sample batch I'm having some trouble understanding fully. It basically takes two partitions by number as referenced with diskpart's list part command and switches thier drive letter assignments with each other. Not really useful and not exactly what I want/need, but anyway...

@for /f "tokens=3" %%x in ('echo list volume ^| diskpart ^| findstr /c:"Volume %1"') do set v1=%%x
@for /f "tokens=3" %%x in ('echo list volume ^| diskpart ^| findstr /c:"Volume %2"') do set v2=%%x
echo select volume %1 > SwitchDisks.csp
echo remove letter %v1% noerr >> SwitchDisks.csp
echo select volume %2 >> SwitchDisks.csp
echo remove letter %v2% noerr >> SwitchDisks.csp
echo select volume %1 >> SwitchDisks.csp
echo assign letter %v2% >> SwitchDisks.csp
echo select volume %2 >> SwitchDisks.csp
echo assign letter %v1% >> SwitchDisks.csp
diskpart /s SwitchDisks.csp
del SwitchDisks.csp

I know that its echoing all the actions to preform inside diskpart to another file to use with diskpart's script switch (/s). Lines 1 and 2 are what I really don't get and things like %v1, %v2, etc though. Explaination any one?

Link to comment
Share on other sites

Put the code in a batch file, and call for it with two arguments.

%x is a argument. Where x is the position.

Put this in a batch file:

@echo off
echo %1
echo %2
echo %3
echo %4
echo %*

call it by: <name>.bat arg1 arg2 arg3 arg4 arg5

Here is more in details: http://commandwindows.com/batch.htm

In your case you need to send the volume number args to script.

Link to comment
Share on other sites

I kind of understand your explaination, but want to know what its doing behind the scenes detail by detail, command by command...

Questions:

Whats the /f swtich for for "for"?

How can diskpart run without /s for "list volume" which is echoed before it without using a file?

Whats the ' for before "echo"?

I guess its incrementing v<number> variables for each volume line returned using findstr on the output from diskpart list volme, but what are the ^'s for?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...