More File Management

In this topic I’ll show you how to create and delete a directory and how to rename files.

Creating Directories

Knowing how to create a directory with NSIS is necessary because if you have to copy files, NSIS will not copy anything if the destination directory doesn’t exists. It is a bit annoying but fortunately it is very easy to create a directory (the example is from the NSIS Help):

4.9.3.3 CreateDirectory

path_to_create

Creates (recursively if necessary) the specified directory. The error flag is set if the directory couldn’t be created.

You should always specify an absolute path.

CreateDirectory $INSTDIRsomedirectory

What is important to note from it:

- creates directories recursively: so, if you would create a subdirectory structure, you should only create the last directory in it, like so:

CreateDirectory “C:RootDirSubdir1Subdir2Subdir3″

In this case, if there was no “Rootdir” earlier, 4 directories will be created.

- absolute path: you shouldn’t specifiy a directory with relative path, like ‘CreateDirectory “MyFolder” ‘ or ‘CreateDirectory “..” ‘ because it won’t work.

In fact, specifying directories which are relative to the launcher’s path is very easy with NSIS as you’ve seen earlier (think of “$EXEDIR” or “$APPDATA”).

You should also use the double quotes (“) when specifying a path, to ensure that NSIS will recognize them properly. For example, if you specify the path “C:My very new directoryhey this is newereven newer directoryhelp how to get out of here” without quotes, NSIS will give you an error because it thinks that the spaces in in are delimiters. However, this is acceptable (with no quotes):

CreateDirectory C:RootDirSubdir1Subdir2Subdir3

because it has no spaces in names of the directories. You should also pay attention to this when working with registry keys.

When will you need to create a directory? For example, when an application stores its settings in the application data folder. Then you should copy all files to this directory from you USB drive prior to launching the main exe. When you close the program, the launcher should copy all files from application data folder back to the USB drive. Doing so, on next run your settings will be the same on every PC, no need to reconfigure the application.

To create a directory in the user’s application data directory, use this code:

CreateDirectory “$APPDATAMySoftwareSettings”

Note that this “$APPDATA” is not the same as “Appdata”. “$APPDATA” is substituted by NSIS to

“C:Documents and SettingsYOURUSERNAMEApplication Data”

where YOURUSERNAME is the logged in user’s name (which is probably different on another computer).

Appdata is, on the other hand, the directory where we keep the main program files.

Deleting Directories

In some cases you may want to delete a directory. It is very simple with NSIS:

RMDir “$APPDATAMySoftwareSettings”

This will remove the “Settings” directory from “Application DataMySoftware” directory. If you add an extra “/r” switch after RMDir, it will delete all files from the specified directory and from its subdirectories. So, if you got “Settings” directory with a “Data” subdirectory, using the “/r” switch will remove all files in the “Settings” directory and also removes its “Data” subdirectory.

RMDir /r “$APPDATAMySoftwareSettings”

Renaming Files

You may find yourself in a situation when you have to rename some files. You can do it for example if you would like to keep local installation files intact or when a software stores its settings in a file or folder that must contain the logged in user’s name.

In the first situation, you should rename the local installation’s file (or files) before launching the main exe, and after closing it, you should restore its original name. It is somewhat better than overwriting the original one (as we would do with k-mania.Ini in the previous topic, if a local Kleptomania is present on the host computer) because if our portable accidentally crashes, the original file would be in its original location, we only need to restore its original name.

To rename a file, use this code:

Rename “$EXEDIRBackupsettings.ini” “$EXEDIRBackupsettings_new.ini”

It will rename “settings.ini” to “settings_new.ini”. The first parameter is the file’s path with the old name and the second one is its path and its new name.

It works with directories also; so if you want to rename a directory, just give the source path and the destination path.

The second situation needs a more complicated code:

System::Call “advapi32::GetUserName(t .r0, *i ${NSIS_MAX_STRLEN} r1) i.r2″

Rename “$EXEDIRBackupsettings.ini” “$EXEDIRBackupsettings-$0.ini”

The first line of the code gets the logged in user’s name and stores it in $0.

The second line renames “settings.ini” file to “settings.ini-USERNAME.ini”.


You’ll probably not see applications that requires this kind of renaming too often but if you come across one, you’ll know how to handle it. If not, go to NSIS forum and search for a solution.

One response to “More File Management”

  1. wilsont3ch.com » How To Make Portables

    [...] 7. More File Management [...]