Sunday 23 September 2018

windows - How do I actually open a program from a batch file? All it does is open a command prompt with that file name



I'm currently trying to learn Batch file scripting so maybe I'm just not seeing what I'm doing wrong but I am just making a very simple batch file that opens up a text file.


Here is the code I am using:


@echo off
title Opening a File Lesson
START "C:\Projects\batch\hi.txt"
pause

Instead of opening the textfile like it should, all it does is open the Batch File command prompt with the corresponding output:



Press any key to continue . . .



Which is fine, that's what I intended but then instead of opening the text file, it just opens ANOTHER command prompt with the file path: C:\Projects\batch\hi.txt as the caption and does absolutely nothing but output my default path:



Microsoft Windows [Version 6.1.7600] Copyright (c) 2009 Microsoft Corporation. All rights reserved.


C:\Projects>



Can someone guide me as to what I am doing wrong and why it's not opening with a default program? I tried opening other files too like Outlook and Visual Studio 2010 using the absolute(target) path and it does the same thing so its not just text files. Any help would be appreciated, I'm not used to scripting languages just yet.



Answer



To keep in line with your current example, I would add the following:


START "" notepad.exe C:\Projects\batch\hi.txt

because what you really want to do is open notepad and pass in a parameter, in this case it's the filename. Athom's solution may work also, but this one just sprung to mind.


If you want to wait for notepad to close before you finish:


START "" /wait notepad.exe C:\Projects\batch\hi.txt

The title of the new batch window is inside the quotes, in my examples I don't need a title so I leave it blank. I think the START command expects one, so it might be mandatory.


I also suggest learning powershell when your done, the words power and shell really describe it's usefulness. I wouldn't say give up on batch though because right now some things are just easier in batch. Also at this point powershell can't return negative exit codes which just sucks, but I digress.


No comments:

Post a Comment

Where does Skype save my contact's avatars in Linux?

I'm using Skype on Linux. Where can I find images cached by skype of my contact's avatars? Answer I wanted to get those Skype avat...