Page 1 of 1

[Resolved] RunProgram with CHDIR

Posted: Fri Aug 08, 2025 10:59 am
by bds107
Hello everyone,

I'd like to exit my Windows console program with a CHDIR /D <path>.

Code: Select all

RunProgram("cmd.exe", "/K CHDIR /D " + DirLoc, "")
doesn't seem to work. In fact, CMD.EXE runs on top of the existing CMD.EXE.
So, if you execute an EXIT, you'll be taken to the previous CMD and its location.
SetCurrentDirectory(DirLoc) doesn't work either.

Following A.I.'s advice, I tried creating and running a batch file, but nothing worked. What's more, Google Gemini told me it wasn't possible.

Any other suggestions?

Re: RunProgram with CHDIR

Posted: Fri Aug 08, 2025 2:38 pm
by Axolotl
I don't think you can do that.
An executable - which by definition runs in a child process - cannot change its parent process' working directory.

Maybe this work around from the old days will help. (not tested)
+ Create a batch file inside your app.

Code: Select all

@REM Filename: %temp%\mynewworkingdir.bat
@cd /D <my new working dir> 
+ Call this bat after your app terminates inside the console
maybe you should use a second batch file to automate this..

Code: Select all

 @echo off 
rem myAppCaller.bat
<AppName.exe> %* 
if exist %temp%\mynewworkingdir.bat (
  %temp%\mynewworkingdir.bat
  del %temp%\mynewworkingdir.bat 
)

Re: RunProgram with CHDIR

Posted: Fri Aug 08, 2025 3:21 pm
by bds107
Hey,

I'm glad you mentioned the batch file method.
I asked ChatGPT (or was it Copilot or GROK) if it was possible to change the CurrentDirectory with a Windows API. For security reasons, this isn't possible.
It is indeed changed, even with RunProgram or SetCurrentdir(), or an API, but once CMD takes over, you return to the original prompt. Damn....
I successfully resolved the issue using those BATCH files.
I'll mark this thread as resolved. Thanks for your help anyway!!