I would like to use the detox utility (on my Mac) inside OCaml, here is my failed attempt :
In my home directory, I first did mkdir mydir, cd mydir and touch accented\ filename\ àèíóù.txt. Then, inside utop :
In this particular, minimal example, I supposed I could find an adhoc fix by replacing the spaces by "\ ", but obviously I’m looking for a solution that works in all cases.
The second difficulty that I have is with retrieving the output of the detox -n command. If I redo the above example with a non-problematic filename (say easy_filename.txt) instead of accented\ filename\ àèíóù.txt :
There is no error message but the output of the command is not even printed in utop, how could I retrieve it ? Using the stdout channel or perhaps I should create an Ocaml-made executable for this.
You can use Sys.command (Filename.quote_command "detox" ["-n"; the_file]).
The easiest approach is to redirect its output to a file and then read the file. Again with Filename.quote_command you can do this by using the ~stdout (and ~stderr) optional arguments.
In addition to @nojb’s answer (quote if you use Sys.command, or use something the allows passing arguments one by one), I just wanted to mention that the problem here is the spaces in the filename, not the accents. Sys.command uses your shell, which passes “accented”, “filename”, and “àèíóù.txt” as different arguments.
And since you mentioned having tried with \, note that you need to double the \ so that it remains
in the final string. Indeed the escape sequence \ denotes a single space:
# Sys.command "ls -l foo\\ bar.txt";;
-rw-rw-r-- 1 kim kim 0 Jun 8 09:06 'foo bar.txt'
- : int = 0
But of course, the right answer is the one given by @nojb to use Filename.quote_command which is much more robust and also handles cmd.exe (Windows command lines).