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

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.

Related

Opening an image using 'system' or 'exec'

My goal is to open image files in a default image viewer (Windows 10 Photos app), and close them per user input. My file path contains backslashes, not standard slashes, although replacing them doesn't seem to change the results I mention below.
I tried the following:
Kernel.system('full_path_to_image')
or the same thing using exec instead, but it simply returns a format error Errno::ENOEXEC. Manually entering the file path in the command interpreter works even if the interpreter is opened via:
Kernel.system('cmd')
I tried to avoid the shell by using a multi-argument version of system, but I could not.
Is it possible to do what I want to?
According to this answer, this should work on windows.
system("start #{path_to_image}")

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

"edit" not a valid command in cmd.exe?

I'm trying to create a .cfg file for bcc32 compiler and I'm following the instructions. I have installed correctly and placed an environment path as instructed but when I type "edit bcc32.cfg" into the command prompt it says that edit isn't a valid command? What am I supposed to do?
You could also create a .bat file, edit.bat, to replace the 16-bit edit program (removed because x64 windows flavors won't run it) which would launch your favorite editor.
#echo off
notepad %1
#echo on
This is what I wound up doing as a simple patch so I could carry on the way I always had for the most part. Just type:
edit myfile.ext
in the command prompt to use it.
Note: notepad is not my favorite editor - this is just an example that will work with stock windows.
Note 2: #echo off and #echo on are shown for clarity. You may also shorten this by omitting the echo statements and simply placing the # before the command to be silenced.
#notepad %1
I just use notepad (since they took out the edit command) from the command window like so:
C:\Borland\BCC55\bin> notepad bcc32.cfg
The file will open in notepad for editing. When you've finished editing the file, save it and you're done.
I have found this works for seeing in-window text of a complete file, on a 64bit machine. Once your path is set in cmd prompt, type the word type... followed by "filename" do you see how I used the quotes around the filename only!
type "filename"
You type it just like this (changing filename for your files name) and you will be able to see the entire file text in the cmd window. Not sure how to edit from here on but maybe someone can figure it out from here and tell me.
Assuming you're using Windows 7 (where edit.exe and edlin.exe have been removed):
Use powershell.exe instead of cmd - thereby edit will be available via command line.
Take a look at: http://en.wikipedia.org/wiki/Windows_PowerShell
simple answer....
if your using an old version of windows (xp e.t.c...) you would be able to use edit
but since your using new version of windows, Microsoft has updated and removed the commands that they think are not relevant e.g.. (msg, edit) depending if its a bit32 bit64 or bit82...

how do you make a shortcut that can select a file and run a DOS command with it as a parameter?

I need this to be able to ask the user for a file, then after the user chooses one, it will run a CMD command like "program.exe [selected file]"
If you're asking for a way to have a batch script create an open file dialog, I don't believe that's possible. You're better off creating a small program that will do this for you.
There might be a way, but I don't know it. I'm going to cover some alternatives.
GUIs work mostly by attaching commands to files, not the other way around. You can do that using file extensions. You can do that using Send To.
File extensions permit the association of an command (e.g. perl) with a file extension (e.g. .pl) Using "assoc" and "ftype", one associates the name to an extension, and a command to that name.
I'm not at my Windows machine at the moment, so I can't give you the exact usage. Type "help assoc" and "help ftype" at the prompt for usage. Use "%1" (quote included) in the command to indicate where the name of the clicked file should appear in the command.
Whenever you double-click on a file with that extension, it will be launched in with your command.
Alternatively, you can use "Send To". Somewhere in your profile is a directory named "Send To". (Just do a search to find it.) Put a shortcut to the application in there. To use the shortcut, right-click on the file you want to open, choose Send To, choose the application.
Eric
Check out the cmd command (the modern version of the DOS command line on Windows), which can invoke itself recursively. The command-line options are available by typing cmd /? at a command prompt.

How do I open a file using its default application from Perl on Windows?

I have a directory of files that I would like to scan on a regular basis and execute with the default application they are associated with. They are not executable so system("file.torrent"); does not work. How are you able to run files with there associated applications in Perl?
The standard windows way is with ShellExecute.
In perl you can do it with, well, ShellExecute. Its in the Win32::GUI package.
Have not tried it. But it looks simple enough.
start
You could manually parse the relevant part of the registry, find the associated application, and kick it off yourself: but the command prompt's built-in start command life easier.
So, for your example you would simply do a system("cmd /c start file.torrent")
Not Perl specific but you can always use the 'start' command. The first argument will be the title of the new command prompt opened and the second argument is the file to open.
system('start "dummy title" "some file.doc"'); # opens the document in word
Another option we use is
system("RunDLL32.exe SHELL32.DLL,ShellExec_RunDLL file.torrent")
Never use system() on windows !
Crappy and bad method (PAS)
Just to highlight the comment by BeowulfOF above
system( "test.log" )
will open test.log in it's associated application just the same as entering
test.log
on the command line

Resources