Link to home
Start Free TrialLog in
Avatar of n4narik
n4narik

asked on

string manipulation

i have a path to a file: x:\dir1\dir2\test.txt < this path is generated dynamically.
i need a way to get only x:\dir1 from that string. i.e. get the drive and the first directory.
ASKER CERTIFIED SOLUTION
Avatar of Perl_Diver
Perl_Diver

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Adam314
Adam314

You could use a regex:
$path =~ s/(.{3}[^\\]+)/$1/;



or use the File::Spec module:
use File::Spec;
($volume,$dirs,$file)=File::Spec->splitpath($path);
$dir1=(File::Spec->splitdir($dirs))[1];
$path="$volume\\$dir1";