4

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.

An Dorfer
  • 1,178
  • 2
  • 8
  • 14
Chris
  • 43
  • 1
  • 1
  • 4
  • 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
  • Thanks @sep332, and Znau. I appreciate your help. I figure once I get the syntax down I will get used to doing relative paths anyway for my scripts. Seems more logical to use them that way. – Chris Apr 24 '13 at 16:09
  • @Znau ^^ :) just so you know that I meant to tag you as well. – Chris Apr 24 '13 at 16:10
  • Do not edit the question title, to reflect that it has been answered, that is not how this site works. Mark a answer as accepted, and upvote it. – Zoredache Apr 24 '13 at 16:23
  • @Zoredache Sorry :( a lot of websites I frequent similar to this use that method, i apologize. – Chris Apr 24 '13 at 18:49
  • Possible duplicate of [Strange behavior of Windows Start Command](http://superuser.com/questions/550662/strange-behavior-of-windows-start-command), http://superuser.com/questions/413939, http://superuser.com/questions/239565, http://superuser.com/questions/465028 ... – Karan Apr 24 '13 at 22:44

2 Answers2

7

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.

MDMoore313
  • 5,956
  • 1
  • 27
  • 31
2

.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.

Austin T French
  • 10,505
  • 27
  • 42
  • 3
    BTW, the ability to just call these applications without the start command (or even with it [but without specifically calling notepad.exe] as BigHomie is doing) is provided via the Windows MIME file type handling mechanism. Meaning that any file type which you can double click to open should work the same way here. The default action will be used. IOW, If Athom wanted CSV files to work he could associate CSV with notepad and he'd be able to use those files in the same way. – krowe2 Oct 28 '14 at 16:06