0

I have 100 disks which I have to make online. I tried the below perl code to do the same.

foreach (1..100){
    open(FILE, ">test.txt");
    print FILE ("select disk $_\nonline disk");
    close FILE;
    system("diskpart.exe /s test.txt");
}

My question here, is there a better efficient way to execute diskpart commands in a loop, like use of shell script or anything else ?

Thanks in advance!

mpapec
  • 50,217
  • 8
  • 67
  • 127
Subhash
  • 568
  • 1
  • 5
  • 21

1 Answers1

3

You can pipe commands on diskpart.exe standard input,

use strict;
use warnings;
use autodie;

open my $p, '|-', 'diskpart.exe';
foreach my $i (1..100) {
    print $p "select disk $i\nonline disk\n";
}
close($p);
mpapec
  • 50,217
  • 8
  • 67
  • 127
  • That's exactly what I would write. But the OP was asking for a more *efficient* way, and I think creating an input file — as he was doing — is probably the most efficient technique – Borodin May 25 '15 at 14:31
  • Actually he calls system in the loop for every single line of the input. – mpapec May 25 '15 at 17:37