简体   繁体   中英

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

this should work

filePath = "c:\path\to\file name.jpg"
system %{cmd /c "start #{filePath}"}

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM