0

I'm writing myself a GUI utility for use in a CMD window to navigate between folders, rather in the style of the old Norton Change Directory utility for DOS.

When run, the app pops up a folder tree to allow the user to select a folder to which to navigate and then closes and returns to the CMD prompt. At the moment, the way it works is that it is run as the first command in a "main" batch file. It writes a secondary batch file, in my app's folder under AppData, containing the commands to change drive and directory to the folder the user selected, and the main batch file then invokes this second batch file using CALL.

It works fine, but this way of actually changing the CMD window's current directory strikes me as inelegant both from the point of view of needing to be run from a batch file (so that the user's selection can be acted upon after my app has closed) and of needing the secondary batch file to do the actual navigation.

So, my question is, how can my app send the instructions to the instance of CMD that owns the window in which the app is run to the folder the user selected? I've tried doing a ShellExecute of "CMD /K ..." but although that does indeed navigate to the selected folder, it does so in a new CMD window, not the one my app is run in. The conceptual gap I have is how to get the current CMD to act on my app's instructions after my app has terminated.

Fwiw, I thought of trying to write the user's folder selection into an environment variable in the CMD window's environment for the CMD processor to act upon from there, but this seems to require that the CMD window be opened via "Run as Administrator", which I definitely don't want.

12
  • I can't get what you mean with "how to get the current CMD to act on my app's instructions after my app has terminated". How should it act? Commented Nov 12, 2015 at 15:45
  • why not simply output to the cmd WriteLn(Format('cd "%s"', [the_selected_directory])) when your app closes? ` Commented Nov 12, 2015 at 15:53
  • Well, if I try to change the current directory from within my app, it seems to have no effect on the current directory of the CMD window after my app has terminated -it's as if the "current directory" in the CMD window reverts to whatever it was before my app ran. So it seems to me that I need to get the CMD instance which is "hosting" the window to change directory Commented Nov 12, 2015 at 15:53
  • "why not simply output to the cmd" because that has no effect whatever in or on the CMD window the app is run in. Commented Nov 12, 2015 at 15:57
  • @MartynA Is you program opening the console window or is it started from within the console window by specific command? Commented Nov 12, 2015 at 16:08

1 Answer 1

2

Your program cannot influence the environment variables of the command interpreter because they're separate processes. Your program cannot change the directory of the command interpreter directly, either, because, again, they're separate processes.

You need to use a batch file because the command interpreter executes batch files internally. Since it's all the same process, the batch file has the power to change the current directory, and for that change to remain in effect after the batch file finishes running.

Therefore, you need some way for your interactive program to communicate the directory selection back to the batch file so that it can act on it.

Instead of writing the instructions to another batch file, you could write the result to standard output. Have the batch file capture that output into a variable, and then execute cd on that variable. The batch code would look something like this:

for /f "tokens=*" %%a in ('[select_dir.exe]') do (
  set DIRSELECTION=%%a
)
cd /d %DIRSELECTION%

Your Delphi code would look like this:

writeln(selected_dir);

To allow that command to work, you'll need to make sure your program is marked as a console program, as with {$APPTYPE CONSOLE}. If it's not, then the batch file won't receive any output, and probably won't even wait for your program to finish running before proceeding. It's OK for a console program to display a TForm, just like a GUI program.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for that, I'll give it a whirl. I must admit, I've never had occasion to use "for /f tokens ..." except maybe messing around with the examples in Neil Rubenking's marvellous "DOS Batch File Lab Notes" book.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.