develooper Front page | perl.perl6.users | Postings from February 2018

Open files by given name and extension and ask for deletion

From:
mimosinnet
Date:
February 13, 2018 00:28
Subject:
Open files by given name and extension and ask for deletion
Message ID:
20180213002758.GA26073@mimomedia
After a while, a bunch of files asking for cleanup rest in some folders (like 'downloads'). 

This script allows to see whats in a set of 'pattern.extension' files 
(using Linux xdg-open) and asks for deletion (moving to the /tmp 
folder). 

I leave it here for consideration and improvement. Also, it may be 
helpful to other Perl6 newcomers.

Happy Perl6!

---
#!/usr/bin/env perl6
use v6;

#| Opens 'pattern.ext' files and asks for deletion. Write 'all' for 
selecting all files.
sub MAIN( $pattern is copy, Str $ext ) {

	$pattern = "" if $pattern eq "all";
	my @files = '.'.IO.dir(test => /.*$pattern.*\.$ext/);

	for @files -> $file {
		my @args = 'xdg-open', $file.basename;
		my $command = run @args;
		$command.exitcode == 0 or die "system @args failed: $!";
		my $delete = prompt("\n \n Delete file $file (s/n) ");
		last if $delete eq "";
		next if $delete ne "s";
		say "mv $file /tmp/$file";
		$file.IO.rename("/tmp/$file");
		prompt("\n Press 'return' to continue ");
	}

	@files = '.'.IO.dir(test => /.*$ext$/);
	say "-" x 60;
	for @files -> $file { $file.Str.say }
	say "-" x 60;
}
---

-- 
(≧∇≦) Mimosinnet (Linux User: #463211)



nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About