Al Phaba
Al Phaba

Reputation: 6785

How to retrieve the folder name of a given dir?

I have this dir path c:/server/THE/BPS_DATA/THE_1 in my script and I need to retrieve the folder name which is two dirs above this.

In my example I need to retrieve the value THE

I tried it with fileparse of the

my($datapath) = "c:/server/THE/BPS_DATA/THE_1"; 
print " datapath is: $datapath\n";

my($filename, $bpsPath, $suffix) = fileparse($datapath);

Here it returns c:/server/THE/BPS_DATA/

Any advice?

Upvotes: 1

Views: 77

Answers (1)

ikegami
ikegami

Reputation: 386541

use Path::Class qw( dir );
say dir('c:/server/THE/BPS_DATA/THE_1')->parent->parent;

Oh wait, I see you just want "THE"?

use Path::Class qw( dir );
say dir('c:/server/THE/BPS_DATA/THE_1')->parent->parent->basename;

or

use Path::Class qw( dir );
say( (dir('c:/server/THE/BPS_DATA/THE_1')->dir_list)[-3] );

Upvotes: 6

Related Questions