Sunday, January 16, 2011

Solved: Open Multiple Text Files Simultaneously with a Batch File

I wanted to open three text files and one program (not related to the text files) all at once. I did NOT want to double-click on all of them, or open a containing folder, highlight them all, then click enter. I wanted to double-click ONE thing and have all four objects open, all at once. This is for a bible study I'm doing only sometimes. At work I use a bunch of shortcuts in the Startup folder to start the things I always use every day, but that is not suitable for this.

The off-the-cuff answer was to write a quick batch file to do this, but .bat can be a frustrating thing to use. Most of the frustration comes from the way Batch wants one process to close before starting the next thing on the list. I tried various flavors and commands with CALL and START, but it would just by golly not open all the text files at once. This frustrated me enough to spend something like 3 hours finding the solution below.

The most pressing problem was opening the text files all at once. SaswatPadhi at go4expert presented the following to a user at that site, and it works perfectly within its limitations. Copy/paste this into a new text document (Notepad) and save it as file type "all files" with a filename ending in .vbs (this is a Visual Basic script). This code will open all the text files in a specified directory:

Dim objFSO
Dim MyFile
Dim MyFolder
Dim objShell

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set MyFolder = objFSO.GetFolder("C:\FolderName")
Set objShell = WScript.CreateObject("WScript.Shell")

For Each MyFile In MyFolder.Files
If Right(MyFile.Path,4) = ".txt" Then
objShell.Run("notepad " + MyFile.Path)
End If
Next


Replace C:\FolderName with the path to the folder containing your text files. I saved this as open.vbs and ran it, and it opened all the .txt files in the specified directory. There were a couple of text files in the directory I did NOT want to open, and I don't use them much, so I renamed them filename.tx1 and now open.vbs ignores them. I am sure there is a more specific way to do this to include only the files you want (or exclude the ones you don't) but this gets the job done.

That left opening the separate program. For this, I was sure I could use a batch file. I had managed to get a batch file to open the program and one text file, and I was pretty sure I could get it to open the program and my spanking-new .vbs file. I got it working . . . but, being a .bat file, the Command window wanted to stay open until the program it CALLED was closed. I thought STARTing the program would do it, but that just opened TWO Command windows and nothing else.

Someone else already had this problem, and Lasse V. Karlsen at stackoverflow.com gave the solution: the START command is picky. If you have a path or filename with spaces, the path needs to be inside quotes. It also sees what is in the first set of quotation marks as a title for whatever path/process (in quotes) follows. If what follows is another line with another START command, it doesn't work. START wants to start something with a name AND a path, if you use quotes.

So:
START "name" "C:\path\to.exe"
will work but
START "C:\path\to.exe"
will not.
note that the name doesn't have to be anything. A set of quotes with nothing between them will satisfy START, so
Start "" "C:\path\to.exe"
will work.

The reason not to use CALL is that it will keep the Batch file running in an open Command window until the last thing it CALLed is closed. START will shut the window when the process it STARTs is started.

So I ended up with one thing to double-click to open my text files simultaneously, AND open my program, AND shut itself off when it was done opening all these things. It's a .bat file that looks like this. Insert your paths and filenames as appropriate, and it should work for you too.

START "" "C:\My Folder\Second Folder\open.vbs"
START "" "C:\Program Files\Subfolder\program.exe"


Two lines, that's it. It doesn't even need a CLS or EXIT command. It works by double-clicking one icon for the .bat file. Simple, elegant, and 100% effective.

For those who need a little more spoon-feeding, what you need to do is copy/paste those lines into a new Notepad document, save it as file type All Files, and give it a file name ending with .bat (not .txt). If you need to open more stuff or different types of stuff, you're on your own, but don't worry - someone before you already had the same question; the answer is out there somewhere!

No comments: