Extract filename from full path using perl

By: Sam Chen Emailed: 1673 times Printed: 2152 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

This is a simple trick but comes in handy when you need to extract just the filename from the full path.

#!/usr/bin/perl -w
use strict;
my @temp;
my $string;
my $filename;
$string = "/var/spool/email2fax/1233123/Agoda-Booking-Sep-2012.tiff";
for ( reverse @{[ split( '', $string ) ]} )
{
   last if /\//;
   unshift @temp, $_;
}

$filename = join '', @temp;
print $filename;
exit;

Perl Home | All Perl Tutorials | Latest Perl Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(0)


Be the first one to add a comment

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Sam Chen
Extract filename from full path using perl
Encrypting Passwords in Tomcat using Servlets
malloc, calloc - Storage Management - in C
Character Pointers and Functions in C
Declarations in C
strcpy() and strncpy() sample program in C++
Sample program to draw a rectangle in J2ME
Design Patterns for Properties in a Java Bean
PushbackInputStream example program in Java
FileInputStream - sample program in Java

More Tutorials in Perl
Steps to Install perl-Apache-DBI in linux
Extract filename from full path using perl
Flush history file on disk (unique hosts reach flush limit of 20000) - Awstats
What is Perl?
History of Perl?
Why was it named Perl?
Major features of Perl
The design of Perl
Perl Applications
Implementation of Perl
Perl for Windows
Perl6 - The future of Perl
POST to URL from Perl Script

More Latest News
Most Viewed Articles (in Perl )
What is Perl?
Flush history file on disk (unique hosts reach flush limit of 20000) - Awstats
Extract filename from full path using perl
POST to URL from Perl Script
History of Perl?
Why was it named Perl?
Major features of Perl
The design of Perl
Perl Applications
Implementation of Perl
Perl for Windows
Perl6 - The future of Perl
Steps to Install perl-Apache-DBI in linux
Most Emailed Articles (in Perl)
Why was it named Perl?
The design of Perl
Perl for Windows
History of Perl?
Perl6 - The future of Perl
Extract filename from full path using perl
What is Perl?
Major features of Perl
Perl Applications
Implementation of Perl
POST to URL from Perl Script
Flush history file on disk (unique hosts reach flush limit of 20000) - Awstats
Steps to Install perl-Apache-DBI in linux