1. Here’s one way to do it:

    my ($src, $trg) = @ARGV;
    die "$src isn't a directory" unless -d $src;
    die "$trg isn't a directory" unless -d $trg;
    `xcopy /s /e $src $trg`;

    We check to make sure both arguments are really directories, then we invoke xcopy to do the dirty work. We could have also used:

    system("xcopy /s /e $src $trg");
  2. Here’s one way to do it:

    @hosts = `net view`;
    foreach (@hosts) {
      next unless m#\\\\#;
      chop;
      s/^(\S+).*/$1/;
      push @sorted, $_;
    }
    print join("\n", sort @sorted);

    We run the command net view and capture the output as a list of lines. We then go through each line looking for hostnames (they start with \\), chop off newlines and comments, and add the matches to another list. We then sort the second list and print it.

Get Learning Perl on Win32 Systems now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.