Search Results

Windows shell extension to easily add any folder to the PATH

How many times has this happened to you? You’ve downloaded a new Windows command-line tool and want to be able to use it from wherever your current directory happens to be, but the only way to add a folder to Windows’ system PATH is via an unwieldy and hard-to-find modal dialog in the System Properties’ [...]

How many times has this happened to you? You’ve downloaded a new Windows command-line tool and want to be able to use it from wherever your current directory happens to be, but the only way to add a folder to Windows’ system PATH is via an unwieldy and hard-to-find modal dialog in the System Properties’ Advanced tab. Well, I had just this problem last week when I was configuring some new machines and wanted to add some of my favorite command-line tools (mostly ports of Linux/Unix tools like which and less). I was about to go through System Properties to add their directory but figured there had to be a better way.

After a few minutes of Googling, I found this post by “/\/\o\/\/” which demonstrated a way of using Windows PowerShell (the next generation advanced command-line environment for Windows) to add a shell extension, used by right-clicking on any Explorer folder, which adds the selected folder to the system PATH.

Unfortunately, the code shown in the post didn’t quite work as-is and besides I’m not too comfortable yet in PowerShell. So I decided to fix the code and build a script that does the same thing but from CMD (or by double-clicking). It’s very simple to use; just execute the script from a CMD session or double-click it in Explorer. To uninstall, just run it again; it automatically backs out its changes when run a second time.

License and acknowledgement: The original (though non-working) code sample is by “/\/\o\/\/” and linked above; I claim no credit for it. However, I made it work and packaged it into a user-friendly script and so claim copyright over that portion under the GPL.

With that out of the way, here’s the code:


