Tuesday, March 10, 2015

Make New Directory Script

I like to keep my projects organized by date. I  name new project folders with this format inside a "Project" folder on the root of my data drive:

YYYY-MM-DD - Project Description

It'll end up like this:

D:\Projects\2015-03-10 - Make New Directory Script\

It's an easy way to keep things consistently organized. I'll usually have some ambiguous reference directories in there that I don't want to be mixed in with regular projects (e.g. development, temp data, snippets, links, tools, logos, notes, etc.).

Shortcut Method

Another method is to add a Date Created sorting option to your file explorer window, but this may not help keep projects separated from those reference directories:
  • Open Windows Explorer
  • Press Alt + v to open the View menu, and choose the Details view. Sorting options will appear over the main file area of the window
  • Right click on that details bar and choose More...
  • Scroll down and check Date created to add it as a sorting option. Look around the Details menu and select/deselect parameters that are useful or unhelpful

Make a Formated Folder with Python

Let's get real. I like my method.

I'll also be honest: I'm not saving any real time here.  I can easily click the New Folder button and immediately start typing a folder name, but I have to look ALL the way down at the clock to verify the date, then I gotta type all 13 characters of the date prefix. I'm over it.


I threw these two tricks together in five lines using Python's os and datetime modules - And 40% of that just calls those modules!

 import os  
 import datetime  
 today = datetime.date.today()  
 newpath = today.strftime('D:\Projects\%Y-%m-%d - Description')  
 if not os.path.exists(newpath): os.makedirs(newpath)  

Line 3 prepares the today() date object
Line 4 formats the path and embeds the 4-digit year (capital %Y), etc.
Line 5 even checks to  make sure the path doesn't already exist.

Killer.

As long as you have Python installed, you can save this code as makeNewDirectory.py and run it. It should take just a fraction of a second to run.

Batch File

I was trying to run this as a .bat file, but it was kind of tricky and I got bored. Here's what I came up with.

mkdir easily makes a new directory, and this comment on StackOverflow pulls apart a %date% stamp with concatenation. I got them to work separately, but not together. Play around with underscores and let me know what you can find.

No comments: