Monday, December 18, 2006

macros in good old DOS

I just read this post (from Jeffery Hicks?): Make an old friend do your work where he explains how you can use good old doskey to make your life easier (at the command line), and it motivated me to write a blog entry for 2 reasons:

- he missed a few points
- I want to remember this stuff too, since it seems I'm starting to forget stuff =o(

Let's see, first of all he defines a macro like this:
doskey xl="%programfiles%\Microsoft Office\Office11\Excel.exe" $1 $2 $3 $4 $5

and explains "You can use variables like $1 and $2 (all the way up to $9)".
but why limit yourself to 9 parameters? you can write it as:
doskey xl="%programfiles%\Microsoft Office\Office11\Excel.exe" $*

that will take whatever number of parameters you pass to it. To test that it actually took more than 10 parameters I created a file 1.xls and then ran this command (a lot of people don't know this one):

for /l %e in (2,1,11) do copy 1.xls %e.xls

Which by the way means (loop from 2 to 11 in steps of 1, copy file 1.xls to [variable].xls);
now I had 11 files to play with, so I called my macro:
xl 1 2 3 4 5 6 7 8 9 10 11

which brings me to another point he missed: "from a command prompt I can type xl file1.xls file2.xls file3.xls and have Excel open all three files"
You don't have to specify the extension, you can just pass the file name-no extension and excel will open it happily

This also brings me to another point: if you just wanted to open a single file, you could just type the file name:
1.xls
and Windows will open it in the default application (most likely excel) so for single files, we don't even need the macro

finally, this macro thing doesn't really work for notepad (or notepad2) if you call
np 1.txt 2.txt it tries to open the file "1.txt 2.txt"

you can only open single files =o(

one last tip, to delete a macro you just do
doskey myMacro=

6 comments:

Jeffery Hicks said...

I'm glad you enjoyed the article. I'm always open to improving things and am open to suggestions if I've overlooked something. So I had to test out your comments. I think you're missing something.

First, even though you can do a doskey macro like

doskey xl="%programfiles%\Microsoft Office\Office11\Excel.exe" $*

You are still limited to opening 10 files. Notepad is an exception and there are probably others. But try it with Word. Create 12 text or doc files. At a prompt type macroname file1.txt file2.txt etc

You should only see 10 files open. Using the FOR command is something completely different. That parameter type is different than doskey.

As for invoking an application by filename I did state that all you have to do is type the file name. However, if you want to open the file with a different application, then a doskey macro comes in handy.

Finally, thanks for the reminder about deleting a macro. I should have included that.

BlackTigerX said...

mmm... I tested that here (and did again just now) and it works fine, I can open 11 files no problem

the for loop was just a side thing, which just helped me to create 11 files and showed a lot of people who don't know about the for loop using numbers

and the invocation by file name, yes, it was included in your article, although I thought it was mentioned explicitly more than implicitly so just wanted to make the point clear

and finally
1 - I just wanted to extend and clarify some points on your article
2 - I didn't think you would read this =oS

best regards

Jeffery Hicks said...

You can use a doskey macro and pass it more than 10 parameters? That just doesn't seem right. What is the exact doskey macro syntax you are using to create it and what is the command expression you are using to open 10+ files?

BlackTigerX said...

doskey xl="%programfiles%\Microsoft Office\Office11\Excel.exe" $*

and call it:
xl 1 2 3 4 5 6 7 8 9 10 11 12 13

opens 13 files for me

Jeffery Hicks said...

That's interesting. Try it with a macro for WinWord. I can't get that to open more than 10 files. I think what we may have to conclude is that the limitation may be application specific, such as Notepad's refusal to open more than one file. I'm wondering if it is related to the fact that Excel opens all files in one instance of Excel, but Word (at least for me) opens a new instance for every file.

BlackTigerX said...

that seems to be the case, and also with Winword you have to specify the extension for each document =o(