::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: AddToPath.cmd ::
:: Written by Andrew Rich (andrew.rich@project-insomnia.com) ::
:: Copyright (c) Andrew Rich under GPL (http://gnu.org/licenses/gpl.html) ::
:: Based on code by "/\/\o\/\/" at http://bit.ly/58hYYh ::
:: ::
:: This command script adds an Explorer shell extension which, when ::
:: selected on right-clicking a folder, will add it to the system PATH. The ::
:: change takes effect immediately for new CMD or PowerShell instances ::
:: started after adding the folder to the PATH. CMD or PowerShell instances ::
:: started adding the folder will continue to use the PATH in effect at the ::
:: time they were started. ::
:: ::
:: Usage: ::
:: Double-click AddToPath.cmd or execute AddToPath.cmd from a CMD session. ::
:: The only command-line parameter available is /? which shows this text. ::
:: ::
:: Requirements: ::
:: - Windows XP SP3 or later. Not tested on Vista or Windows 7 BUT should ::
:: work unaltered. Please send feedback if you run AddToPath on Vista or ::
:: Windows 7. ::
:: - The user executing the command script must be a local Administrator. ::
:: However, the PATH update will affect all users. ::
:: - reg.exe must be on the system PATH. This should be the case for any ::
:: properly functioning Windows machine. ::
:: - Microsoft PowerShell must be properly installed. This should be the ::
:: case for any Windows machine which is current on Windows updates. The ::
:: command script will verify that PowerShell is installed. If the script ::
:: reports that PowerShell is not installed, download and install it from ::
:: http://bit.ly/mYzg4. ::
:: ::
:: Uninstall/remove: ::
:: Just run AddToPath.cmd again. If the "Add To Path" shell extension ::
:: installed by AddToPath.cmd exists, running the command script a second ::
:: time will cause the shell extension to be removed. ::
:: ::
:: Manual uninstall: ::
:: From a CMD session, type: ::
:: ::
:: reg delete HKCR\Folder\Shell\Add_To_Path ::
:: ::
:: Note that uninstalling only removes the Explorer shell extension, and ::
:: does not affect any system PATH entries which may have been added. ::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::ENDHEADERCOMMENT

@Echo Off
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION

:: setup
SET ShellExtKey=HKCR\Folder\Shell\Add_To_Path
SET ShellExtKeyText="Add to Path"
SET PSPathKey=HKLM\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell

:: check command params
IF [%1]==[] GOTO CmdParamsDone
IF /I %1 EQU --help GOTO ShowHelp
IF /I %1 EQU /? GOTO ShowHelp
IF /I %1 EQU -? GOTO ShowHelp
GOTO ErrBadParam

:ShowHelp
ECHO.
FOR /F "delims=" %%A IN (%~fs0) DO (
IF /I %%A EQU ::ENDHEADERCOMMENT GOTO :EOF
ECHO %%A
)
PAUSE
GOTO :EOF

:ErrBadParam
ECHO.
ECHO "%1" is not a recognized command-line parameter for %0.
ECHO Type %0 /? for help.
ECHO.
PAUSE
GOTO :EOF

:CmdParamsDone
:: check overall prerequisite: reg.exe must be on the system PATH
reg 2>>NUL 1>>&2
IF NOT [%ERRORLEVEL%]==[0] GOTO ErrNoRegExe

:: is the Add_To_Path shell extension already installed?
:: if the reg query fails (errorlevel=1) then it's not installed
reg query %ShellExtKey% 2>>NUL 1>>&2
IF [%ERRORLEVEL%]==[1] GOTO Install
GOTO UnInstall

:Install
:: check install prerequisite: PowerShell must be installed
:: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell should contain the full path to powershell.exe
:: e.g. C:\WINNT\system32\WindowsPowerShell\v1.0\powershell.exe
FOR /F "usebackq tokens=3" %%a IN (`reg query %PSPathKey% /v Path ^| FIND "powershell.exe"`) DO SET PSPATH=%%a
IF NOT DEFINED PSPATH GOTO :ErrNoPowerShell
IF [%PSPATH%] EQU [] GOTO :ErrNoPowerShell

:: build the shell extension's command line
:: does powershell.exe's full path include spaces? if so, add quotes
FOR /F "tokens=2" %%a IN ("%PSPATH%") DO SET s=%%a
IF DEFINED s (
SET AddToPathCmd="%PSPATH%"
) ELSE (
SET AddToPathCmd=%PSPATH%
)
SET AddToPathCmd=%AddToPathCmd% -NonInteractive -NoProfile -Command [system.environment]::setEnvironmentVariable('path',$env:path + ';%%1','machine')

:: create the shell extension's base key under Folder\Shell />reg add %ShellExtKey% /ve /d %ShellExtKeyText% /f 2>>NUL 1>>&2
IF NOT [%ERRORLEVEL%] EQU [0] GOTO ErrRegAdd1

:: add the command line
reg add %ShellExtKey%\Command /ve /d "%AddToPathCmd%" /f 2>>NUL 1>>&2
IF NOT [%ERRORLEVEL%] EQU [0] GOTO ErrRegAdd2

ECHO Installed. Right-click on a folder in Explorer to add it to the system PATH.
PAUSE
GOTO :EOF

:UnInstall
reg delete %ShellExtKey% /f 2>>NUL 1>>&2
IF NOT [%ERRORLEVEL%] EQU [0] GOTO ErrRegDel
ECHO UnInstalled.
PAUSE
GOTO :EOF

:ErrNoRegExe
ECHO Error: reg.exe was not found on the system PATH.
PAUSE
GOTO :EOF

:ErrNoPowerShell
ECHO Error: Windows PowerShell is not installed (or is not installed correctly).
ECHO Go to http://bit.ly/mYzg4 to download and install Windows PowerShell.
PAUSE
GOTO :EOF

:ErrRegAdd1
ECHO Error creating the shell extension's base key.
ECHO Run %0 again to back out any changes.
PAUSE
GOTO :EOF

:ErrRegAdd2
ECHO Error adding the shell extension's command line.
ECHO Run %0 again to back out any changes.
PAUSE
GOTO :EOF

:ErrRegDel
ECHO Error removing the shell extension's registry entry.
ECHO To remove manually, type the following at the CMD prompt:
ECHO reg delete HKCR\Folder\Shell\Add_To_Path
PAUSE
GOTO :EOF

You can copy, paste and save that as AddToPath.cmd, or just download it (I recommend downloading it because line wrapping doesn’t show properly here). AddToPath.cmd.zip

I hope this is useful. Please send me feedback if you use it!

Leave a Comment

I’m a Mac. Are you surprised?

I grew up with PCs. My first PC was an actual, original IBM PC model 5150 (which still lives, dormant for decades now, in my garage). Along the way, I worked on Commodores (VIC-20 and C-64), an Osborne, a Sinclair ZX-80, and there must have been an Apple ][ in there somewhere. But the PC [...]

I grew up with PCs. My first PC was an actual, original IBM PC model 5150 (which still lives, dormant for decades now, in my garage). Along the way, I worked on Commodores (VIC-20 and C-64), an Osborne, a Sinclair ZX-80, and there must have been an Apple ][ in there somewhere. But the PC standard won out and by the late 80s that was all I used. I went through a series of ever-more-powerful PCs, mostly home-built but some mass-market branded, for a number of years. I got to the point where I knew Windows, up through XP, like the back of the proverbial hand.

Meanwhile, Apple was evolving the Mac OS and finally released OS X, a true Unix-class OS with no legacy baggage. I watched from afar but as the OS and machines got better and better, I thought that I might be interested in making a switch–especially when Apple moved to Intel processors and it became possible to easily run Windows on Macs. Finally, I decided that when my then-current PC (a Dell Inspiron laptop) died, I'd buy a Mac notebook to replace it. Perversely, the Dell hung on for a year or two past its expected lifetime, but finally gave up the ghost when I (accidentally, I swear!) spilled most of a bowl of soup into it while working at home one day.

So I bought a MacBook Pro. I acclimated myself to OS X very quickly and was able to keep my Windows applications and workflows mostly intact with VMWare, running Windows side-by-side on OS X. But then a strange thing happened: I found I really didn't need Windows on my Mac after all. I tried keeping VMWare turned off for a week, then for a month, and then I just didn't turn it back on again and finally uninstalled it. There isn't anything that I could do on my Dell on Windows XP Pro that I can't do on my Mac, but (in my experience, as always) OS X beats Windows in the usability and stability department by a mile. And it's trite and over-used, but the Mac does indeed "just work". Things I want to do are right where I subconsciously expect them to be and work the way I instinctively want them to. There's tons of power under the hood, since OS X is a true Unix, but I don't need to deal with it unless I have to, or want to.

So yes, after years and years of being a PC, I'm a Mac.

Leave a Comment

As retailers cut back cities confront ‘ghostboxes’ – SFGate

As retailers cut back cities confront ‘ghostboxes’: The building, sitting derelict and silent on acres of asphalt, is now listed for sale at $10.5 million. But there’s been little interest in the near windowless warehouse-like building that occupies a lot the size of a dozen football fields. For potential tenants ‘it’s a hard pitch because [...]

As retailers cut back cities confront ‘ghostboxes’:

The building, sitting derelict and silent on acres of asphalt, is now listed for sale at $10.5 million. But there’s been little interest in the near windowless warehouse-like building that occupies a lot the size of a dozen football fields.

For potential tenants ‘it’s a hard pitch because for most uses it seems to be a bit of a tough fit,’ said Brian Ritter, business development director of the Bismarck-Mandan Development Association.

As the recession takes its toll on big-box retailers, more communities across the country are having to confront not just the eyesore of giant empty stores, but also the loss of jobs and tax revenue that follow.


View Larger Map

The long-vacant former EXPO Design Center building at East Palo Alto’s Ravenswood 101, once thought to merge with the Home Depot next door to form a super-size building center, has now been divided into two spaces and houses a Nordstrom Rack and a Sports Authority. Both new retailers seem (from appearances: parking full, busy checkout lanes) to be doing quite well. A new microbrewery/restaurant (Firehouse Grill) has also recently opened and the former Circuit City location will soon reopen as a Mi Pueblo grocery store–EPA’s first supermarket in at least a decade.

Leave a Comment

I started using the Internet in around 1994

My first Internet connection was via 2400bps modem to Netcom, on a beige-box 386 probably running Windows 3.11. This would have been in the early to mid 90s, probably 1994. I had an @ix.netcom.com address, used NetCruiser on the nascent Web and poked around in newsgroups.

My first Internet connection was via 2400bps modem to Netcom, on a beige-box 386 probably running Windows 3.11. This would have been in the early to mid 90s, probably 1994. I had an @ix.netcom.com address, used NetCruiser on the nascent Web and poked around in newsgroups.

Leave a Comment

Confessions of a Switcher (part 3)

This is part three of a theoretically infinite series. It’s been roughly five months since I brought home the MacBook Pro and almost that long since my last update to this series. I’ve become incredibly comfortable in the OS X environment and, with a very few exceptions, can do anything I ever did in Windows. [...]

This is part three of a theoretically infinite series.

It’s been roughly five months since I brought home the MacBook Pro and almost that long since my last update to this series. I’ve become incredibly comfortable in the OS X environment and, with a very few exceptions, can do anything I ever did in Windows. In the event I do need Windows, I can use VMWare Fusion to boot Windows XP from a Boot Camp partition with seamless desktop integration. Just today I found a solution to one of the last Windows requirements — syncing my HTC Mogul phone, a Windows Mobile device. Normally one would use ActiveSync to sync a Windows Mobile device, or pay $30 for Missing Sync. I’ve found a free product that does exactly what I need and no more: Eltima Software’s SyncMate. It syncs my contacts and calendars to the Mac’s Address Book and iCal, respectively, and can mount the WinMo file system as an external volume on the Mac for file transfer.

Here’s a current list of third-party software I’m using.

  • Angry IP Scanner — the built-in Network Utility has most of this application’s functionality; I use either or both depending on what exactly I’m trying to do.
  • Book Collector
  • ChronoSync — I haven’t actually started using this yet, but I’ve installed the trial and am checking it out.
  • CrossOver Office — supposed to allow (some) Windows applications to install and run directly in OS X, but I’ve had little success as of yet.
  • Fetch — seems to be the best ftp client for OS X.
  • Google Earth
  • Jolly’s Fast VNC — even in public Alpha, this is the best VNC client I’ve found for OS X, and (apprehensive of using an Alpha) I tried quite a few before this one. Does what it says on the tin.
  • Logitech Harmony Remote software — Web-based programming tool for my Harmony 880 and 670 universal remotes.
  • Movie Collector
  • NetNewsWire — my choice for RSS newsreader. I started with the built-in Mail application, but it couldn’t handle 200+ feeds with any stability; I tried Endo and gave it a couple of months, but eventually gave up on it after one too many crashes and system resource grabs — plus, its UI is a nightmare. NNW does what I want and does it well.
  • OpenOffice.org — the excellent free, open-source alternative to Microsoft Office I’ve been using for years, now in a spiffy new OS X-native version.
  • Opera — if you’ve been reading Project Insomnia for lo, these many years, you know I’ve been an Opera fan for quite a long time. Since switching to Mac I’ve converted almost completely to Safari. I keep Opera around for alternate-browser testing and also use it when I need to have more than one Google Account session open simultaneously, but it’s pretty much fallen off my radar in general.
  • Remote Desktop Connection — the only Microsoft software on my OS X partition is a fine port of the standard RDC client.
  • SketchUp — nifty 3-D sketching tool which I have so far been completely unable to learn. I’d like to use it to model the cabinet wall we want to build in the living room.
  • SplashID — password vault, works with the Mogul to keep all my many and varied passwords safe. Syncing SplashID between the Mac and the Mogul is one of the very few remaining tasks for which I still need Windows; the Mac version doesn’t sync directly but only imports saved files.
  • SyncMate — see above.
  • TextWrangler — this is a terrific text editor that handles code of all kinds, from PHP to HTML to Java.
  • TinkerTool — essentially the OS X equivalent to TweakUI.
  • Transmission — BitTorrent client.
  • VLC Player — for the rare filetype that QuickTime + Flip4Mac can’t handle.
  • VMWare Fusion — see above.

I’m assembling a list of useful tips and tricks, things I’ve learned by trial and error or lucky Googling. That will probably be the subject of part four of this series.

Leave a Comment

Easy AdSense by Unreal

Project Insomnia is Digg proof thanks to caching by WP Super Cache