0

I'm developing a very simple Electron app for Windows which, when executed from the command prompt, opens a dialog box trough which the user can select a folder. The app would then change the command prompt directory to the directory selected by the user.

My end goal is to be able to simply type dirnav, select a folder from the dialog box and have the app take care of redirecting the command prompt to the selected directory (instead of typing cd C:\Users\myName\whateverDirectory. Here's what I have so far:

const exec     = require('child_process').exec;
const electron = require('electron');
const {app, dialog} = electron;

app.on('ready', () => {
    dialog.showOpenDialog(
        {
            title: 'Select a directory',
            defaultPath: '.',
            buttonLabel: 'Select',
            properties: ['openDirectory']
        }, (responce) => {
            exec('cd ' + responce[0], () => {
                app.quit();
            });
        }
    );
});

Unfortunately, simply doing exec('cd ' + responce[0]) doesn't seem to work, because instead of changing the directory of the command prompt the application was runned from, it changes the directory of another (unknown to me) command prompt. Is there any way to work around that?

Gark Garcia
  • 450
  • 6
  • 14
  • 2
    It is unclear what process you are trying to change the directory for. When you run `exec()` it makes a new command prompt that has it's entirely own environment. As you have discovered, changing the directory in that and then letting that shell window exit just changes the current directory in that shell prompt and does not affect anything else. – jfriend00 Jul 08 '18 at 01:39
  • You will need to tell us what real problem you're trying to solve? Perhaps you should just remember the path they select in your app and use that as the defaultPath for future operations yourself? – jfriend00 Jul 08 '18 at 01:40
  • 1
    There's no `cd.exe`, try running `where cd` and see. That's because a process can only change it's environment, and the changes will be lost when it exits. Therefore [`cd` has to be an **internal command**](https://ss64.com/nt/cd.html). [Same in *nix](https://unix.stackexchange.com/q/38808/44425) – phuclv Jul 08 '18 at 01:49
  • 1
    There is no such thing as `cd.exe`. `cd` is an internal command of `cmd.exe`, just as `dir`, `cls` and `echo` are internal to `cmd.exe`.. In addition, `cd` only affects the currently open command (terminal) window, and what your code is doing doesn't make much sense calling `cd`. – Ken White Jul 08 '18 at 01:55
  • @jfriend00 @KenWhite @phuclv Thanks a lot for explaining me this! I can now see the problem is that the `cd` command in being runned from Node's own command prompt (as explained by @jfriend in his first comment), in such a way that it doesn't affects the user's command prompt at all. Is there's any way to affect an arbitrary command prompt from outside of it's environment? – Gark Garcia Jul 08 '18 at 13:44
  • @jfriend00 On regards to your second comment. The code posted here is actually the entirety of the app's source code. I'm just trying to make a simple app so that instead of typing `cd C:\Users\myName\whateverDirectory` I can just type something like `dirnav` (which would open a dialog box for selecting a folder), select the desired folder and have the application lead the command prompt to the selected directory. – Gark Garcia Jul 08 '18 at 13:51
  • 2
    OK, it would help if you "edit" your question to explain that that is the end goal of what you're trying to do. I'm guessing that maybe you can launch your app with a batch file, have your electron app write something to stdout that can be used by the batch file to change the current directory in the shell in which the batch file is running. But, I don't know exactly how to do that. – jfriend00 Jul 08 '18 at 16:09
  • @jfriend00 Maybe I can make the application create a new temporary batch file (something as simple as `cd whatEverDirectoryWasSelected`) and then have the main batch file (the one from where the app would be launched from) call the temporary one (`call C:\somewhere\temp.bat`). – Gark Garcia Jul 08 '18 at 22:37
  • @phuclv Thanks! I didn't know about that. Just edited the question so that it makes sense: _How to correctly execute cd.exe from inside of Node.js?_ **=>** _How to correctly execute the cd command from inside of Node.js?_. – Gark Garcia Jul 09 '18 at 14:02

1 Answers1

1

Here's a simple scheme that will work from a batch file:

for /f %%i in ('node yourapp.js') do set NEWDIR=%%i
cd %NEWDIR%

And, my yourapp.js is this (just to prove that the concept works):

process.stdout.write("subdir");

This will end up executing in the batch file:

cd subdir

You should be able to plug in your electron showOpenDialog() in your own app and then just write the result to process.stdout.

The for loop in the batch file does indeed look odd, but it's the only way I found that people have found to get the stdout from an app into an environment variable that you can then use later in the batch file. You could, of course also use a temp file (redirect output to a temp file), but I thought an environment variable was a cleaner solution.

jfriend00
  • 683,504
  • 96
  • 985
  • 979