简体   繁体   中英

How do I find all lib directories under a Windows path?

I am getting the result "File not found " when I run the script:

use File::Basename; 

my @dirs = grep { fileparse($_) =~ /^[L|l]ib/ } 

split /\n/, dir e:\\/ad/b/s; 

print @dirs; 

This is my actual code. I am trying to grep the directories and subdirectories which have the name lib or Lib in the whole drive.

If you are using the code from my answer to your previous question , the only thing I can think of is that there might be some external dir.exe on your path that does not understand the commandline options for cmd.exe 's built-in dir . For example, with Cygwin's directories in my path, I get

dir: cannot access /ad/b/s: No such file or directory

You should also get in the habit of showing the exact output you are getting if you want people to be able to help you more effectively.

To make sure that does not happen, use:

use strict; use warnings;

use File::Basename;

my @dirs = grep { fileparse($_) =~ /^[Ll]ib/ }
           split /\n/,  `cmd.exe /c dir e:\\ /ad/b/s`;

print "$_\n" for @dirs;

Note the backticks `` ` . Note also the correction to the pattern you are using.

Any number of things.

  • I hope you're using backticks that are getting lost in your post:
  • But you don't need split , if you use backticks, because it will come back as a list.
  • I don't think [L|l] means what you think it means. If you just mean a capital "L" or a lowercase "l", the alternation symbol is not needed. The proper expression is [Ll] ( or (?i:l)ib which means that we localize the i-flag for the group.)

So if it looks like this:

 use File::Basename; 

 my @dirs = grep { fileparse($_) =~ /^[Ll]ib/ } qx{dir /AD /B /S e:\\};

That should work, if there is anything that matches that. Just make sure that you use the qx operator or backticks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM