Link to home
Start Free TrialLog in
Avatar of Samuel-AMC
Samuel-AMC

asked on

Batch - Running Diskpart from batch file

Hello everyone,

"Batch Diskpart, running few commands at the same time."

Is been a long time since i wanted to create a batch file that can allow me to run Diskpart and few commands at the same time, i've been wondering if is possible or not.
For example, this is what i do when ever i use Diskpart and few commands:

1.) I open CMD, and type: Diskpart
2.) Then type: List volume, or depending on what i want Diskpart to do for me.
3.) Then i keep doing this line per line, etc. i don't know if you guys get my point.

But thats what i would like to be able to do, but all my attempts have failed.
Any help will be very appreciate,
Thanks in advance
Avatar of Qlemo
Qlemo
Flag of Germany image

You can
* either write a text file containing all commands for diskpart, and provide that with the -s parameter of diskpart,
* or use a batch file (.cmd) like this:
@echo off
(echo select disk 1
 echo list vol
) | diskpart

Open in new window

you can add the command after the diskpart command it will work .Batch files will continue with second command automatically  once it is finished the first one

say

xcopy /e/h  e:\folder1 d:\folder2
netsh int ip set address "local area connection" static 10.10.1.50 255.255.0.0 >>iplog.txt
del /q dirlist.txt

first it will copy then set IP then del and then exit .
you can also create another bat file or script and call it in the batch process

xcopy /e/h  e:\folder1 d:\folder2
netsh int ip set address "local area connection" static 10.10.1.50 255.255.0.0 >>iplog.txt
del /q dirlist.txt
c:\batch2
c:\test.vbs
Avatar of Samuel-AMC
Samuel-AMC

ASKER

Thanks Qlemo, sumeshbnr,
Let me explain a little bit more, one of the thing i use Diskpart the most, is to
remove drive from my computer window, and i tried before what Qlemo suggested,
and never worked for me, but let me try again with your example.
Qlemo is working :-)

DISKPART>
Disk 1 is now the selected disk.

DISKPART>
Volume ###  Ltr  Label        Fs     Type        Size     Status     Info
  ----------  ---  -----------  -----  ----------  -------  ---------  --------
Volume 0     D                       DVD-ROM         0 B  No Media
Volume 1         RECOVERY     NTFS   Partition     14 GB  Healthy    System

DISKPART> Press any key to continue . . .

Now i need to figured out how to do the task, if i add the code you posted, it runs,
but then close very fast, so i added a pause at the end, now i can see diskpart working.
but since i need to select which drive i need to work with, is okay for diskpart to stop at
this point, the thing is that diskpart need my help to manually select the drive as not
always you spect the drive to be the same letter as before, i don't konw if you got me.
Are you able to "detect" the drive automatically, e.g. by label RECOVERY?
We will need to to at least two separate diskpart calls anyway - one for displaying/detecting the drive, and one for executing the final commands.

Please list all commands you perform for your task, and how you find the disk. We can automate the detection, or just prompt you for an input.
sumeshbnr, how should that link help? It just lists the diskpart options to use ... We can assume the commands are already known.
Thanks sumeshbnr, but thats exactly what i always do, but manually,
I meant, since i love to always work and play with my batch files, i always
wanted to make a batch file to call diskpart for me, instead of me going to
CMD and beging to type if you know what i meant.
Qlemo, this works very well, nice job, good code,

(echo select disk 1
 echo list vol
) | diskpart

But let explain... when i run diskpart manually, i first let diskpart to show me the
list of drive on the machine, then i select disk 1, or sometimes is 2, 3, etc. now what
i would like to do is to make diskpart to let the List volume there, i meant, so i can
decide which volume to select, etc.
Okay, this time i got it right so i can make diskpart to show only the volume
without having diskpart selecting the volume x # etc. the only problem now
is having diskpart to let you continue with the task wihtout having the batch
file close by the same task.

(echo list vol
) | diskpart

Pause
Sorry to sound stubborn, but you need to show the exact sequence of commands you use. I understand you would do a
  diskpart list disk
and than manually choose one disk <diskno>.
 (echo select disk <diskno>
   echo list volume) | diskpart

and then?
Okay... the idea is to have diskpart to remove or hide a drive, in this case we are
traying to make diskpart to hide or remove drive 1 which in other words is drive D:\
but like i said, since that drive is not always the same volume, the letter yes, is always
the same, i meant letter D, but volume sometimes is 1, or 2, or 3, etc. but the whole
subject here is simple, there is nothing else to show, thats it, thats all about it.
That's some info now, but it is NOT simple. D: can be a volume on drive 1, so we have to parse thru all volumes of all drives to detect "drive" D:.
What happens if D: is just one of many volumes on a drive?
What is the command you use to "hide" the "drive"? Please show an example, so we can make sure we are on the same line.
Okay look, the script you posted is okay, with the only diferents that it does not le you
do a thing, i meant, you can't select anything, your script if there is not a pause at the
very end, the batch file just opens and close, thats all, now i have nothing to show you,
but let me post how i do it from command line:

1.) Diskpart
2.) List volume
3.) Select volume 1
4.) Remove letter D
5.) Exit

But i do this manually, and if volume is some other numner, then i select that number
and continue with the rest of the task.
ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Okay Qlemo, i got your point, but could you please explain a little bit about the script?
I meant, i executed the script, and you don't get to see nothing, just the wait a bit and
then the: "Press any key to continue . . ." i meant, how about if the volume is not 1 or 2 ?
how about if the volume is 4? etc. or it should do it even if it is 1, 2, 3, 4, 5, etc ???
Thanks
Thank You Very Much Qlemo, i will figure out how to work with the script,
or if you could add an explanation that would be nice, thanks in advance.
Now i need to get some good sleep.
The FOR loop detects which volume "D:" is on, and that for the follow-up diskpart command. You will have issues if D: is on another drive, as that script only checks for the first drive.

If you want to see some (not much) output, remove  >nul   from the diskpart command. That should tell you at least if the command executed successfully.
Nothing happens after removing ">null" just a blinking dash that's all.
Does diskpart usually show you more? You will only see the output of the select volume and remove letter part, and only if there is a drive D:.
Actually when you do this manually yes, it does show you what was done,
and whats the status of the diskpart content, but with the batch script you
made it doesn't show anything, like i said, i execute the batch file and all i see
is the blinking dash and nothing else, thats the pause command functionality
waiting for me to click any button to finish the task. but is okay with me as long
as the script do the task.