File Management

Sometimes you have to copy files before you launch the main program exe. Let’s see an easy example where we should copy an .ini file to the Windows directory.

The chosen software is Kleptomania, which allows you to capture and process images and text from any (even clipboard-unaware) application. You can copy text onto the clipboard, launch your internet browser or email program for further editing, sum numbers, count a number of words/characters and more.

Capturing Changes

So start up Total Uninstall and click on Install. Drag and drop “InstallKleptomania 2.5kleptomania_setup.exe” file to TU’s window. After the system scan, install Kleptomania using Total Uninstall. I won’t write down every little steps like I did in the previous chapter. If you get stuck somewhere, go to Chapter II and look for help there.

Install with default settings, so basically the only thing you have to do is clicking on Next every time. When the install finishes, let the install program launch Kleptomania. Kleptomania will now build up a font database. When it’s done, close Kleptomania and go back to TU and examine what changes were made.

In the File System section there’s only one entry important: k-mania.Ini in the Windows directory (apart from the main program files in “C:Program FilesKleptomania”).

We should copy these files (program files and k-mania.Ini to a directory, so copy them i.e. to “C:Portable KleptomaniaAppdata”. First you have to create the directory structure we used in Chapter II, “Bringing NSIS To The Game”.

Registry changes

The registry changes captured by Total Uninstall show nothing important, except from

“HKEY_LOCAL_MACHINESOFTWAREMicrosoftWindowsCurrentVersionUninstallKleptomania 2.5″

This is required when you want to uninstall Kleptomania, but as you’ve seen in Chapter II it is not written to the registry when you launch the portable version of a program.

Because there are no other changes we should pay attention to, so uninstall Kleptomania. Make sure you have copied all program files and k-mania.Ini to “C:Portable Kleptomania” directory.

So we have the program files and the settings file (k-mania.Ini). Let’s examine the latter one.

First, search for any path in this .ini file (the easiest way is to search for paths is searching for “:”).

There are two of them:

Install Path=C:PROGRA~1KLEPTO~1

Database Path=C:PROGRA~1KLEPTO~1k-mania.Pat

Change it to relative paths, like so:

Install Path=.Appdata

Database Path=.Appdatak-mania.Pat

Unfortunately there’s no entry which defines the location of k-mania.Ini (its location is hard-coded).

That’s why we should copy it prior to the main program’s launch.

Some program, if they find their .ini files in their own directory, will use it instead of the .ini file in its installed location. To test this, delete k-mania.ini from the Windows directory and make sure in the program’s directory there’s the copy of it. Now launch Kleptomania with k-mania.exe.

An error message appears, telling that k-mania.Ini not found. So there’s nothing left, we have to copy this ini file to Windows directory. It is not so good because if you have no admin rights on the target PC, you won’t be able to run Kleptomania.

To copy a file with NSIS, use this code:

CopyFiles /SILENT “$EXEDIRAppdatak-mania.Ini” “$WINDIR”

/SILENT tells NSIS to copy files in the background.

“$WINDIR” is the destination directory (“C:Windows” if Windows is located there).

Because we want our settings to be saved, we’ll copy this file back to the “Appdata” directory after k-mania.exe is terminated:

CopyFiles /SILENT “$WINDIRk-mania.Ini” “$EXEDIRAppdata”

This is the same code but with inverted directions.

We also want to keep Windows directory clean, so at the end of the scrip we’ll delete k-mania.Ini file:

Delete “$WINDIRk-mania.Ini”

Because there’s no registry settings to bother with, the NSIS script is very simple (you have to pay attention to the words written in red):

;—Definitions—-

!define SNAME “Portable Kleptomania”

;—-Includes—-

!include “Registry.nsh”

;—–Runtime switches—-

CRCCheck off

AutoCloseWindow True

SilentInstall silent

WindowIcon off

XPSTYLE on

;—–Set basic information—–

Name “${SNAME}”

Icon “${SNAME}.ico”

Caption “${SNAME} Launcher”

OutFile “..${SNAME}.exe”

;—–Version Information——

LoadLanguageFile “${NSISDIR}ContribLanguage filesEnglish.nlf”

VIProductVersion “0.1.0.0″

VIAddVersionKey /LANG=${LANG_ENGLISH} “ProductName” “Portable Kleptomania Launcher”

VIAddVersionKey /LANG=${LANG_ENGLISH} “LegalCopyright” “© tpr 2007″

VIAddVersionKey /LANG=${LANG_ENGLISH} “FileDescription” “Portable Kleptomania”

VIAddVersionKey /LANG=${LANG_ENGLISH} “FileVersion” “0.1″

Section “Main”

CopyFiles /SILENT “$EXEDIRAppdatak-mania.Ini” “$WINDIR”

Sleep 300

ExecWait “$EXEDIRAppdatak-mania.exe”

CopyFiles /SILENT “$WINDIRk-mania.Ini” “$EXEDIRAppdata”

Sleep 500

Delete “$WINDIRk-mania.Ini”

SectionEnd

Note that there’s a “Sleep 500″ before deleting k-mania.Ini.

Save this script with name “Portable Kleptomania.nsi” in directory “C:Portable KleptomaniaSource”.

Now only the icon is missing, so extract it from k-mania.exe with Icons From File, and save it with name “Portable Kleptomania.ico” in directory “C:Portable KleptomaniaSource”. Remember that the name of the .nsi file and the .ico file must be the same.

Now open makeNSISw.exe, drag and drop the script file “Portable Kleptomania.nsi” and theoretically the launcher exe is created in the parent directory (“C:Portable KleptomaniaPortable Kleptomania.exe”).

Do some testing to see if everything works as expected. You can use some of the tools if you wish:

• First of all, copy the whole “Portable Kleptomania” directory to a USB drive and try it on another PC (or copy it over a lan if available). Try it on as many computers as you can.
• Total Uninstall: instead of a setup file, use the program’s main exe file. Let TU do a system scan and then let it launch the exe. Do some things in the program and close it. Now see what changes were made to your system.
• RegMon, FileMon: monitor the portable’s activity and see if everything works as expected.
• Launch the portable, then rename k-mania.Ini file in the Appdata folder. Now close Kleptomania and see if the k-mania.Ini file is copied back or not to the Appdata folder. If not, check your script (and, of course, rename the .ini file back).
• Launch Portable Kleptomania and change some settings in it (in the Properties page). Now close it and see if it is reflected in the .ini file. Now launch it again and see if your settings came back or not.


Note: you can use wildcards to copy files if you wish. For example,

CopyFiles /SILENT “$EXEDIRAppdata*.*” “$WINDIR”

will copy all files (and directories) from the Appdata directory to the Windows directory. Use it with care, especially when deleting files in this way.

Another note: if you copy files this way, you may overwrite the local installation’s settings file (if any). To prevent this, you should rename the local installation’s k-mania.Ini file before launching portable Kleptomania, and at the end you should restore its original name. For example, if you rename the local “k-mania”.Ini to “k-mania_.Ini” will keep it from overwriting. In the next topic you’ll see how to rename files with NSIS.

One response to “File Management”

  1. wilsont3ch.com » How To Make Portables

    [...] 6. File Management [...]