Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to chop the directory path... What my code does is looks in a file to get the file names and ftp the files to the location in the path. My path varies so I want to get it into a variable and cd to that dir and move the file there. In the path c:/Programfile/Perl/test/temp.txt How can I get c:/Programfile/Perl/test into a variable? Any ideas.... Thanks

Replies are listed 'Best First'.
Re: How to get the Directory Path
by ikegami (Patriarch) on Sep 22, 2005 at 19:36 UTC

    The portable way:

    use File::Spec (); my $path = 'c:/Programfile/Perl/test/temp.txt'; my ($volume, $directories, $file) = File::Spec->splitpath($path); my $dir = File::Spec->catpath($volume, $directories, ''); # Optional: $dir = File::Spec->canonpath($dir); print("$dir\n");

    Update: Fixed typo in var name.

      Thanks a lot... It worked..
Re: How to get the Directory Path
by davidrw (Prior) on Sep 22, 2005 at 20:25 UTC
    Check out File::Basename :
    use File::Basename; my $filename = 'c:/Programfile/Perl/test/temp.txt'; my $dirname = dirname($filename);