On Windows, I always prefer to use Portable Apps. I store them in a different drive on my computer. That way, if I have to reinstall my OS, I still have all my apps available.

There’s one catch though. If I want to use the apps directly from the command line or access them programmatically, I have to give the full path to the executable. The only way around it is to update the PATH` environment variable with the directory where the portable app exists.

This gets a little painful and when restoring the system, it takes a whole bunch of time adding these to the PATH environment variable again.

It would be ideal if I had a single folder, which is added to the PATH, and I have some sorts of links or shortcuts there to launch the apps.

However, many apps require that they be run from the directory where they exist, rather than by using a shortcut or link.

To overcome this issue, we need to do things a bit differently.

I create a directory C:\bin where I keep all the files which will launch these applications. This is the ONLY directory that gets added to the PATH environment variable.

Inside that folder, I have to create a file with the same name as the executable program I want to run. Except that, this file extension ends with .cmd. So, for example, if I want to use ripgrep from the terminal, I should create a file called rg.cmd

Inside this file, the contents should be

@echo off
pushd "D:\MyApps\ripgrep\"
"rg.exe" %*
popd

What this script essentially does is navigates to the directory where ripgrep exists, runs rg.exe with all the arguments given, and after that gets back to the current directory where the command was called from.

Doing this repeatedly became a challenge, so here’s a small script called makecmd.cmd which does this.

:: makecmd.cmd - this file should be in C:\bin and C:\bin should be in the PATH
@echo off
setlocal

set FORCE=0

:: Check for -f flag
if "%~1"=="-f" (
  set FORCE=1
  shift
)

:: Validate args
if "%~1"=="" (
  echo Usage: makecmd [-f] name full_path_to_exe
  exit /b 1
)

if "%~2"=="" (
  echo Usage: makecmd [-f] name full_path_to_exe
  exit /b 1
)

set NAME=%~1
set TARGET=%~2
set OUT=C:\bin\%NAME%.cmd

:: Check if file exists
if exist "%OUT%" (
  if "%FORCE%"=="1" (
    echo Overwriting existing %OUT%...
  ) else (
    echo Error: %OUT% already exists. Use -f to overwrite.
    exit /b 1
  )
)

echo Creating %OUT%...

(
  echo @echo off
  echo pushd "%~dp2"
  echo "%~nx2" %%*
  echo popd
) > "%OUT%"

echo Done.

So, now if I want to have ripgrep in my path, I have to type this in the command prompt.

makecmd rg D:\MyApps\ripgrep\rg.exe

If the file already exists, the command exits unless the -f flag is added for forcing an overwrite.

What this script does:

  1. Checks for the -f flag first.
  2. If no arguments are given it gives the usage message and quits.
  3. If two arguments are not there it gives the usage message and quits.
  4. Checks if the file already exists in C:\bin. If it does it gives the message that the file already exists and to use the -f flag to overwrite.
  5. Creates the corresponding .cmd file
"%~dp2"

gives the directory path to the executable file

"%~nx2"

gets the executable file name

This has been a huge timesaver for me.