Open Word document from command line with additional data source parameter - windows

It is possible to open a word document from the command line using this:
rundll32 url.dll,FileProtocolHandler path.to.word.document.doc
Unfortunately, that document needs an external data source, so the path of that data source has to be set manually once it is opened.
Is there a more convenient way to provide an additional parameter to point to the data source?

You can open a word document using
winword.exe <filepath>
To use winword in command prompt you need to set your path variable like this
set path=%path%;C:\Program Files\Microsoft Office\Office14

In the cmd instance I opened the document but just typing in the document name in
"whatever it's called.docx"

If you use PowerShell, you can type "ii FileName". For example, "ii *.docx" will open all the .docx files in the folder. It works for all types of files, not just Word documents.

Related

How to open a file in a specified program using window comand prompt

How can I open a file from the command prompt in a specified program rather than the default program for opening the file.
like in MAC terminal
open main.js -a "Sublime Text"
currently I only do
start filename.extension
which opens the file in the default program.
please what command can I use to achieve this?
With Windows, you type application first.
So with Notepad, which is on the Windows path, you can type
notepad filename.extension
By 'Windows Path' I mean a list of directories that Windows looks in for your application. If your app is in one of those folders, then you only have to type the application name. If your app isn't, then you need the full path to the application.
Most of the Windows native apps (like Notepad, MSPaint, etc) are automatically on the path. However apps that are installed afterwards sometimes don't update the path and you need the full path. You can usually get this by right-clicking on the application and getting properties. Often you'll need quote marks - specifically if there are spaces in the path, which there usually are because "Program Files", so:
"C:\Program Files (x86)\Notepad++\notepad++.exe" filename.extension
Note the quote marks go around the path to the application file itself - not all the way to the end of the line. An easy way to check that you've got the full path to the file is with the dir command:
dir "C:\Program Files (x86)\Notepad++\notepad++.exe"
Some applications expect instructions about what to do with the file, and you may need to figure out what else to put on the command line. Usually google will tell you this.
For example, to execute an SQL script, with one tool I use, just putting the filename on the command line won't work, you do something like:
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -U user -d dbName -f filename.extension

How to run an executable that contains a space in path from command line on Windows 10?

H:\>"H:\Program Files\R\R-3.4.0beta\bin\R.exe"
'H:\Program' is not recognized as an internal or external command,
operable program or batch file.
H:\>"H:\Progra~1\R\R-3.4.0beta\bin\R.exe"
The system cannot find the path specified.
H:\>H:\Progra~1\R\R-3.4.0beta\bin\R.exe
The system cannot find the path specified.
I tried using "..." and Progra~1 and both are not working on Windows 10.
What I'm doing wrong?
Short answer: Use & 'C:\path with spaces\app.exe'
Explanation: Just type your path into powershell and use TAB for auto completion when you choose any directory containing spaces. Powershell will automatically insert single quotes 'bla bla' and it will also put an & in front which is needed to treat the string as something that should be executed. Continue completing your path like usual.
The way to do this - and I can't believe I'm just now figuring this out - is to use Windows short names generated for files with non-8dot3 names. To get the path or program name in question, type dir /x <path to program>. It will spit out something like PROGRA~1 for Program Files folder. Of course you have to do that directory by directory, and if you have multiple files/folders with spaces in the name, it's cumbersome. If you want the full path formatted with short names, you can do:
for %I in (*) do echo %~sI
For example, if the file I want to access is C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\170\License Terms\License_msodbcsql_ENU.txt, I could type:
for %I in ("C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\170\License Terms\License_msodbcsql_ENU.txt") do echo %~sI
And what I get back is the much easier C:\PROGRA~1\MICROS~2\CLIENT~1\ODBC\170\LICENS~1\LICENS~1.TXT.
Annoying that you can't query the whole directory path without using a loop, but it is what it is. Good for aliases.
There may be an easier way with powershell, but I'm pretty sure there isn't from the cmd prompt.
you must be doing something wrong as the double quotes encapsulates the path to the executable including spaces.
To ensure you are doing it correctly, start typing the path to the command and use TAB after F:\Program until you see the correct path, in your case it will automatically do this
"F:\Program Files"
Use your arrow key to go back behind the end quotation and continue the path and use tab until you have reached.
"H:\Program Files\R\R-3.4.0beta\bin\R.exe"
You can also try and issue it with Start
start "H:\Program Files\R\R-3.4.0beta\bin\R.exe"
If Your cmd windows is open on the actual System drive where "Program Files" are located, you can run this instead of adding the drive letter as well:
".\Program Files\R\R-3.4.0beta\bin\R.exe"
Or add it to your environment variables with the path, then it should execute with just:
R.exe

How to suppress 'Open with' window

