The main problem is your use of invalid escapes:
use warnings;
print "Root\ToOrganization\Service_b37189b3-8505-4395_Out_BackOffice.xml";
Unrecognized escape \T passed through at ... line 2.
Unrecognized escape \S passed through at ... line 2.
RootToOrganizationService_b37189b3-8505-4395_Out_BackOffice.xml
So your $file
variable doesn't contain what you think it does.
Your rindex
call itself is fine, but you can simply do this (assuming you are on a Windows system):
use strict;
use warnings;
use File::Basename;
my $path = "Root\\ToOrganization\\Service_b37189b3-8505-4395_Out_BackOffice.xml";
my $dir = dirname($path);
print "dir = $dir\n";
Or (this should work on any system):
use strict;
use warnings;
use File::Spec::Win32;
my $path = "Root\\ToOrganization\\Service_b37189b3-8505-4395_Out_BackOffice.xml";
my $dir = (File::Spec::Win32->splitpath($path))[1];
print "dir = $dir\n";
Note that if this is actually a real windows path, the code above will strip the drive letter (it's the first element of the list returned by splitpath
).