import shutil
import os
import sys




def file_list_(source_path, ext):
	eu = ext.upper()
	base_names = [s for s in os.listdir(source_path) if s.upper().endswith(eu)]
	abs_paths = [os.path.abspath(os.path.join(source_path, s)) for s in base_names]
	return base_names, abs_paths

def ensure_dir(f):
	try:
		os.makedirs(f)
	except OSError:
		if not os.path.isdir(f):
			raise
			
# def main():
def collectmain(sourceDir, outDir):
	
	# sourceDir = os.getcwd()
	roots = []
	# sourceDir = os.path.join(os.getcwd(), "State-1")
	# outDir = os.path.join(os.getcwd(), "State-1 Cases")
	# ensure_dir(outDir)
	
	# outdirs = []
	
	for root, dirs, files in os.walk(sourceDir):
		if root != sourceDir:
			roots.append(root)
			print root
		
	# for root in roots:
		# print root
		# print os.path.basename(root)
		# raw_input()
	totalFiles = 0
	for root in roots:
		filename = os.path.basename(root)
		for i in range(0, 24):
			number = str(i).zfill(2)
			caseName = "%s_Hour_%s.sav" %(filename,number)
			# print root
			# print caseName
			casefullName = os.path.join(root,caseName)
			# print casefullName
			# raw_input()
			try:
				shutil.copy(casefullName, outDir)
				totalFiles += 1
			except:
				pass
	
	print "Total Copied Files : ", totalFiles
	# sys.exit()
	
	
	
# sourceDir = os.path.join(os.getcwd(), "State-1")
# outDir = os.path.join(os.getcwd(), "State-1 Cases")
# collectmain(sourceDir, outDir)
