Commands

From Ketarin
Revision as of 21:04, 26 October 2011 by Floele (talk | contribs)

Many actions can be defined using Ketarin's user interface. Some advanced behaviours require custom scripts though.

Command types

Pre-update commands

In the “Commands” tab of each application and in the settings of Ketarin you can enter a command which is to be executed before every update. "Before update" means before it is attempted to download, when an update has been found. By returning various exit codes (eg. "exit 1" when using a batch script) you can influence the behaviour of Ketarin:

Exit code Result
1 Abort download, mark as failed.
2 Skip downloading, mark as "update available" (since version 1.5).
3 Skip downloading, but update the "last updated" date of the application and mark as "update downloaded". Allows you to use an external download application to download the file, but still mark it as properly updated in Ketarin.

Post-update commands

In the “Commands” tab of each application and in the settings of Ketarin you can enter a command which is to be executed after every update. This is useful for unzipping ZIP packages, virus scans, installations in background and other purposes.

Post-update all command

In the settings of Ketarin you can define a command that is executed after all applications have been updated, for example to execute cleanup tasks or sync the results to another location.

Execution order

The commands that can be entered are executed in the following order:

  1. Default pre-update command in settings
  2. Application specific pre-update command
  3. Download of update
  4. Default post-update command in settings
  5. Application specific post-update command
  6. Command to execute after updating all applications

Script types

Ketarin supports Batch scripts and C# scripts. Below every text box in which you can enter a script is a button which lets you choose the script type.

Batch scripts

Batch scripts are simple and useful. They allow you to execute any command you would usually execute on command line. Typical possibilities are renaming, copying and deleting files.

By default, Ketarin waits for the script to be finished before proceeding with the next step. If you do not like that, you can put an ”&” at the very end of the script to execute it in background. You can also temporarily disable a line in the script by adding “rem ” at the beginning.

Command Task
7z x "{file}" -o"{root}PATH\" -y Extract the downloaded file to a given path on the drive where Ketarin.exe is located (7zip required)
7z x "{file}" -o"{root}PATH\" *.exe *.dll -r -y Extract the specified file types from the downloaded file to a given path on the drive where Ketarin.exe is located (7zip required)
7z e "{file}" -o"{root}PATH\" -y Extract the downloaded file to a given path on the drive where Ketarin.exe is located; flat, not preserving the directory structure (7zip required)
7z x "{file}" -o%temp% *.exe -r Extract the specified file types from the downloaded file to the temp directory (7zip required)
...\UniExtract.exe "{file}" "{root}PATH\" Extract the downloaded file to a given path on the drive where Ketarin.exe is located, using UniExtract (unofficial updated release via LupoPenSuite)
start "" /wait ...\UniExtract.exe "{file}" %temp% Extract the downloaded file to to the temp directory, using UniExtract (or via LupoPenSuite)
copy /y %temp%\*.exe "{root}PATH" Copy files of the selected type from the temp directory to a path on the drive where Ketarin.exe is located
robocopy %temp% "{root}PATH" *.exe /e /b /np Copy files of the selected type from the temp directory to a path on the drive where Ketarin.exe is located, using Robocopy
ren "{file}" ABC.msi Rename a file (with wildcard support)
del "{file}" /s /q Delete a file
rd "PATH" /s /q Delete a folder
start "" /wait COMMAND Start a command and wait until it finishes
nircmd elevate cmd /c copy /y "{file}" "%ProgramFiles%" Copy a file with elevated privileges, requires NirCmd, NirCmd Command Reference
nircmd elevate cmd /c Start an elevated command prompt, requires NirCmd
nircmd inisetval "{root}PATH\install.ini" "section" "key" "1" Write value to INI, requires NirCmd
nircmd shortcut "%ProgramFiles%\procexp.exe" "~$folder.desktop$" "{appname}" Create a shortcut to a file on the desktop, requires NirCmd
...wget.exe --output-document={root}PATH\{file:filename} {preupdate-url} Download using Wget download manager, requires Wget (some background)

C# scripts

Additional to Batch scripts you can also create C# scripts. C# can be considered much more powerful than Batch, and you can also directly access the internal data structure of Ketarin. If you know C# (numerous tutorials available on the internet), you can write scripts which perform any actions you like. For simplicity, you do not have to define classes or functions, just the code.

Provided here is a simple C# script that is meant for use after downloading a file. It creates a file containing a string which is the MD5 checksum of the downloaded file. For instance file.bin gets downloaded by Ketarin, file.bin.md5 will be created by the script containing the MD5 text string.

Example C# Code:

string fileName = app.PreviousLocation;
            
  System.IO.FileStream file = new System.IO.FileStream(fileName, System.IO.FileMode.Open);
  System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
  byte[] retVal = md5.ComputeHash(file);
  file.Close();
 
  System.Text.StringBuilder sb = new System.Text.StringBuilder();
  for (int i = 0; i < retVal.Length; i++)
  {
    sb.Append(retVal[i].ToString("x2"));
  }

using (System.IO.StreamWriter outfile = new System.IO.StreamWriter(fileName + @".md5"))
            {
                outfile.Write(sb.ToString().ToUpper());
            }

Snippets

Using the Command button, you can also save and open code snippets. You can create snippets for many generic commands so you won't have to copy/write them again every time.

  • Insert snippet - Insert a custom saved snippet at the current cursor position (select a snippet from the dropdown menu).
  • Save as - Saves the currently selected text (or the contents of the text area if none is selected) as snippet. You can save as new or overwrite an existing one.
  • Delete snippet - Delete a snippet (select a snippet from the dropdown menu).