Wednesday, July 6, 2011

Dear Dav Nads, help me make some folders

yoGirl: Davnads,  you put the "sic" in forensic bc you got skillz. 
Davnads: dat rite
yoGirl: I'm trying to stage some data on my network for a eDiscovery engagement that I need to process using the Cloud. I don't have time to manually create 500 staging folders with sub directories.
Davnads: Yo chair yo' Problem
yoGirl: :-(
Davnads:  Damn sad faces, they always get 2 me. Okay I will help!

In response to my fan mail, I created a ugly (I don't program for a paycheck) Python script that will assist the process of creating directory structures in mass. This script uses the os module.  "As is" the script will read a comma delimited file, containing 3 folder names, line by line.

Folder_Names.csv
David Nides,HDD SN XX,Mobile Phone,Network Share
Danny Nides,HDD SN XX,SharePoint Data,Network Share

For each line, it will create a directory structure consisting of the parent folder named based on the first line variable, and sub directories using the second, third, and forth line variables. For example:

  >David Nides
   >>HDD SN XX
   >>Mobile Phone
   >>Network Share


  >Danny Nides
   >>HDD SN XX
   >>Share Point Data
   >>Network Share


The code is listed below. Note it is currently set to write the folder structure out to the "D:\" drive but this can be easily changed. Let me know if you have any questions. 

-----------------------------------------------------------------------------------------------------

#Created by David Nides, 6/29/11
#This python script will input a CSV file (refer to the input.txt template)
#Parse each row and create a directory.
import os
import csv
file = csv.reader(open('folder_names.csv'), delimiter=',')
for row in file:
    os.chdir('d:')
    os.mkdir(row[0])
    print "creating ",row[0]
    temp1='d:/'+row[0]+'/'+row[1]
    temp2='d:/'+row[0]+'/'+row[2]
    temp3='d:/'+row[0]+'/'+row[3]
    os.mkdir(temp1)
    print "creating ",temp1
    os.mkdir(temp2)
    print "creating ",temp2
    os.mkdir(temp3)
    print "creating ",temp3
    os.chdir('d:')