[Resolved] RunProgram with CHDIR

Everything else that doesn't fall into one of the other PB categories.
User avatar
bds107
New User
New User
Posts: 9
Joined: Fri Jul 28, 2023 10:35 am
Location: Bruges (Belgium)

[Resolved] RunProgram with CHDIR

Post 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?
Last edited by bds107 on Fri Aug 08, 2025 3:22 pm, edited 1 time in total.
Axolotl
Addict
Addict
Posts: 819
Joined: Wed Dec 31, 2008 3:36 pm

Re: RunProgram with CHDIR

Post 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 
)
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
User avatar
bds107
New User
New User
Posts: 9
Joined: Fri Jul 28, 2023 10:35 am
Location: Bruges (Belgium)

Re: RunProgram with CHDIR

Post 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!!
Post Reply