Monday, October 4, 2010

Scriptcode: misc batch files and visual studio macros

Here are the random macros I use in Visual Studio and windows batch files. Nothing monumental, but I find them useful often enough.
  • scheib.vb
    • The general purpose Visual Studio macros I use, particularly useful to me are:
  • addpath.bat
    • Eases adding more directories to your path environment variable.
  • cmd_here.bat
    • Right click any directory or file in windows explorer or a file save/open dialog and get a command prompt at that location.
    • Assists automation to copy certain files from one directory to another, e.g. just the .html files but not the images.
  • remove_empty_directories.bat
    • Cleans up a directory tree to not have empty directories.
The following are useful to have when writing a batch file:
  • isadirectory.bat
  • isafile.bat
  • isemptydirectory.bat
Files can be downloaded here: - visual studio macros - batch files


  1. That is cool, thanks for posting.

    An alternative approach to command-line work and scripting Windows that I like is to install Cygwin on my development machines. Then you have a few options, starting with the least intrusive:

    a) Add %SystemDrive%\cygwin\bin to your path, then you can use all the standard Unix command-line power tools (grep, less, sed, awk, etc) alongside your normal DOS commands, both at the normal Cmd command line and in .bat scripts.

    I like to add it foremost to the PATH, so that in cases where an executable is defined both by Windows and by Cygwin (e.g. 'find') then I get the (generally more powerful) Cygwin version. Some DOS commands (like 'date') are not executables, they are built into the Cmd shell, so they always shadow any executables on the PATH.

    Things like 'copy-certain-files' are now covered by normal 'cp' and 'mv' commands.

    b) Associate .sh files as bash script files:

    assoc .sh=BashScript
    ftype BashScript="%SystemDrive%\cygwin\bin\bash.exe" "%1" %*

    Now you can use bash shell scripts alongside your normal command-line practices.

    Loops and conditional logic are easier in Bash than in DOS, so this makes scripts shorter and more readable.

    c) Finally, once you are used to Bash in scripts, you might want to start using it as your default command-line shell instead of Cmd.

    Whether you use level (a), (b) or (c), I find the 'Console' project on sourceforge an invaluable replacement for the default Windows console. It separates the concept of the terminal (the window you type in) from the shell (the environment that runs your commands), allowing you to run many different shells (Cmd, Bash, Python, etc) all from within the same kind of Window.

    I've never tried to deploy any of this to clients, this is just for my personal development machines.

  2. Thanks. Yes, I use cygwin/bash more than cmd, actually. ;) A lot of these macros are pretty legacy, but they are handy when I'm dashing about in cmd, and I find myself going back to get them.

    I'm said that unix is more powerful, but more tedious too. "List me all files with a particular extension", "Rename all files with a given extension to a new one -- or rename the base file name". So much easier with "dir *.bat", "rename *.txt *.tx_", "rename file.* f.*"... I guess I could build up a pile of script and tote them around everywhere I go... but...

  3. Hey.

    I see. Figured I'd have to get up pretty early to be teaching you any tricks. I guess was writing the comment for the googling masses.

    'ls *.bat' works fine, but yeah, I didn't know Cmd 'rename' worked like that - that is handy.


  4. This is great, thanks. Your HeaderFlip macro is like Alt-O in Visual Assist, which I used to love but ultimately uninstalled due to performance reasons. Now I have the shortcut back, thanks!

  5. @Patrick:
    ;) Indeed, I wrote it when Visual Assist became bloated and unreliable. The 2 features I really missed were ALT-O and the Open any file by substring. CTRL-/ and "of prefixOfFileName" sorta helps there.

    Oh, I'm always up for learning tricks! Send them at me!

  6. Hey Vince, do you know which versions of Visual Studio the macros work on? In particular what the workiness of them on VS2008 and VS2010?

  7. Ok, it does indeed work on VS2010 -- the FlipHeader macro anyway. However, I tried the Chromium one too, and it's significantly faster than yours, at least on VS2010. There's quite a delay going from .h to .cpp in your version, maybe 0.5 sec. I even tried rearranging the extension order in yours to put .h and .cpp first, but speed didn't change much.