Thursday, April 15, 2010

'Parameter is incorrect' error when editing empty batch file in Windows 7

Background

As I've already said, I write batch scripts often. In Windows XP (and ideally Windows 7) I'd follow these steps to create a new bat file:
      Right click and choosing New > Text Document
      I then change the file extension to .bat
      Next I right click on the file and choose Edit
      A blank batch script opens up in Notepad.

Unfortunately the steps above fail in Windows 7. Windows 7 does not like to you edit blank batch files. From what I understand it has something to do with the way UAC treats batch file (yes even when just editing them, and yes even if UAC is turned off). This really cramps my style and inhibits my workflow.

The unfortunate part is that since this is a UAC/Windows 7 problem and not a Notepad problem we won't be helped by editing the context menu item to point it to some other editor like Wordpad or Notepad++.

Recreating the Problem


To see this in action simply follow the steps above to create a blank batch file and try it out. You'll get a vague "parameter is incorrect" error. Now, to confirm that it indeed only happens with blank documents open notepad separately from the start menu and drag and drop the blank bat file inside. Add a few characters to the file hit save, then close notepad. Now right click on the batch file and click Edit. Voila! Magically the Edit command in the context menu works. I haven't found a fix for this yet but will post as soon as I do.

Solution


The problem, as it turns out, exists not only for batch files but also for registry files and other "protected" (aka locked down) files. These files have the special capability of editing the system's resources and thus they have special protection against being run/edited when they are empty. I don't know if this is supposed to be protection against null pointer protection but it sure annoyed me.

Anyhow, I (kinda) found a solution. Basically, instead of fixing the actual problem this is a workaround that puts one space inside of all newly created text documents. It only applied to those created by right clicking and choosing New > Text Document. This fix means text documents will no longer be empty and therefore won't throw the error.

The fix it to add a new string value in the ShellNew portion of the registry entry for the .txt file type. The value you need to create is this one:

[HKEY_CLASSES_ROOT\.txt\ShellNew]
"Data"=" "


If you are lazy like me you can get a .reg file to make the change for you by downloading this solution's zip file from my Google Group.

As you can see this just creates a space in the text files when they are created. It is a bit dirty, but works, and will get you on your way! Again, let me know if you come up with a more elegant solution.

1 comment: