Jump to content
Ketarin forum

TUTORIAL : Example use of Powershell in Ketarin : Retaining X installers


Ambimind
 Share

Recommended Posts

Motivation


Ketarin has a built-in feature to remove the last downloaded file.

But, what if one wanted to keep a history of 2 or 3 or X prior versions; and to delete the oldest file only, as newer version became available, thus maintaining a fixed number of installers?

 

And why not, while we're at it, get a sense of how the new Powershell integration works in Ketarin?

 

These will be our aims.

 

Solution


1. Take any one of your Ketarin applications(jobs), for which you require a history of installers, lets say a maximum of 3.

 

2. In this case it is important that each new downloaded installer has a unique name. If the publisher does not provide for this, you may use the following solution :

 

{appname}_{f:yyyy}-{f:MM}-{f:dd}.{url:ext}

 

bI02WC3.png

 

3. Next we will input the Powershell script. First, as shown below, change the command type to "Powershell script".

Note that the script will be executing only AFTER the new file is downloaded.

zCZHqPM.png

 

3.1. Into the input(purple border) shown in Image 2, paste the following Powershell script :

 

#PURPOSE:
# Ketarine can delete the previous downloaded file.
# However there are circumstances when 2 or more copies are desirable.

# This script automates the keeping multiple copies.
# $keep determines number of copies.
#ASSUMPTIONS:
#   1. That script is run after new file download.
#   2. That downloaded file is unique.
#   3. That there are no other files, but program installers, in download location(See Note 2).
#NOTE:
#   1. Any files previously downloaded, numbering more then $keep, will be deleted.
#   2. Subfolders will not be deleted, store supporting files there.

#Number of files to keep
$keep = 3

# Location variables
$file = $app.CurrentLocation
$fdir = (Get-Item $file).DirectoryName
$fnm = (Get-Item $file).Basename
$fext = (Get-Item $file).Extension
$fnmext = (Get-Item $file).Name
$kroot = PWD

#Change dir to current job dir
CD $fdir

#An object containing file refs, sorted by creation time(first being newest)
$fsorted= Get-ChildItem -File | Sort-Object -Property LastWriteTime -descending

#Select the newest files, as specified by $keep
$newset = $fsorted | Select-Object -First $keep

#Delete the others
$fsorted | Remove-Item -Exclude $newset
 

 

Remarks :


It is important to note that before proceeding to run the script, one must place all supporting, non-installer files in a sub-folder. In addition, it is assumed that each job/app has its own folder.

 

Please also refer to the following link, to understand the purpose of the variables at the start of the script : https://ketarin.org/forum/topic/3940-tip-ketarin-variable-equivalents-in-powershell/?p=10697

 

This is of course a very simple script, however it should now be clear where and in what capacity one can use Powershell in Ketarin. Also now you have a convenient way to maintain more then one installer per job, while limiting the total number.

 

 
 

Link to comment
Share on other sites

Since you're already performing a GCI on there and collecting valuable input for the base name, you should be able to store everything in one folder with something like this:

 

 

$fsorted = Get-ChildItem -File -Name $app.Name"_*."$fext | Sort-Object -Property LastWriteTime -descending

 

That should (assuming you pattern your downloaded file names similarly) correctly select only matching files for filtering, then removal, thus eliminating the need to use a unique folder per application.

Link to comment
Share on other sites

Thank you for sharing. This is almost exactly what I would like to implement for several of my apps. :)

Glad to know it.

 

Since you're already performing a GCI on there and collecting valuable input for the base name, you should be able to store everything in one folder with something like this:

$fsorted = Get-ChildItem -File -Name $app.Name"_*."$fext | Sort-Object -Property LastWriteTime -descending

That should (assuming you pattern your downloaded file names similarly) correctly select only matching files for filtering, then removal, thus eliminating the need to use a unique folder per application.

Thanks for the tip. To my surprise this actually works, despite the fact that "-Name" returns an array of filenames, rather then a collection of "FileInfo" objects.

 

As you suggest, the script implementation becomes dependent on file name form; I wanted to avoid this manual alignment and re-alignment when change occurs. Also in some of my jobs the supporting files have similar names and extensions to the installers. So I took the blanket approach.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.