Kampot Tool

I love robocopy.exe and have been using it since 1996, it's powerful filtering when copying files is very handy for many operations.

The problem is that there is no API for it. Under newer Windows versions you are no longer able to call executables from a Windows Service, which was possible in the past. So I can no longer use Robocopy from a service.

Another requirement I had was to zip up all files changed today. I would first use robocopy to a second location filtering for new files and then zip all files there and then delete them.

So I wrote my own basic Robocopy clone and added some more features.

Kampot.exe is a command line tool, the only parameter it takes is the name of a 'Kampot Command file' which is a bit like the Robocopy job file. The command file is XML based and has one or multiple actions to be performed. For details about the format see the examples below.
Usage:
kampot.exe command file  [-v] [-s] [-r] [-f]

-v verbose, show lots of information during execution
-s silent, don't show any information during execution
-r report, don't show the report at the end of the execution
-f files, list the affected files at the end of the execution
All four switches just affect what is displayed on the command line. All configuration of the actual execution is done in the command files or the kampot.config configuration file.
Examples:
If you read this whole page, it may sound pretty complicated, but looking at the following examples should get you going.

Some of the things you see in the examples are described below in the Advanced Command File Features section.
Backup new source code
I write a lot of code and at the end of the day I want to upload all changed or new files to an online storage. Before Kampot I would use robocopy.exe to copy all new files except for DLLs and EXEs to a new location, then zip them into one file and finally manually upload that file. Now I can use this command file:
<?xml version="1.0" ?>
<kampot version="2.0">
<actions>
  <inkapack source="%data%/Code" 
    destination="%working%backup/RecentCode_%time%.zip"
    excludefiles="pdb$|dll$|exe$|suo$|user$" 
    excludefolders="Backup|obj|debug|logs|temp|\.svn"     
    encrypt="true" age="15" size="-200"
    recursive="true" reportlevel="1" updatepackage="false" />
    
   <skydrive file="%last%" rootfolder="Backup" folder="Code" />
    
</actions>
</kampot>
- Zip files in the code folder under the data directory as defined in the global config file.
- The zip file will be in the working directory/backup/ and the file name has the current time
- exclude any files with the extenions: .pdb, .dll, .exe, .suo, .user
- exclude a whole bunch of folder names
- encrypt the zip file.
- Only zip files less than 15 hours old
- Only zip files less than 200kb in size
- Include subdirectories
- Use a report level of 1
- Do not add to an existing zip file with the same name
- Upload the file just zipped to skydrive.live.com into the location /Backup/Code/
Delete old files
I have a lot of log files sitting around and after a while I don't care about them anymore, with Kampot I write a simple command file and I'm done.
<?xml version="1.0" ?>
<kampot version="2.0">
<actions>
   <inkadelete source="d:\logs\" age="-240" />
</actions>
</kampot>
- Delete all files under D:\logs and subdirectories (default)
- they must be older than 240 hours or 10 days.
Change a connection string in web.config
On my development box, I use the same asp.net site with different databases, rather than changing the connection string I use a master web.config file:
  <connectionStrings>
    <add name="Main"   
    connectionString="Data Source={%server%};Initial Catalog={%database%};user=myuser;pwd=*********"/>
  </connectionStrings>
and then use a CopyTemplate Action:
<?xml version="1.0" ?>
<kampot version="1.0">
  <actions>
    <copytemplate source="%data%\master_web.config" 
         destination="C:\inetpub\wwwroot\web.config" >
      <token name="server" value="db01" />
      <token name="database" value="Dev-Database" />
    </copytemplate>
  </actions>
</kampot>
For the other databases, I have different command files. You can have an unlimited number of tokens defined.
The Inka Actions:

InkaList: List files according to the specified filters
InkaCopy: Copies files according to the specified filters
InkaPack: Zips files according to the specified filters
InkaDelete: Deletes files according to the specified filters

They all share a common set of attributes which define how they work:

source = The source directory for the files you want to process.

destination = The destination, a folder for InkaCopy, a file for InkaPack and not relevant for InkaList and InkaDelete.

