[erlang-questions] how to open_port with spaces in path?

Eric P. Melbardis <>
Mon Feb 12 19:59:29 CET 2007


Need to use 'unix style' or erlang internal format.

 
filename:split("c:/a b/cc").
["c:/","a b","cc"]

I just pass the 'unix' style name to altname and use the result as the
command when doing a spawn. Spaces are no longer an issue.

Regards



-----Original Message-----
From: Serge Aleynikov [mailto:] 
Sent: Monday, February 12, 2007 10:50 AM
To: Eric P. Melbardis
Cc: Denis Bilenko; 
Subject: Re: [erlang-questions] how to open_port with spaces in path?

Actually the code below won't work, because filename:split/1 is bound to

the same problem of dealing with spaces as the one mentioned in the 
beginning of the thread:

1> filename:split("c:\temp\a a\my.exe").
["c:","\tempa amy.exe"]

The main point about having file:altname/1 is quite useful though.  Yet,

it is somewhat counter intuitive on Windows (I would think that the 
results of the two cases below would be reversed):

2> file:altname("c:\temp\a a").
{error,eio}
3> file:altname("c:/temp/a a").
{ok,"AA0919~1"}

Serge

Eric P. Melbardis wrote:
> Sorry, you are correct
> Here is the code I use for 8.3 names:
> 
>
%%----------------------------------------------------------------------
> -
> %% @spec altname(Win32Path) -> string()
> %%       Win32Path = path()
> %% @doc Convert file name to MS-DOS 8.3 format.
> %%
> %% Function uses an internal Erlang file primitive, not in the kernal
> %% manual. Lifted from the test_server and used elsewhere inside
Erlang,
> 
> %% so it should be safe for a while atleast!
> %%
> 
> altname(Path) ->
>     filename:join(altname(filename:split(Path),[])).
> 
> altname([],_) ->
>     [];
> altname([PC | T],BaseList) ->
>     FullPath = filename:nativename(filename:join(BaseList ++ [PC])),
>     NewPC = case catch file:altname(FullPath) of
> 		{ok,X} ->
> 		    X;
> 		_ ->
> 		    PC
> 	    end,
>     NewBase = BaseList ++ [NewPC],
>     NextDir = filename:nativename(filename:join(NewBase)),
>     [NewPC | altname(T,NewBase)].
>   
> ===============================================================
> 
> I forgot how traumatic it was!
>   
> 
> 
> -----Original Message-----
> From: Serge Aleynikov [mailto:] 
> Sent: Monday, February 12, 2007 9:58 AM
> To: Eric P. Melbardis
> Cc: Denis Bilenko; 
> Subject: Re: [erlang-questions] how to open_port with spaces in path?
> 
> You made me curious to look at the sources to see if this happens 
> indeed, but it seems that filename:nativename/1 only does path 
> normalization and doesn't do name mangling:
> 
> filename.erl:545
> ================
> 
> nativename(Name0) ->
>      Name = join([Name0]),			%Normalize.
>      case os:type() of
> 	{win32, _} -> win32_nativename(Name);
> 	_          -> Name
>      end.
> 
> win32_nativename([$/|Rest]) ->
>      [$\\|win32_nativename(Rest)];
> win32_nativename([C|Rest]) ->
>      [C|win32_nativename(Rest)];
> win32_nativename([]) ->
>      [].
> 
> Serge
> 
> Eric P. Melbardis wrote:
>> With r10 & windows platform, you can use
>>
>> filename:nativename(FullPathName)
>>
>> works the best, since the file name mangling is dynamic! (ie relative
> to
>> other similar file names!)
>>
>> regards
>>
>>
>> -----Original Message-----
>> From: 
>> [mailto:] On Behalf Of Serge
>> Aleynikov
>> Sent: Monday, February 12, 2007 5:44 AM
>> To: Denis Bilenko
>> Cc: 
>> Subject: Re: [erlang-questions] how to open_port with spaces in path?
>>
>> Since this seems to be a known bug, you may try some workarounds.
The
> 
>> most obvious one is to rename a directory so that it doesn't contain 
>> spaces.  The less obvious one is to find out what is the "short name"
> of
>> a directory, and use that instead:
>>
>> c:\temp>dir /X /AD "a*"
>>
>>   Volume in drive C is unlabeled      Serial number is 381F:4FF1
>>   Directory of  C:\temp\a*
>>
>>   2/12/07   8:31         <DIR>    AA0919~1        a a
>>                0 bytes in 0 files and 1 dir
>>    3,814,490,112 bytes free
>>
>> In the example above "AA0919~1" is the equivalent name of the "a a" 
>> directory:
>>
>> c:\temp>dir "AA0919~1"
>>
>>   Volume in drive C is unlabeled      Serial number is 381F:4FF1
>>   Directory of  C:\temp\AA0919~1\*
>>
>>   2/12/07   8:31         <DIR>    .
>>   2/12/07   8:31         <DIR>    ..
>>   2/12/07   8:31               0  my.exe
>>                0 bytes in 1 file and 2 dirs    0 bytes allocated
>>    3,814,490,112 bytes free
>>
>> Serge
>>
>> P.S. This second workaround doesn't seem very appealing, but if you 
>> can't possibly change directory names, this may be your only option.
>>
>> Denis Bilenko wrote:
>>> Hello,
>>> I'm trying to execute external program which happened to have spaces
>> in
>>> path (not uncommon on windows). open_port exits with einval:
>>>
>>> 56> open_port({spawn, "D:/a a/my.exe"}, []).
>>>
>>> =ERROR REPORT==== 11-Feb-2007::23:59:35 ===
>>> Error in process <0.102.0> with exit value:
>>> {einval,[{erlang,open_port,[{spawn,"D:/a a/my.exe"},[]]},{erl_eval
>>> ,do_apply,5},{shell,exprs,6},{shell,eval_loop,3}]}
>>>
>>> ** exited: {einval,[{erlang,open_port,[{spawn,"D:/a a/my.exe"},[]]},
>>>                     {erl_eval,do_apply,5},
>>>                     {shell,exprs,6},
>>>                     {shell,eval_loop,3}]} **
>>>
>>> I've tried to use quotes
>>>
>>> 57> open_port({spawn, "\"D:/a a/my.exe\""}, []).
>>>
>>> it doesn't change anything.
>>>
>>> Googling revealed this:
>>> Known problems
>>>   * os:cmd on WIN32 does not always catch the output from the
> executed
>> program
>>>     correctly. There is also a problem with executing programs with
>>> space in the path.
>>> http://www.erlang.org/doc/doc-4.8.2/erts-4.8.2/notes_history.html
>>>
>>> It was not fixed? Are there any workarounds?
>>>
>>> erl: 5.5.3
>>> os: winxp
>>>
>>> /Denis.
>>> _______________________________________________
>>> erlang-questions mailing list
>>> 
>>> http://www.erlang.org/mailman/listinfo/erlang-questions
>>>
>> _______________________________________________
>> erlang-questions mailing list
>> 
>> http://www.erlang.org/mailman/listinfo/erlang-questions
>>
> 

-- 
Serge Aleynikov
Routing R&D, IDT Telecom
Tel: +1 (973) 438-3436
Fax: +1 (973) 438-1464




More information about the erlang-questions mailing list