tags:

views:

114

answers:

2

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.

+2  A: 

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.

Axeman
+1 but you missed the possibility of a different `dir` program in `%PATH%`.
Sinan Ünür
+3  A: 

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.

Sinan Ünür
@Sinan, I assume @lokesh is a Windows XP user. I tested the code he borrowed from you and it worked without anything funny. So now I'm thinking he's using some other system.
Mike
@Mike: I am on Windows XP. When I put `c:\opt\cygwin\bin` in the path, `No such file or directory` is the message I get because Cygwin's `dir.exe` thinks `/ad/b/s` is a path. (It is perfectly OK with `E:\ `). So, @lokesh must have some other `dir` in the path. The solution is to explicitly invoke `cmd.exe`s built-in dir that understands the options that are passed to it.
Sinan Ünür
@Sinan, your explantion makes sense.
Mike
@Mike: Thank you.
Sinan Ünür