General purpose file system utility routines
Perl6
Latest commit 55e827c Jan 27, 2016 @ugexe Add windows tests on appveyor
Permalink
Failed to load latest commit information.
lib Fix flag typo Oct 16, 2015
t Initial commit Oct 8, 2015
.appveyor.yml Add windows tests on appveyor Jan 28, 2016
.travis.yml Test osx on travis Jan 28, 2016
META6.json Update META6.json Jan 13, 2016
README.pod Add windows tests on appveyor Jan 28, 2016

README.pod

PathTools

General purpose file system utility routines

Exports

FLAGS

Definitions of the argument flags that can be passed to PathTools routines:

:f - list files
:d - list directories
:r - recursively visit directories

:!f - exclude files
:!d - exclude directories
:!r - only in the immediate path

routine ls

Args $path, Bool :$f = True, Bool :$d = True, Bool :$r

Returns Str @paths

use PathTools;

# all paths using directory recursion
my @all   = ls($path, :r, :d, :f);

# only *file* paths, found using directory recursion
my @files = ls($path, :r, :!d, :f);

# only directories in the current level (no directory recursion)
my @dirs  = ls($path, :d, :!r);

Like the built-in dir but with optional recursion. Any undocumented additional named arguments passed in will be passed along to the internal mkdir and dir calls used. For instance, one may wish to pass :$test which internally defaults to none('.','..') and is documented further here: dir

> .say for ls('t');
/home/perl6/Perl6-PathTools/t/01-basic.t
/home/perl6/Perl6-PathTools/t/00-sanity.t

To search for files, just grep the results of ls:

> my @files     = ls($path, :r, :!d, :f);
> my @p6modules = @files.grep(*.IO.extension ~~ 'pm6')

routine rm

Args *@paths, :Bool $f = True, Bool :$d = True, Bool :$r

Returns Str @deleted-paths

# rm -rf tmp/foo
my @deleted-files = rm("tmp/foo"), :r, :f, :d);

Passes its arguments to ls and subsequently unlinks the files and/or deletes folders, possibly recursively.

> .say for rm('t');
/home/perl6/Perl6-PathTools/t/01-basic.t
/home/perl6/Perl6-PathTools/t/00-sanity.t

routine mkdirs

Args $paths

Returns Str $created-path

# generate a multi level temporary path name
my $created-path = mkdirs(".work/{$new-month}/{$new-day}")

VM/OS independent folder creation. Identical to the built-in mkdir except the path parts are created folder by folder. This usually isn't needed, but in some edge cases the built-in mkdir fails when creating a multi level folder.

> say mkdirs('newDir/newSubdir');
/home/perl6/newDir/newSubdir

routine mktemp

Args $path?, Bool :$f = False

Returns Str $tmppath

# create a temporary folder and clean it up after program exit
my $cleanup-path = mkdirs("/tmp/.worker{$id}/{time}")

If argument :$f is True it will create a new file to be deleted at END { }. Otherwise, by default, creates a new folder, $path, and will attempt to recursively cleanup its contents at END { }.

If $path is not supplied, a path name will be generated automatically with tmppath

# a random directory
> say mktemp();
/tmp/p6tmppath/1444251805_1

# a random file
> say mktemp(:f);
/tmp/p6tmppath/1444251805_1

# a file (or directory) name of your choosing
> say mktemp(".cache", :f);
/home/perl6/Perl6-PathTools/.cache

routine tmppath

Args $base? = $*TMPDIR

Returns Str $pathname

my $pathname = tmppath(".work")

Generate a (hopefully) unique timestamp based path name that is prefixed by $base. This does not actually create the path; Use mkdirs or mkdir on the result.

> say tmppath();
/tmp/p6tmppath/1444251805_1

> say tmppath(".work");
.work/p6tmppath/1444255482_1

> say tmppath("/con/con")'
/con/con/p6tmppath/1444268295_1

> say tmppath.IO.e;
False

> say mkdirs(tmppath).IO.e
True