1. Perl
  2. File operation
  3. here

Extract the directory name and base name from the file name

To extract the directory name and base name from the file name, use the fileparse method of File::Basename module.

use File::Basename 'fileparse';
my ($base_name, $dir) = fileparse $file;

You can use the fileparse function of the File::Basename module to separate the file name from the directory name and the base name. Note that the 0th in the list of return values is the base name and the 1st is the directory name, which is the reverse of the original order.

File::Basename is created to be OS independent. On Unix, Windows, and MacOS, the file delimiters are different, but they can be used without problems.

Example

This is an example to extract the directory name and base name from the file name.
use strict;
use warnings;

# Extract the directory name and base name from the file name

use File::Basename 'fileparse';

my $file_unix = 'dir1/dir2/base_name.txt';
my $file_win = 'dir1 \\dir2 \\base_name.txt';
                               
print "1: Extract the directory name and base name from the file name.\n";
my ($base_name, $dir) = fileparse $file_unix;
print "For Unix\n".
  "\$dir = $dir\n".
  "\$base_name = $base_name\n\n";

($base_name, $dir) = fileparse $file_win;
print "For Windows\n".
  "\$dir = $dir\n".
  "\$base_name = $base_name\n\n";
      
print "2: Extract only the base name.\n";
# In the array slice, extract only the 0th of the list.
my $base_name_only = (fileparse $file_unix) [0];
print "\$base_name_only = $base_name_only\n\n";

Output

1: Extract the directory name and base name from the file name.
For Unix
$dir = dir1/dir2/$base_name = base_name.txt

For Windows
$dir = dir1\dir2\$base_name = base_name.txt

2: Extract only the base name.
$base_name_only = base_name.txt

Perl Reverse Dictionary

Related Informatrion