excludefiles = exclude files whose filename match the expression. This looks at the filename only not the path. A common expression is exe$ which means any filename that ends in exe.

excludefolders = exclude folders whose names that match the expression, this tests only a single folder name not a full path. However it tests all folders in the path beginning at the top. If you have something like C:\myData\temp\word\letter\ and the expression is temp if will not even look anywhere below C:\myData\temp\.

excludepath = excludes files whose full absolute file names match the expression. This compares the full path rather than a single file name or folder name. If you want to exclude images but only if they are somewhere under a temp directory, you should use: temp.+(jpg$)|(png$)|(gif$) The .+ stands for any number of any characters.

includefiles = only file names matching this expression are included. This is an inclusion rule and looks at file names only. If this doesn't exist or is empty, all files will be processed.

recursive = The allowed values are 'true' and 'false' and the default is true. If set to 'false' only the initial folder is processed without traversing into subdirectories.

age = Exclude files that are older or newer a certain number of hours. -10 includes all files that are older than 10 hours. 24 includes all files that have been changed in the last 24 hours. 0 ignores the age of the file.

size = Exclude files that are smaller or bigger than a certain number of kBytes -10 includes all files that are smaller then 10kb, 1000 includes all files that are bigger than 1000kb. 0 ignores the size of the file.

stoponerrors Normally if a directory does not exist, the whole command file execution is terminated, you can set this to false to ignore the problem and continue with the next command.

When copying files, they are only copied when the same file does not yet exist at the destination.

The InkaPack action has three additional attributes:

encrypt = The allowed values are 'true' and 'false'. Default is false, when set to 'true' the zip file is encrypted with an AES256 algorithm. You can view the list of the files in the zip file in Windows Explorer, but to extract the content you need a tool that supports this decryption such as WinZip or 7Zip. The password for the encryption has to be specified in kampot.config in the setting 'ContentPassword'. It has to be encrypted, see 'Encrypting passwords' below for more details.

updatepackage = The allowed values are 'true' and 'false'. Default is false, if set to 'true' the files are added to a file with the same name if it exists, if set to 'false' that file will be deleted and a new file will be created.

pathhandling = Prior to version 2.24 the path recorded in the zip was the full original one from the hard drive where it was zipped. That was hardly ever needed and more often annoying. Since Version 2.24 by default only the path relative to the start folder is recorded in the zip file. This setting can be set to one of the following options:

With all four settings there is a chance for an error when there are two files with the same name and path information. This can not be stored in a zip file. Even the full path is not save, because you can later add a file from a different drive.

Regular Expressions:
The exclude and include attributes use regular expressions to filter files to use in the action. These are much more powerful than the usual Windows *.* wildcards but also harder to write. If you don't know them, look at the examples above to get an idea.
Encrypting passwords:

Any passwords that can be used by Kampot, must be encrypted in the config file.
We are using the Windows built-in encryption. You encrypt your password and store it in the kampot.config file. Kampot can then decrypt it as long as it runs on the same computer under the same user account that encrypted it. So if you move computers or change you account, you have to encrypt the password again.

To encrypt your password, use the program creel.exe that comes with Kampot. Just start it and type your password into the upper text box and click 'Encrypt'. The encrypted text is displayed in the lower textbox and also copied into your clipboard. Just paste the text into the value field in the config file.

The Copy Template Action:
Copies a template file to a new location while replacing certain tokens in it. See the example above.
The SkyDrive Action:

Uploads a file to Microsoft SkyDrive online storage service.

The attibutes are:

file = The single file that should be uploaded. The %last% token is often used here to reference a file created by a previous action.

rootfolder = The name of the folder below the root of your SkyDrive. This has to be specified.

folder = The name of the folder on the second level. If empty the file is uploaded into the folder specified in rootfolder. There is currently no way to upload files into lower levels.

In addition two settings are required in kampot.config

SkyDriveUserName = Your username for Windows Live.

SkyDrivePassword = Your Windows Live password. This has to be present in an encrypted form. See the 'Encrypting passwords' section for more details.

