Windows 7 How to change "My Documents" env var temporarily for a certain GUI program?

pstein

Extraordinary Member
Joined
Mar 20, 2010
Messages
454
Ok, I know, I can change/set environment variables through
Control Panel--->System

The current environment variable values are fine for most of my programs.

However there is one program which puts some config files into the folder which is currently defined as user variable "My Documents".
I don't want that. I want to define my own, different folder for these files.
And I don't want to change always the general environment variables in "System" dialog.

Unfortunately there is no setting in the program in question to define a different location.

So is there another smart way of temporarily, externally changing the environment variable for ONE particular GU based program?

I could imagine that a batch script might be suitable with the following commands:

set "My document"=D:\path\to\different\location\
start myprogram123.exe


This trick above does not work.
But is there a similar way?

Maybe there are Registry hacks which allow individual enviroment variables for ONE particular GUI programs.

Thank you
Peter
 
Solution
To change the location where a specific program stores its files without altering system-wide environment variables, you can indeed utilize a batch script or PowerShell script to set environment variables temporarily for that program. Here’s how you can achieve this:

Method 1: Using a Batch Script​

You were on the right track with your initial idea. You can create a batch script to set the environment variable just for the duration of running your program:
Code:
@echo off setlocal set "MyDocuments=D:\path\to\different\location\" start myprogram123.exe endlocal
In this script:
  • setlocal and endlocal ensure that the environment variable changes are local to the script.
  • set...
To change the location where a specific program stores its files without altering system-wide environment variables, you can indeed utilize a batch script or PowerShell script to set environment variables temporarily for that program. Here’s how you can achieve this:

Method 1: Using a Batch Script​

You were on the right track with your initial idea. You can create a batch script to set the environment variable just for the duration of running your program:
Code:
@echo off setlocal set "MyDocuments=D:\path\to\different\location\" start myprogram123.exe endlocal
In this script:
  • setlocal and endlocal ensure that the environment variable changes are local to the script.
  • set "MyDocuments=D:\path\to\different\location\" sets the MyDocuments variable to your desired folder.
  • start myprogram123.exe launches your program with the modified environment.

Method 2: Using PowerShell​

You can also achieve this using PowerShell for more flexibility and functionality:
Code:
$env:MyDocuments = "D:\path\to\different\location\" Start-Process -FilePath "myprogram123.exe"
In this PowerShell script:
  • $env:MyDocuments = "D:\path\to\different\location\" sets the MyDocuments environment variable.
  • Start-Process -FilePath "myprogram123.exe" launches your program.

Note:​

  • Make sure to replace "myprogram123.exe" with the actual executable name of your program.
  • Ensure that the paths provided in the script are correct.
  • Remember that these changes are temporary and will not affect other programs on your system.
  • Always test scripts in a safe environment before applying them to production systems.
By using these scripts, you can effectively set specific environment variables for individual GUI-based programs without altering the system-wide environment settings.
 
Solution