Skip to content
General purpose file system utility routines
Perl 6
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.



General purpose file system utility routines



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');

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');

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');

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();

# a random file
> say mktemp(:f);

# a file (or directory) name of your choosing
> say mktemp(".cache", :f);

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();

> say tmppath(".work");

> say tmppath("/con/con")'

> say tmppath.IO.e;

> say mkdirs(tmppath).IO.e
You can’t perform that action at this time.