AtomicPorkchop
AtomicPorkchop

Reputation: 2675

How can I unlink a windows folder with spaces?

I am trying to remove files and folders that do not match our naming standard. Well I can seem to loop through an array of collected files and folders that do not match and delete them. The problem I am running into is I cannot remove files that have spaces in them. I am running on Windows with Strawberry Perl.

Here is a sample of the array

Picture.jpg
Sample Document.doc
New Folder

The only thing I can delete successfully is Picture.jpg in this example.

Here is the function

foreach my $folder (@$returned_unmatches) {
    print "$folder\n";
    remove_root_junk($office,$folder);
}

Here is the subroutine.

sub remove_root_junk {

my $office = shift;

my $folder = shift;

my $order_docs_path = $office_names{$office};

unlink "$order_docs_path\\$folder";

}

$order_docs_path is just the full path up to where I am working. In this case C:\Data\Locations\Canton\Order_Documents if this help at all.

Upvotes: 2

Views: 711

Answers (1)

Greg Hewgill
Greg Hewgill

Reputation: 993931

Under normal circumstances, directly unlinking a directory is not supported. The documentation for unlink says:

Note: unlink will not attempt to delete directories unless you are superuser and the -U flag is supplied to Perl. Even if these conditions are met, be warned that unlinking a directory can inflict damage on your filesystem. Finally, using unlink on directories is not supported on many operating systems. Use rmdir instead.

If your subdirectory is not empty, the rmdir documentation suggests using rmtree in File::Path.

Upvotes: 3

Related Questions