Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the path without the filename in Windows using Perl?

I am looking to get the path without the filename in windows through perl

I have the full path like this:

c:\My_Designs\ipcore_script\test\src\IP_CORE\mux\sus.do

I need only the path and want to strip off the filename like:

c:\My_Designs\ipcore_script\test\src\IP_CORE\mux

like image 206
mitt2050 Avatar asked Dec 17 '22 06:12

mitt2050


2 Answers

Use File::Basename

> perl -MFile::Basename -wE "say dirname(qq(c:/perl/bin/perl.exe));"
c:/perl/bin
like image 91
TLP Avatar answered Dec 21 '22 23:12

TLP


You can also use Path::Class:

#!/usr/bin/env perl

use strict;
use warnings;

use Path::Class;

my $file = file('c:\My_Designs\ipcore_script\test\src\IP_CORE\mux\sus.do');

print $file->parent, "\n";

Output:

C:\My_Designs\ipcore_script\test\src\IP_CORE\mux
like image 45
Sinan Ünür Avatar answered Dec 21 '22 22:12

Sinan Ünür