Search code examples
javarubyjruby

open file that has a space (or &) in its name, with default app


I'm trying to open a file, which path is C:\Users\kevin\Documents\LIN KED.png (or C:\Users\kevin\Documents\LIN&KED.png). The file path would be in linked_file_command.

I want to open the file in its default application on windows. I would like to keep using the linked_file_command string, just to have a variable instead of a hardcoded path.

I have tried the followings :

1) system %{cmd /c "start #{linked_file_command}"}
(from https://stackoverflow.com/questions/9476291/how-to-open-file-in-default-application-ruby )

2) system('cmd /c start "" C:\Users\kevin\Documents\LIN KED.png')
(from https://www.ruby-forum.com/topic/209961 )

Result for both :

Le système ne peut trouver le fichier C:\Users\kevin\Documents\LIN. (The system cannot find the file C:\Users\kevin\Documents\LIN)

Thanks in advance ^.^


Solution

  • MANAGE SPACE

    system('cmd /c start "" "C:\Users\kevin\Documents\LIN KED.png"')

    I read here superuser.com/questions/511486/…

    The first "" are for the shell window name and the second "" are for the command (parameter)

    MANAGE &

    Wasn't necessary when I used the line above.

    In other cases, try adding a '^' before the & symbols

    system('cmd /c start "" "C:\Users\kevin\Documents\LIN^&KED.png"')

    from : How do I escape ampersands in batch files?