The configuration file: kampot.config
This file holds the global configuration for Kampot, there are settings for the program itself as well as some for the actions performed.
Only change the settings in the <appSettings> section. The four directories are used as global tokens. The other settings are for specific actions.
Advanced Command File Features:
When using command files with multiple actions you often repeat yourself or you want to refer to a file that has been created in a previous action.
The <parameters> section.
At the beginning of the command line you can have a parameters section where you can define parameters which are used as the default values for all following actions. The actions can overwrite the parameters but if they don't specify them, the defaults are used. The elements in the parameter section must have the same name as the attributes in the action node.
<parameters>
    <excludefiles>pdb$|dll$|exe$</excludefiles>
</parameters>

<actions>
   <inkalist source="C:\code\" />
   <inkalist source="C:\docs\" excludefiles="temp$" />
</actions>
The first action uses the excludefiles as specified in the parameters section. The second one overrides it.
Tokens
There are five types of tokens that can be used in command files:
You use them by wrapping their name in two percent characters:
<actions>
   <inkalist source="%myTestFolder%foo" />
</actions>
kampot.config tokens
Defined in the kampot.config configuration file and therefor available to all command files.
<appSettings>
    <add key="token_theTestFolder" value="F:\hosts\tools\Kampot\" />
    <add key="token_myname" value="Peter" />
</appSettings>
All keys in the AppSettings section that begin with token_ are considered global tokens, however to use them in the command files, you only use the second part after the 'token_' bit.

local tokens
These are defined in the command file itself like this:
<tokens>
<myTestFolder>F:\hosts\tools\Kampot\</myTestFolder>
</tokens>

<actions>
   <inkalist source="%myTestFolder%foo" />
</actions>
Builtin tokens
%last% = refers to the file created by the previous action. Not all actions create a file so this may be empty.
%time% = the current date and time in the format 20090228T235825 for 28th of February 2009, 11pm 58 minutes 25 seconds. This is useful for creating files with a unique name.
%date% = the current date in the format 20090228 for 28th of February 2009.
%lasttwomonths%, %lastthreemonths% and %lastsixmonths% = These are special token that expand into a yearMonth regular expression. Say we have August 2013, the first one would expand to '2013(07|08)', say we have February 2014, the second one would expand to '(201312|201401|201402)'. This is to match files or folders that are named this way. I needed this because I had to match folders based on their names rather than on the age of the files they contain. (New in version. 2.47)
Command Line tokens

Sometimes you need a similar command file for two operations. You can now supply tokens on the command line, they are handled just like the tokens you specify in the tokens section of the command files.
Use tokens on the command line like this:

kampot.exe -token_targetDrive=E
kampot.exe -token_myname="Joe Block"
The '-token_' is just the identifier for a token, the actually token names here are 'targerDrive' and 'myname'.

[Tokens were added in version 2.13]
Windows Environment tokens

After all these application specific tokens have been processed, the final step in the token substitution is using Windows Environment variables. The have the same percent sign format anyways. So any found environment variables are replaced with their value.

[Windows Environment token were added in version 2.41]

Advanced Topics:

Use Kampot as a Service - Rather than starting it from the command line with the command file as a parameter you run it as a service and drop the command files into a directory. I use this on web servers where web applications drop command files to perform actions that they are normally not allowed to.

Use the API - Both the command line tool and the service are just clients to the Kampot API, if you are using .net you can use the API directly with or even without command files.

Write your own Action Provider - Each action is executed by a provider. Kampot can be extended by adding more providers which then can handle new actions.

Documentation about these topics is coming later. If you are interested, please let me know.
Missing RoboCopy features:
There are lots of features missing, but I when I need them I can still use robocopy.
License:
Free for personal use. Contact me for commercial use.
System Requirements:
.Net Framework 3.5 or newer.
Download:
Get it from my download page
Installation:
Extract the zip file to an empty folder on your hard drive, open kampot.config in notepad and adjust the folders if you want to use them.
Some example command files are included.
Release Notes:
20-Nov-2013: Added missing assembly file to distribution. Also added supportedRuntime attributes to config to enable execution under both .Net frameworks 3.5 and 4.x

Pages in this section