0

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 ^.^

KaHinCostner
  • 167
  • 3
  • 17
  • what is the name of the image..? does it contain a space.?? if so please try without the space of the name of the file – noone May 31 '18 at 14:54
  • The image name is "LIN KED.png". I have tried without space, it works, but I would like to have a feature that can open a file with a space in its name. – KaHinCostner May 31 '18 at 14:59
  • Have you tried `system('cmd /c start "C:\Users\kevin\Documents\LIN KED.png"')`? – Tibrogargan May 31 '18 at 16:49
  • @Tibrogargan does it work for you ? Because for me it just open a window shell. This worked for me `system('cmd /c start "" "C:\Users\kevin\Documents\LIN KED.png"')` !! – KaHinCostner May 31 '18 at 17:55
  • `system('cmd /c start "" "C:\Users\kevin\Documents\LIN KED.png"')` I read here https://superuser.com/questions/511486/how-to-start-open-a-file-folder-that-contains-space-in-its-name-through-command that the first "" are for the shell window name and that the second "" are for the parameter – KaHinCostner May 31 '18 at 17:58
  • Answered your own question (and you should create an answer). Didn't test it since I have no ruby environment on this machine (hence no actual answer) - but I do know that windows commands expect their parameters to be quoted if they contain spaces. – Tibrogargan May 31 '18 at 18:20
  • yes finally, I took a lot of time to find it. Thank you Tibro ! – KaHinCostner May 31 '18 at 18:28
  • Now I have to manage the & in file names -_-' – KaHinCostner May 31 '18 at 18:28

2 Answers2

0

this should work

filePath = "c:\path\to\file name.jpg"
system %{cmd /c "start #{filePath}"}
noone
  • 6,168
  • 2
  • 42
  • 51
  • hum, is that different from the 1) in my question, because I feel that it's the same. Also, I tried applying this with : `linked_file_command = "C:\\Users\\kevin\\Documents\\LIN KED.png"` `system %{cmd /c "start #{linked_file_command}"}` but I get the same output. I had to use extra slashes to make windows read them. – KaHinCostner May 31 '18 at 15:16
0

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?

KaHinCostner
  • 167
  • 3
  • 17