User Control Panel
Advertisements

HELP US, HELP YOU!

Exclude Extension

 
Post new topic   Reply to topic    Bot Depot Forum Index -> General Programming Questions
View unanswered posts
Author Message
Cheater
Senior Member
Senior Member


Joined: 10 Jun 2005
Posts: 236

Reputation: 15.8Reputation: 15.8

PostPosted: Thu Jun 16, 2005 12:13 pm    Post subject: Exclude Extension Reply with quote

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
The Merovingian


Joined: 18 Apr 2004
Posts: 2557
Location: London, England
Reputation: 39.3Reputation: 39.3Reputation: 39.3Reputation: 39.3
votes: 7

PostPosted: Thu Jun 16, 2005 12:28 pm    Post subject: Reply with quote

Show us your code.
_________________
~ Josh
[ Need bot hosting on a dedicated server? PM me. ]
Back to top
Cer
Upgraded Agent
Upgraded Agent


Joined: 03 Feb 2004
Posts: 3776
Location: Michigan
Reputation: 146.9
votes: 4

PostPosted: Thu Jun 16, 2005 12:47 pm    Post subject: Reply with quote

Just do a regexp:

$file =~ s/\.(txt|html|pl|db|etc|etc)$//ig;
Back to top
Cheater
Senior Member
Senior Member


Joined: 10 Jun 2005
Posts: 236

Reputation: 15.8Reputation: 15.8

PostPosted: Thu Jun 16, 2005 1:02 pm    Post subject: Reply with quote

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
The Keymaker


Joined: 03 May 2006
Posts: 2292
Location: Colorado
Reputation: 47Reputation: 47Reputation: 47Reputation: 47Reputation: 47

PostPosted: Thu Jun 16, 2005 2:09 pm    Post subject: Reply with quote

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
Senior Member


Joined: 10 Jun 2005
Posts: 236

Reputation: 15.8Reputation: 15.8

PostPosted: Thu Jun 16, 2005 2:32 pm    Post subject: Reply with quote

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
The Merovingian


Joined: 18 Apr 2004
Posts: 2557
Location: London, England
Reputation: 39.3Reputation: 39.3Reputation: 39.3Reputation: 39.3
votes: 7

PostPosted: Thu Jun 16, 2005 3:57 pm    Post subject: Reply with quote

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
Sentinel


Joined: 19 Jul 2004
Posts: 556
Location: Los Angeles, CA
Reputation: 50.7
votes: 1

PostPosted: Thu Jun 16, 2005 8:16 pm    Post subject: Reply with quote

why not just do something like:

@files = <*.pl>;
Back to top
Display posts from previous:   
Post new topic   Reply to topic    Bot Depot Forum Index -> General Programming Questions All times are GMT
Page 1 of 1

 



Protected by phpBB Security phpBB-TweakS
phpBB Security Has Blocked 9 Exploit Attempts.
Antispam Captcha Mod by phpbb-security.com
Powered by phpBB © 2001, 2005 phpBB Group