Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question

marked as duplicate by Karan, Scott, Brad Patton, Simon, 8088 Apr 25 '13 at 16:17

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1  
tried with no start and no " " ? –  Lorenzo Von Matterhorn Apr 24 '13 at 15:13
2  
"start" opens a new CMD. If you want it to launch a program, put the name of the program instead. –  sep332 Apr 24 '13 at 15:14
2  
If you are just starting to learn and you are on W7 or you don't mind installing it, I would highly recommend learning Powershell instead. –  EBGreen Apr 24 '13 at 15:20
    
@EBGreen Well I plan to branch into other languages and environments as well but the company I work for uses batch scripts a lot so I have to learn the syntax. Using the direct file path without the start and " " seems to work, but I can't help but get the feeling that's a sloppy way of doing it. Is that the correct way? –  Chris Apr 24 '13 at 15:23
    
Sure, as long as you're comfortable with the system deciding what program to use to open the file, it's no problem. –  sep332 Apr 24 '13 at 15:30
show 5 more comments

2 Answers

up vote 1 down vote accepted

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.

share|improve this answer
add comment

.txt does not require the start command.

Just call it:

"C:\Projects\batch\hi.txt"

Other formats DO require the start, for example csv I use start.

share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.