Enable portability with Nircmd

Share interesting information or links related to portable apps here.
Post Reply
Message
Author
User avatar
webfork
Posts: 10821
Joined: Wed Apr 11, 2007 8:06 pm
Location: US, Texas
Contact:

Enable portability with Nircmd

#1 Post by webfork »

Background

NirCmd is a widely-used and well-tested program for basic system automation, and includes a "wait" function to start operations after another program has closed. When your program of choice shuts down, it can move settings stored in the file system to the local folder.

There are definitely more mature, more comprehensive tools available to enable portability, but Nircmd is small, easy, and actively developed. Anyone familiar with Windows batch operations should be able to make it work. This was tested on Windows 7x86.

Steps:
  1. In an existing program folder, save a copy of NirCmd. In this example, we'll be using Timeline
  2. Locate the folder where settings are saved (in the file \AppData\Roaming\.thetimelineproj.cfg)
  3. Create a text file and name it "Timeline Portable.bat" and paste in the following text:

    Code: Select all

    @echo off
    start "" copy /y "%userprofile%\AppData\Roaming\.thetimelineproj.cfg" .
    start "" timeline.exe
    start "" nircmd.exe waitprocess timeline.exe exec copy /y ".thetimelineproj.cfg" "%userprofile%\AppData\Roaming\"
  4. Launch Timeline Portable.bat

What's happening in this process
  • Before the Timeline program starts, the config file (if present) is copied from it's normal location to the local folder(start "" copy /y "%userprofile%\AppData\Roaming\.thetimelineproj.cfg" .).
  • The program is launched (start "" timeline.exe)
  • Nircmd waits in the background for the process to close before copying the config file back to it's home (start "" nircmd.exe waitprocess timeline.exe exec copy /y ".thetimelineproj.cfg" "%userprofile%\AppData\Roaming\").
---

Alternative: delete rather than move

Note that it's also possible to just delete rather than copy settings, even though the whole point of portability is not having to re-enter the configuration over and over. As some programs with only a few settings changes are incidental, this can also enable program "stealth" status where no files are left behind on a machine.


Related:

User avatar
zorro
Posts: 83
Joined: Sat Mar 19, 2016 2:32 pm
Location: Hamburg, Germany

Re: Enable portability with Nircmd

#2 Post by zorro »

Sorry to say - but for this you don't need Nircmd ;-)
In the second line you can simply add the "/wait" (start /wait) switch, it does the job as well. Most non-portable programs, even those with cluttered and complex settings/entries can be made portable with simple batch files like this - the "/wait" switch beeing the vital core.

TP109
Posts: 571
Joined: Sat Apr 08, 2006 7:12 pm
Location: Midwestern US

Re: Enable portability with Nircmd

#3 Post by TP109 »

Zorro is right, but the idea is useful. There's no need to use sophisticated tools like yaP when a single line batch file can do the job.

User avatar
webfork
Posts: 10821
Joined: Wed Apr 11, 2007 8:06 pm
Location: US, Texas
Contact:

Re: Enable portability with Nircmd

#4 Post by webfork »

zorro wrote: Sun Mar 01, 2020 4:34 am Sorry to say - but for this you don't need Nircmd ;-)
In the second line you can simply add the "/wait" (start /wait) switch, it does the job as well. Most non-portable programs, even those with cluttered and complex settings/entries can be made portable with simple batch files like this - the "/wait" switch beeing the vital core.
I'm not especially disappointed that I didn't discover something wholely new here, but could you spell out what you mean by the /wait switch?

I spent time on the Nircmd solution because it was lower memory than wrapper tools -- if there's something in the OS that will handle this without running independent software, all the better.

User avatar
SYSTEM
Posts: 2043
Joined: Sat Jul 31, 2010 1:19 am
Location: Helsinki, Finland

Re: Enable portability with Nircmd

#5 Post by SYSTEM »

webfork wrote: Sun Mar 01, 2020 4:54 pm
zorro wrote: Sun Mar 01, 2020 4:34 am Sorry to say - but for this you don't need Nircmd ;-)
In the second line you can simply add the "/wait" (start /wait) switch, it does the job as well. Most non-portable programs, even those with cluttered and complex settings/entries can be made portable with simple batch files like this - the "/wait" switch beeing the vital core.
I'm not especially disappointed that I didn't discover something wholely new here, but could you spell out what you mean by the /wait switch?

I spent time on the Nircmd solution because it was lower memory than wrapper tools -- if there's something in the OS that will handle this without running independent software, all the better.
With /wait, the start command only returns control to the batch file when the program has quit. That allows you to trivially execute additional commands afterwards.

