1
$\begingroup$

This line below in the cmd shell unzips the content of a zip file D:\zips\test.zip in the folder C:\Users\mike\Desktop\test:

C:\Program Files\7-Zip\7z.exe e -oC:\Users\mike\Desktop\test D:\zips\test.zip

If I use Run nothing happens with the ouput error 1:

Run["C:\\Program Files\\7-Zip\\7z.exe e -oC:\\Users\\mike\\Desktop\\test D:\\zips\\test.zip"] 
$\endgroup$
  • $\begingroup$ I don't know why this happens, but it's worth trying RunProcess. Note that you need to separate the executable name and its argument (unlike with Run). $\endgroup$ – Szabolcs May 3 at 16:44
  • $\begingroup$ I suggest first doing some tests such as Run["echo testing > test.txt"] which should create a text file in your Documents folder. $\endgroup$ – Somos May 3 at 19:56
  • 1
    $\begingroup$ You have to escape quotes around the full executable path as described here. Something like Run["\"C:\\Program Files\\7-Zip\\7z.exe\" -h"] should work. $\endgroup$ – Tim Laska May 3 at 20:14
1
$\begingroup$

This is an expansion of my comment.

With its use of backslashes and spaces in directory names, the Windows OS a bit of an odd ball. If you open a CMD prompt and try to execute the 7z.exe help (-h) without quotes, then I get the following error:

C:\Users\Tim>c:\Program Files\7-Zip\7z.exe -h
'c:\Program' is not recognized as an internal or external command,
operable program or batch file.

Now, the CMD prompt will automatically add quotes if you start typing a path to an executable and these quotes will need to be represented in the Run[] function. The actual CMD prompt will look like this

C:\Users\Tim>"c:\Program Files\7-Zip\7z.exe" -h

7-Zip 18.05 (x64) : Copyright (c) 1999-2018 Igor Pavlov : 2018-04-30

Usage: 7z <command> [<switches>...] <archive_name> [<file_names>...]

<Commands>
  a : Add files to archive
  b : Benchmark
  d : Delete files from archive.... 

The following Mathematica code should execute 7z.exe help and read the output.

outputfile = CreateFile[];
Run["\"C:\\Program Files\\7-Zip\\7z.exe\" -h >>"  <> outputfile];
stext = OpenRead[outputfile];
ReadString[stext]
Close[stext];
DeleteFile[outputfile];
(* 7-Zip 18.05 (x64) : Copyright (c) 1999-2018 Igor Pavlov : 2018-04-30

Usage: 7z <command> [<switches>...] <archive_name> [<file_names>...]

<Commands>
  a : Add files to archive
  b : Benchmark *)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.