0

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.

brian d foy
  • 129,424
  • 31
  • 207
  • 592
User1611
  • 1,081
  • 4
  • 18
  • 27
  • Er, what does the second line do? – Susheel Javadi Dec 01 '09 at 06:00
  • this line : split /\n/, dir e:\\/ad/b/s; <-- is your problem. why don't you use glob instead to find files starting with [lL]ib ? – ghostdog74 Dec 01 '09 at 06:42
  • 1
    please show your actual code. you seem to be missing some parts there – ysth Dec 01 '09 at 06:42
  • this is my actual code... am trying to grep the directories which have the name lib or Lib in the whole drive.am not worried about the files present.i want only the directories named as lib or Lib in the whole drive including subdirectories. – User1611 Dec 01 '09 at 06:47
  • @lokesh, suppose dir e:\\/ad/b/s is actually `dir e:\\/ad/b/s`, this code of yours should have worked like you expected, unless there're no direcotries named Lib or lib on Drive E. – Mike Dec 01 '09 at 08:22
  • @all, somehow the backquotes get automatically lost :( – Mike Dec 01 '09 at 08:27
  • 2
    This is where the code is coming from: http://stackoverflow.com/questions/1783631/how-can-i-make-perls-filefind-faster/1783662#1783662 – Sinan Ünür Dec 01 '09 at 11:53
  • Mike: Yes, it should have been `\`dir e:\\/ad/b/s\`` – Sinan Ünür Dec 01 '09 at 12:06
  • 1
    @lokesh The code you posted **CANNOT** be the code you are running: `syntax error at t.pl line 5, near "dir e:" Substitution replacement not terminated at t.pl line 7.` – Sinan Ünür Dec 01 '09 at 13:36
  • 1
    This is getting a bit rude. You repeatedly ask for help when you don't even bother to get compiling code. We do expect that you are going to be the person to do the most work on your own behalf, so show some respect for us by at least posing a good, thoughtful question with compiling code to show that you are at least trying. – brian d foy Dec 01 '09 at 16:07

2 Answers2

3

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.

Community
  • 1
  • 1
Sinan Ünür
  • 116,958
  • 15
  • 196
  • 339
  • @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 Dec 01 '09 at 12:28
  • 2
    @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 Dec 01 '09 at 13:35
  • @Sinan, your explantion makes sense. – Mike Dec 01 '09 at 14:35
2

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
  • 29,660
  • 2
  • 47
  • 102