Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there a way to, in general, create a Windows shortcut to a file and instruct Windows to pass certain switches to the application before passing the filename?

For example: I have an Excel file, abc.xlsx. If I want to create a shortcut to open this file read-only, I would change the target to "C:\Program Files\Microsoft Office\Office12\EXCEL.EXE" /r abc.xlsx, but this fails when another person on my network opens the shortcut and their EXCEL.EXE is not in that same folder.

Surely there's a way to tell Windows to pass the /r switch without supplying the path/filename of the application that is supposed to open the file?

share|improve this question

migrated from stackoverflow.com Nov 17 '11 at 16:40

This question came from our site for professional and enthusiast programmers.

1  
If you want to pass command line switches, you must also pass the application. Because who knows, maybe *.xlsx files are handled by some non-Excel application where the /r switch means "reformat hard drive". – Raymond Chen Nov 15 '11 at 21:28
    
Dup of superuser.com/questions/29569/…;. Intermediate bat/cmd file is not necessary. – Vadzim Jun 22 '15 at 12:31

Use a batch (cmd/bat) file for this purpose. Do not create a shortcut to Excel, but rather create a shortcut to your batch file.  This file will have the necessary logic.  Look at the example image, which is VS.NET command prompt which uses same bat file with parameters.

  1. Find the correct path for Excel, if it is not in path *
  2. use /r
  3. use the correct Excel file name.

* Because the location of Excel may vary from machine to machine, it may be helpful to use start excel "c:\myfile.xls" /r within your batch file.  start excel should initiate Excel regardless of its location.

VS.NET Command Prompt Example

share|improve this answer
    
Grumble, Win 7 still doesn't have basic features that OS/2 had twenty years ago. Off to create a pointless one line batch file. – Skip Huffman Sep 28 '14 at 14:36

Your shortcut should contain the full path to both the executable and the target file, like this:

"C:\Program Files\Microsoft Office\Office12\EXCEL.EXE" /r "C:\Users\Administrator\Desktop\abc.xlsx"

Note that both EXE and XLS paths are in quotes to avoid problems with spaces in the name.

share|improve this answer

Target is the field to change. But since a shortcut points to a path two shortcuts to the same file may contain different target depending on how the network drives are mapped. So generally, you can't copy a shortcut over the network and hope it will work, since the target might no longer be valid.

share|improve this answer
    
Ok, let me phrase this differently. I want to create a shortcut to abc.xlsx that opens abc.xlsx as read-only. If I create the shortcut mentioned in my original post, it opens a new instance of Excel 2007 that isn't grouped with existing open Excel files. Thus I would like the same behaviour as when I double-click abc.xlsx in Explorer, but with Windows passing the '/r' command line switch. This has nothing to do with networking. – jaytea Nov 8 '11 at 7:40

You could define an environment variable to Office's directory and use this variable in your shortcut. Of course, this variable will have to be define on every computer of your network, but it can be done through a script and it can be use for other purpose in the future.

If you define an environment variable named Office, you can create a shortcut as so :

cmd /c ""%Office%\EXCEL.EXE" /r "abc.xlsx""
share|improve this answer

You must log in to answer this question.