When running a file which has an unknown extension (lets say test.nope) from the command line or a batch file - using test.nope, call test.nope or start test.nope - you are presented with a window asking you to "Choose the program you want to use to open this file" (in Windows 7, presumably in most Windows OS's).
Is it possible to suppress this window?
My initial thought was to check if the extension exists in the %PATHEXT% variable before attempting to open the file. However, this does not contain all known file extensions. For example, though the .py extension is not in my %PATHEXT% variable, Python scripts are still opened correctly.
An alternative to a direct registry query (as suggested by #Mitch) is to use the command line utility assoc
assoc .nope
If there is no application registered for the file extension it produces
C:\>assoc .nope
File association not found for extension .nope
If an association is found (for instance, for the .docx extension), it produces
C:\>assoc .docx
.docx=Word.Document.12
You might also find ftype useful. It returns the command line for the file type returned by assoc (I have Office installed in a non-default location, as you can see):
C:\>ftype Word.Document.12
Word.Document.12="D:\Microsoft Office\Office12\WINWORD.EXE" /n /dde
File types are registered in HKCR\ (full documentation available from MSDN). You can find out if a type is registered by checking for the existence of the key. In a batch file, you could use the reg command to do so.
reg query HKCR\.txt || echo This will never print
reg query HKCR\.foobartxt || echo Could not find foobartxt
That being said, file types can be defined and named without having a default handler. Further, those which have default handlers may not have command lines - the file may be launched via DDE or COM.

Microsoft Word Command line Utility

I have a MS Word document whose path is set in the Environment variable.
How to open that document from Command line Utility?
"C:\Program Files\Microsoft Office\Office\Winword.exe" E:\hello.docx
I have the above command to open a document. But in the above command I have hardcoded the document's name and path.
All I want to do is to use an Environment variable to supply the document's name and path.
If you're open to using Powershell instead of the command prompt, you can complete with just a couple of setup steps.
First, create the environment variable for your word document. If you've already done this elsewhere, skip this step. Note that strings that are enclosed by double quotations will resolve variables, so in this example $env:username will resolve to your current Windows user. You could hard-code that too if you like, but this is helpful to generalize the example.
$env:WordDoc = "C:\Users\$env:username\Documents\myDocument.docx"
Next, you will need to add Office's directory to your path variable. You can search for winword.exe to find the location, but it will probably be one of the two below:
C:\Program Files\Microsoft Office\Office14
C:\Program Files (x86)\Microsoft Office\Office14
You can simply append that path to the environment variable, like so:
$env:Path += ";C:\Program Files\Microsoft Office\Office14"
Anyway, once that is set you can use winword from powershell to open word documents. Here's a simple example:
winword $env:WordDoc
A quick note about changing environment variables in this manner -- they are on the process level. That means that these changes will go away when you close your powershell session. Instead of typing them out each new session, you could save them to a powershell script and run that in the console. Here's a quick script that works on my machine:
param
(
[string]$FilePath
[string]$wordDir = "C:\Program Files (x86)\Microsoft Office\Office14"
)
$env:WordDoc = $FilePath
If(!($env:Path | Select-String -SimpleMatch $wordDir))
{
$env:Path += ";$wordDir"
}
winword $env:WordDoc
Doing this in the command prompt would involve a similar procedure -- you still need to set your PATH environment variable to recognize Microsoft Office. This answer offers some insight on how to do that.
If you mean from a batch file, and MS Word is properly associated with the .doc and .docx file extensions on your system, it's very simple.
Put the following in a batch file (for instance, C:\Temp\StartHello.bat). I've used DocVar to be the path and filename of the document; replace it with whatever your environmental variable is named.
set DocVar="E:\Hello.docx"
%DocVar%
Run it
C:\Temp>StartHello
If the environmental variable is already set, just remove the first line from the batch file that assigns it. This leaves you with a single line:
%DocVar%
If you mean "directly from the command line", you can just skip the batch file part:
C:\Temp>%DocVar%

How to open a file from the command line with a specified program?

I would like to open a PDF in Photoshop from the command line. My current issue right now is that the default application for opening PDFs is Adobe Acrobat. I'm wondering if there is any parameter I can pass to specify which program to use when opening a file.
In other words, I want to emulate the option of "Open-with" when you right-click a file to open it with the non-default application, but from the command line.
I do not want to change the default application for PDFs to be Photoshop.
Any ideas?
All you need to is provide the filename as a command line argument:
photoshop <path to file>
(<path to file> needs to be quoted if it contains spaces)
For example:
photoshop "C:\Users\csterling\Documents\some document.pdf"
If the directory containing photoshop.exe isn't in your Path environment variable, you'll need to provide the full path:
"C:\Program Files\Adobe\Photoshop\photoshop" "C:\Users\csterling\Documents\some document.pdf"
This isn't a feature of the command prompt, it's a feature of the executable, i.e. photoshop.exe has to be programmed to accept a file to open as a command line argument. Fortunately, it is, as are the majority of Windows applications that operate on files.
In case you want this to work with relative path in PowerShell, here is the script:
function photo
{
$the_filename=resolve-path $args[0]
photoshop $the_filename
}
Then you can just type:
cd C:\Users\csterling\Documents
photo mypic.jpg
You can do it by using the start command:
start <program-name> <file-path>
In your case, you would have to do something like this:
start photoshop D:\open.pdf
Unfortunately, the current version of Photoshop doesn't support this operation out of the box. You can open the program: start "path_to_photoshop.exe", but there is no way to pass it a file to open. If you really want to do it, you will need to get something like this: https://www.eulanda.eu/en/access-photoshop-api-via-powershell-script. Sorry, I wish I had a better answer, especially since I wanted to be able to do this for a program I was working on.

Resources