|
| Author |
Message |
Cheater Senior Member

Joined: 10 Jun 2005 Posts: 236
  
|
Posted: Thu Jun 16, 2005 12:13 pm Post subject: Exclude Extension |
|
|
| I have a code that lists the names of all the files in a directory. How would I get it to exclude the file extension (.txt, .html, .pl, etc.) |
|
| Back to top |
|
 |
darkmonkey The Merovingian

Joined: 18 Apr 2004 Posts: 2557 Location: London, England
     votes: 7
|
Posted: Thu Jun 16, 2005 12:28 pm Post subject: |
|
|
Show us your code. _________________ ~ Josh
[ Need bot hosting on a dedicated server? PM me. ] |
|
| Back to top |
|
 |
Cer Upgraded Agent

Joined: 03 Feb 2004 Posts: 3776 Location: Michigan
  votes: 4
|
Posted: Thu Jun 16, 2005 12:47 pm Post subject: |
|
|
Just do a regexp:
$file =~ s/\.(txt|html|pl|db|etc|etc)$//ig; |
|
| Back to top |
|
 |
Cheater Senior Member

Joined: 10 Jun 2005 Posts: 236
  
|
Posted: Thu Jun 16, 2005 1:02 pm Post subject: |
|
|
That got rid of the file extension, but it stops listing the files after the first file. Here is the code:
| Code: | sub list {
my ($self,$client,$msg,$listener) = @_;
#!/usr/bin/perl
$dir = "./notes/$client/";
opendir (DIR, $dir);
while ($file = readdir (DIR))
{
$file =~ s/\.(txt|html|pl|db|etc|etc)$//ig;
$reply = "$file\n";
}
close (DIR);
return $reply;
}
}
1; |
|
|
| Back to top |
|
 |
eric256 The Keymaker

Joined: 03 May 2006 Posts: 2292 Location: Colorado
     
|
Posted: Thu Jun 16, 2005 2:09 pm Post subject: |
|
|
| Code: | sub list {
my ($self,$client,$msg,$listener) = @_;
#!/usr/bin/perl #just remove this line already would you?
my $dir = "./notes/$client/"; # always use my when declaring variables
my $reply = "File list:\n";
opendir (DIR, $dir) or return "Error opening file";
while ($file = readdir (DIR))
{
next if $file =~ /\.(txt|html|pl|db|etc|etc)$/;
$reply .= "$file\n";
}
close (DIR);
return $reply;
}
1; |
Important changes:
* USE my
* check for error when opening dir
* next if # this will skip the rest of the code in the block if the condition is true
* $file =~ /\.(txt|html|pl|db|etc|etc)$/; # file ends ($) with any (|) of those letter combinations following a .
* .= instead of = = means replace, .= mean concat (add onto the end of) _________________ Eric256
Proud previous owner and current admin of Bot-depot.com |
|
| Back to top |
|
 |
Cheater Senior Member

Joined: 10 Jun 2005 Posts: 236
  
|
Posted: Thu Jun 16, 2005 2:32 pm Post subject: |
|
|
| Thanks Eric, but about the #!/usr/bin/perl thing, someone told me I needed it at the top. Since you told me to leave it out, I just started wondering what you use it for. |
|
| Back to top |
|
 |
darkmonkey The Merovingian

Joined: 18 Apr 2004 Posts: 2557 Location: London, England
     votes: 7
|
Posted: Thu Jun 16, 2005 3:57 pm Post subject: |
|
|
You need it at the top of your first line of code, ie bot.pl or the file you run. It's a SHEBANG line, and will make it work. *nix requires it, although Windowz is slightly more leneant _________________ ~ Josh
[ Need bot hosting on a dedicated server? PM me. ] |
|
| Back to top |
|
 |
mattaustin Sentinel

Joined: 19 Jul 2004 Posts: 556 Location: Los Angeles, CA
  votes: 1
|
Posted: Thu Jun 16, 2005 8:16 pm Post subject: |
|
|
why not just do something like:
@files = <*.pl>; |
|
| Back to top |
|
 |
|