Code: Select all

@echo off
copy /y "%userprofile%\AppData\Roaming\.thetimelineproj.cfg" .
start /wait "" timeline.exe
copy /y ".thetimelineproj.cfg" "%userprofile%\AppData\Roaming\"
My YouTube channel | Release date of my 13th playlist: August 24, 2020

User avatar
zorro
Posts: 83
Joined: Sat Mar 19, 2016 2:32 pm
Location: Hamburg, Germany

Re: Enable portability with Nircmd

#6 Post by zorro »

Yes webfork, how SYSTEM pointed out: the "start /wait" command calls the specified program and returns to the next batch line as soon as this program is terminated - just what we want if we have to copy settings over to the program folder and clean up afterwards :mrgreen:

Of course there are some special cases, where this does not work - but only because the developer of the program is special: OcenAudio for example - here the working code:

Code: Select all

start "" "ocenaudio.exe"
:WAITLOOP
tasklist /FI "IMAGENAME eq ocen_no_config.exe" 2>NUL | find /I /N "ocen_no_config.exe">NUL
if "%ERRORLEVEL%"=="0" goto RUNNING
goto NOTRUNNING
:RUNNING
goto WAITLOOP
:NOTRUNNING
REG DELETE "HKEY_CURRENT_USER\Software\QtProject" /f
rd /s /q %APPDATA%\ocenaudio
exit
Why? OcenAudio starts with the exe in the first line - but this process terminates seconds after starting, because "ocen_no_config.exe" takes over - starting the application directly by calling this exe is no solution, because then the app starts in default mode, not reading the settings.

Only a weird example where /wait doesn't work :lol:
But - I'm ashamed to say - with a simple batch I even made my Cinema 4D portable and stealth :oops:

User avatar
Midas
Posts: 6727
Joined: Mon Dec 07, 2009 7:09 am
Location: Sol3

Re: Enable portability with Nircmd

#7 Post by Midas »

Nice discussion and tips, thanks to all. :sunglasses:

Anyone objects to moving the topic to "CLI Discussions" as is befitting?

User avatar
webfork
Posts: 10821
Joined: Wed Apr 11, 2007 8:06 pm
Location: US, Texas
Contact:

Re: Enable portability with Nircmd

#8 Post by webfork »

Midas wrote: Tue Mar 03, 2020 2:34 am Anyone objects to moving the topic to "CLI Discussions" as is befitting?
Even though it uses NirCmd, I'd probably recommend keeping it in Resources just because it doesn't actually use the command-line interface.  If used as above, the user wouldn't even see a terminal window for more than a moment or two.
zorro wrote: Mon Mar 02, 2020 11:34 am "start /wait" command calls the specified program and returns to the next batch line as soon as this program is terminated
Thanks for the additional detail

User avatar
Midas
Posts: 6727
Joined: Mon Dec 07, 2009 7:09 am
Location: Sol3

Re: Enable portability with Nircmd

#9 Post by Midas »

webfork wrote: Even though it uses NirCmd, I'd probably recommend keeping it in Resources just because it doesn't actually use the command-line interface.

Both Nircmd and batch commands are CLI resources and I believe they would be more relevant positioned at "CLI discussions" -- but I have no quibble leaving everything as is.

Quick tip: use "START" on its own to launch a second CLI window on the current directory; "START." or "START ." will launch Explorer with the current folder.

User avatar
webfork
Posts: 10821
Joined: Wed Apr 11, 2007 8:06 pm
Location: US, Texas
Contact:

Re: Enable portability with Nircmd

#10 Post by webfork »

Midas wrote: Sun Mar 08, 2020 1:25 pm "START." or "START ." will launch Explorer with the current folder.
Wow, that's extremely useful.

User avatar
Midas
Posts: 6727
Joined: Mon Dec 07, 2009 7:09 am
Location: Sol3

Re: Enable portability with Nircmd

#11 Post by Midas »

Reversely, if you type 'CMD' in Explorer's address bar, it will launch a CLI window for the current folder... :idea:

Mind you, I use the convention of highlighting system commands with capital letters, but most of them are case insensitive.

User avatar
webfork
Posts: 10821
Joined: Wed Apr 11, 2007 8:06 pm
Location: US, Texas
Contact:

Re: Enable portability with Nircmd

#12 Post by webfork »

Midas wrote: Tue Mar 10, 2020 6:15 am Reversely, if you type 'CMD' in Explorer's address bar, it will launch a CLI window for the current folder... :idea:

Mind you, I use the convention of highlighting commands with capital letters, but most of them are case insensitive.
Also very useful. I have a lot to learn on the command line.

Post Reply