Introduction
Here's a little python script that solves the number one problem of shell programming in a Windows environment, namely: How to open the command window in the correct folder?
Many people have clicked and mis-clicked over the years using the "shift-click folder" shortcut built into Microsoft explorer. Personally, I've always found this method a bit clumsy, mixing up the shift and control keys and not clicking on the right folder icon where the "command window here" menu is active.
Recently, I was playing around with python and tkinter on Windows and discovered a new trick for opening a command window wherever you want them. The Idea is super easy and almost trivial: The program just prompts the use for a directory to select and opens a customize windows command box in the selected directory.
Why solve this problem using python? Simply because that's what i was using at the time when i needed to open a command box in a selected directory. But, certainly I could imagine a C# freeware program that does a similar function, and perhaps expands on the idea by providing a launcher menu with adjustable parameters and loadable profiles to create your command box from. I'm just presenting the simplest lowest common denominator approach using Python.
Using the Code
The code is really simple. Popup a tkinter dialog box prompting the user to select a directory. Then, pass this information on to a system call to start a command box.
The only trick to this simple program, under tkinter environment, is to hide the toplevel window, so that it only shows the dialog box.
Here's the code:
"""
File : m$term.pyw
Creator : WPM (Bill Moore)
tkinter gui to open windows command box
in a selected directory
SETUP: make sure pyw is associated with "pythonw.exe" from microsoft explorer
and place this script on your desktop so that it can be double-clicked
whenever you need it.
"""
import tkinter as tk
import tkinter.filedialog
import sys
import os
def TkStartTerm():
result = tk.filedialog.askdirectory(
initialdir = ".",
mustexist = True,
title = "Open Command Window Here"
)
if not result:
return None
os.chdir(result)
os.system(
'start cmd /k "' +
'color 1f & ' +
'mode con: cols=140 lines=15000 &' +
'title cmd ' +
'"'
)
def TkRootHide():
tk.Tk().withdraw()
if __name__ == '__main__':
TkRootHide()
TkStartTerm()
sys.exit(0)
That's it. Enjoy!