Guide 2 - Files

The first thing to do with files is to filter them by their extension. Extensions in Perun2 are always in lowercase. Even if certain file is seen as NAME.PDF in the file system.

select files where extension = 'pdf'
select files where extension != 'pdf'

If there are multiple extensions to check, we should use the In operator.

select files
  where extension in ('pdf', 'doc', 'txt')
select files
  where extension not in ('pdf', 'doc', 'txt')

We can exclude one known file.

select files where fullname != 'a.pdf'

Files can be empty, hidden or read-only. This command also demonstrates how logical operators work.

select files
  where empty and not readonly or hidden

Filter files by their name. This command will select files with name starting with the letter a. If you want to know more about the Like operator, go here. Variable name does not contain extension. If you want it included, you should use fullname instead.

select files
  where name like 'a%'

Do exactly the same as above, but in different way. Indexing of characters in Perun2 is zero-based.

select files
  where name[0] = 'a'

There is even a third way. We can use some intuitive functions designed for string manipulation: startsWith(), endsWith(), contains(), substring(), right() etc.

select files
  where startsWith(name, 'a')

Perun2 contains a custom alternative to the Like operator. The Resembles operator checks if the text contains another text, but is very forgiving. It tolerates spelling mistakes (at most one for every 3 characters) and ignores case size. Go here to read about it. Code below would select a file with name containing text 'somthinng' (there are 2 spelling mistakes).

select files
  where name resembles 'something'

You can also use regular expressions if you understand them. Perun2 follows the ECMAScript regex convention and checks are case sensitive.

select files
  where name regexp 'some|thing|else'

Restrict files by length of their name. As written before, variable name does not contain extension.

select files
  where length(name) <= 7

Find files created, recently modified or recently accessed today (or yesterday).

select files
  where creation = today
  or modification = yesterday
  or access <= christmas(2021)

Find files created during the day that was 5 days ago.

select files
  where creation = today - 5 days

Restrict files by their creation time.

select files
  where creation >= april 2022
  or creation < 3 june 2012
  or creation != 3 june 2021, 12:32
  or creation = 3 june 2020, 12:32:21

Check creation year.

select files
  where creation.year in (2021, 2020)

There are more possibilities: month, day, hour, minute, second. Even day of the week.

select files
  where creation.month not in (june, july)
  and creation.weekday = friday

We can check the clock.

select files
  where creation >= 01:23
  and creation <= 15:13

The same command could be written alternatively using the Between operator. This operator also works with other data types like numbers or strings.

select files
  where creation between 01:23 and 15:13

Restrict files by their size. Petabyte (pb) is the greatest available unit for number suffix.

select files
  where size >= 10mb

Check how old files are. Variable lifetime expresses the difference between current moment in time and the oldest of two values: file creation time and last modification time. We can use various units, from seconds to years.

select files
  where lifetime > 20 days

Find the top 10 largest files.

select files
  order by size desc
  limit 10

Statements where, limit, order by are called filters. Filters are performed sequentially one by one. All filters are explained here.

We can do the same by using index. Indexing is zero-based, so the first element has index 0 and so on.

select files
  order by size desc
  where index < 10

Delete all pdf files except for the 100 recently created.

delete '*.pdf'
  order by creation desc
  where index >= 100

Previous commands featured the order by filter. It sorts files by a value ascendingly or descendingly (keyword asc or desc). We can use multiple values for order. Below is an example. We order pdf files based on their name length from the shortest to the longest. If two files have exactly the same name length, then they are compared again by the year of their creation.

print '*.pdf'
  order by length(name) asc,
    creation.year desc
Next