Skip to main content
Added a workflow to prep dos commands for RunProcess
Source Link
Tim Laska
  • 16.4k
  • 1
  • 35
  • 58

Update

I developed the following workflow in an attempt make the process a little more robust.

First, I created a directory and test file with no spaces or special character. Then I opened a command shell to confirm that the command is working as expected with simple characters. Once it is working as expected, I saved the command history as shown in the screen cap below.

Dos Command Session

Next, I imported the history as a table in Mathematica

SetDirectory[NotebookDirectory[]];
histtable = Import["hist.txt", "Table"]
(* {{"cd", "Dropbox\\WolframCommunity"}, {"dir", "/b", 
  "e:DocTest\\testdir"}, {"doskey", "/History", ">", "hist.txt"}} *)

I see that the second to the last element of the list contains the DOS command that I want to process. So, I created a function to replace my command list with simple names to arbitrary names.

cmdfunction[dir_] := {"cmd", "/c"}~
  Join~(StringReplace["testdir" -> dir] /@ histtable[[-2]])

Now, we can test if RunProcess works on our troublesome folder.

RunProcess@cmdfunction["тест 2"]
(* <|"ExitCode" -> 0, "StandardOutput" -> "test file 2.txt
  
  ", "StandardError" -> ""|> *)

It appears to work. Perhaps, this workflow could be adapted to prepare other DOS commands for RunProcess to take out some of the guesswork.

Original

I broke the command up and got it to work on my system (note that I did it in E:\DocTest).

RunProcess[{"cmd", "/c", "dir", "/b", 
  "E:\\DocTest\\тест 2\\"}]
(* <|"ExitCode" -> 0, "StandardOutput" -> "test file 2.txt
  
  ", "StandardError" -> ""|> *)

I broke the command up and got it to work on my system (note that I did it in E:\DocTest).

RunProcess[{"cmd", "/c", "dir", "/b", 
  "E:\\DocTest\\тест 2\\"}]
(* <|"ExitCode" -> 0, "StandardOutput" -> "test file 2.txt
  
  ", "StandardError" -> ""|> *)

Update

I developed the following workflow in an attempt make the process a little more robust.

First, I created a directory and test file with no spaces or special character. Then I opened a command shell to confirm that the command is working as expected with simple characters. Once it is working as expected, I saved the command history as shown in the screen cap below.

Dos Command Session

Next, I imported the history as a table in Mathematica

SetDirectory[NotebookDirectory[]];
histtable = Import["hist.txt", "Table"]
(* {{"cd", "Dropbox\\WolframCommunity"}, {"dir", "/b", 
  "e:DocTest\\testdir"}, {"doskey", "/History", ">", "hist.txt"}} *)

I see that the second to the last element of the list contains the DOS command that I want to process. So, I created a function to replace my command list with simple names to arbitrary names.

cmdfunction[dir_] := {"cmd", "/c"}~
  Join~(StringReplace["testdir" -> dir] /@ histtable[[-2]])

Now, we can test if RunProcess works on our troublesome folder.

RunProcess@cmdfunction["тест 2"]
(* <|"ExitCode" -> 0, "StandardOutput" -> "test file 2.txt
  
  ", "StandardError" -> ""|> *)

It appears to work. Perhaps, this workflow could be adapted to prepare other DOS commands for RunProcess to take out some of the guesswork.

Original

I broke the command up and got it to work on my system (note that I did it in E:\DocTest).

RunProcess[{"cmd", "/c", "dir", "/b", 
  "E:\\DocTest\\тест 2\\"}]
(* <|"ExitCode" -> 0, "StandardOutput" -> "test file 2.txt
  
  ", "StandardError" -> ""|> *)
Source Link
Tim Laska
  • 16.4k
  • 1
  • 35
  • 58

I broke the command up and got it to work on my system (note that I did it in E:\DocTest).

RunProcess[{"cmd", "/c", "dir", "/b", 
  "E:\\DocTest\\тест 2\\"}]
(* <|"ExitCode" -> 0, "StandardOutput" -> "test file 2.txt
  
  ", "StandardError" -> ""|> *)