2

I’m trying to call cmd commands via Julia in Windows. According to multiple examples I've seen, including the official documentation, this should be as easy placing the command in backticks and then executing it using run.

For example:

mycommand = `dir`
run( mycommand )

But when I try this I get an error:

ERROR: LoadError: IOError: could not spawn `dir`: no such file or directory (ENOENT)

To me the error indicates that Julia is looking for a file named dir and does not attempt to execute it as a command in cmd.

What am I doing wrong? How do I tell Julia that this is a command that is to be executed in the terminal and not a file?

Petahanks
  • 182
  • 11

1 Answers1

1

Use

run(`cmd /c dir`)

as cmd.exe is the executable you need to run.

Bogumił Kamiński
  • 66,844
  • 3
  • 80
  • 107
  • That produces a very similar error: ERROR: LoadError: IOError: could not spawn `cmd /c dir`: no such file or directory (ENOENT) – Petahanks Jan 19 '21 at 14:07
  • This is strange - it works for me. And what does happen if you write `cmd /c dir` after pressing `;` (switching to shell mode)? If it does not work then does running `cmd /c dir` work when you pass it as a command in the terminal? – Bogumił Kamiński Jan 19 '21 at 14:19
  • In shell mode I get the same LoadError as written in my previous comment. In a regular Julia non-shell terminal it gives the error: “ERROR: syntax: extra token "dir" after end of expression”. In both cases I’m writing it without any backticks around the expression. – Petahanks Jan 19 '21 at 15:24
  • I am asking what happens if you write `cmd /c dir` in Windows prompt (not in Julia). Also what version of Windows are you on? – Bogumił Kamiński Jan 19 '21 at 15:28
  • If I write “cmd /c dir” in the windows command prompt I get “'cmd' is not recognized as an internal or external command”. But I don’t understand why I would write that in the windows command prompt, normally I would just write “dir” and that does work. I’m using windows 64 bit windows 10. – Petahanks Jan 19 '21 at 16:21
  • 1
    I think that this question is a duplicate of this one https://stackoverflow.com/questions/32035577/cmd-is-not-recognized-as-an-internal-or-external-command-operable-program-or and this is a misconfiguration of Windows obviously not Julia related – Przemyslaw Szufel Jan 19 '21 at 16:23
  • Przemyslaw is right. I wanted you to test it in Windows prompt to check if `cmd` is in `PATH`. Apparently it is not (and it should be - as any other program you want to invoke without passing a full path to it). – Bogumił Kamiński Jan 19 '21 at 16:31
  • 1
    BTW on my Win10 machine ``run(`dir`)`` just works without any special actions – Przemyslaw Szufel Jan 19 '21 at 17:47
  • It is probably that "bare" `dir` in your system redirects to `ls`. It works also for me because of this, but if Julia was started under `cmd` then bare `dir` outputs control sequences to the terminal while `cmd /c dir` does not. – Bogumił Kamiński Jan 19 '21 at 18:05
  • I’ve added cmd.exe to my path and run(`cmd /c dir`) now works from Julia. But that is still not really what I wanted. I was looking for a way to call instruction from Julia in the same way a .bat script would run them. So if I write “D:” the terminal should change to the D drive, if I then write “cd D:\myFolder” it should change to that folder, if I then write “startMyProgram.exe” it should start a program. If I do this in a Julia terminal: “run(`cmd D: cd D:\myFolder startMyProgram.exe´)” the Julia terminal changes to a windows cmd terminal and none of the other commands are executed. – Petahanks Jan 19 '21 at 18:16
  • This is not the way how `cmd.exe` in Windows was designed AFACIT. You should have a better experience with interacting with shell under Linux. – Bogumił Kamiński Jan 19 '21 at 18:55