Skip to content

Instantly share code, notes, and snippets.

@spookyahell
Last active February 19, 2023 14:04
Show Gist options
  • Save spookyahell/e529f4db9b28a5e70f36edf14805189b to your computer and use it in GitHub Desktop.
Save spookyahell/e529f4db9b28a5e70f36edf14805189b to your computer and use it in GitHub Desktop.
Place this in your path (and add .py to PATHEXT if not already done so) to be able to swtich to between seasons seamlessly)
import sys, re
from os import getcwd, remove, sep
from subprocess import call
season = sys.argv[1]
cwd = getcwd()
switch_to = re.sub(r'S(\d+)', f'S{int(season):02}', cwd)
call(['cmd', '/k', f'TITLE {switch_to.split(sep)[-1]} (in {sep.join(switch_to.split(sep)[:-1])})'], cwd = switch_to)
@spookyahell
Copy link
Author

spookyahell commented Feb 19, 2023

Usage (once properly placed):
cds 2

Expected behavior/conditions:

  • Switches to a folder (in a new subprocess) with S2 or S02 in the title if the previous cwd was containing a season number as well)
  • Does not change the dir on folders that don't follow a specific generic tv season format, please refer to https://gist.github.com/spookyahell/001d75e5e4eaf63dbdcb93d2c4155051
  • However, it has a notable effect on regular folders to: You can "abuse" the implemented Window title improvements (the gist from the link above has even more; they mostly show up when working with external drives)

Known problems with this approach:

  • None of the command history is transferred when switching directories, since you're starting a whole new sub-console.
  • The exit command will only close the most recently created subprocess.
    This means, it will act like a history of places you have gone with cd2
    • If you know the first folder you opened with cd2 by name, you can find it in taskmgr.éxe
      (if you enable the full command line in the "Details" tab)
      and then just right-click it and select "Kill all process-tree"
  • As far as I could tell, there is no proper method that would simply change the active directory of a cmd.exe console from within python (or any other subprocess, for that matter - or like Tombart@SO said, You can't. Like in real life you can't change the path of your parents :)) -- The only other theoretical approach I could think of would be to actually have python type the command itself, but the time required for completion of this method may not be so pleasent, however one of these days, I may give that a try as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment