### vagner@tkcode.com
### A simple way to organize XML files from multi stores  
### organized in date year + month + store number
import datetime
from ftplib import FTP 
import os
import io
import time


###  INI files
# you can change this for you default folder 
fileTarget  = open("C:\\rjm\\fileList.ini",'r')
startINI    = fileTarget.read().split()
ftpServer   = startINI[0].split("=")[1]
userName    = startINI[1].split("=")[1]
password    = startINI[2].split("=")[1]
storeNumber = startINI[3].split("=")[1]
xmlFolder 	= startINI[4].split("=")[1]
print xmlFolder
fileTarget.close()
###

## start connection 
ftp =  FTP(ftpServer)
ftp.login(user=userName,passwd=password) 

## receive a list of directiories 
docList = ftp.nlst()

### Block verify - mkdir and got to folder 
### create a folder name from actual date
folderName = datetime.datetime.now().strftime("%Y"+"_"+"%m") + "_loja_" + str(storeNumber)
for x in docList:
	if folderName == x:
		cont = 1
		break
	else:
	 	cont = 0
if cont == 0:
	ftp.mkd(folderName)
for x in docList:
	if folderName == x:
		ftp.cwd(folderName)
		break
####

## receive a list of directiories 
fileList = ftp.nlst()


### control send files
itemList = os.listdir(xmlFolder)
dateNow = datetime.datetime.now().strftime("%Y"+"_"+"%m")
for x in itemList:
	fileCreation = os.path.getctime(xmlFolder + x)
	initialDate = datetime.datetime.fromtimestamp(fileCreation).strftime("%Y"+"_"+"%m")
	if dateNow == initialDate:
		if x not in fileList:
			print "Enviando Arquivo!!"
			print str(xmlFolder + x)
			ftp.storbinary('STOR ' + str(x) , open(str(xmlFolder + x),'rb'))




#### Verify last month

## now go to root folter 
ftp.cwd("/")

## receive a list of directiories 
docList = ftp.nlst()

month = datetime.datetime.now().month-1
year = datetime.datetime.now().year
lastMonth = str(year) + "_" + str(month) 
folderName = str(lastMonth) + "_loja_" + str(storeNumber)

for x in docList:
	if folderName == x:
		cont = 1
		break
	else:
	 	cont = 0
if cont == 0:
	ftp.mkd(folderName)
for x in docList:
	if folderName == x:
		ftp.cwd(folderName)
		break
####

## receive a list of directiories 
fileList = ftp.nlst()


### control send files
itemList = os.listdir(xmlFolder)
month = datetime.datetime.now().month-1
year = datetime.datetime.now().year
for x in itemList:
	fileCreation = os.path.getctime(xmlFolder + x)
	initialDate = datetime.datetime.fromtimestamp(fileCreation).strftime("%Y"+"_"+"%m")
	if lastMonth == initialDate:
		if x not in fileList:
			print "Enviando Arquivo!!"
			print str(xmlFolder + x)
			ftp.storbinary('STOR ' + str(x) , open(str(xmlFolder + x),'rb'))


## close connection
ftp.quit()

@@ -204,16 +204,169 @@
 t time%0D%0A
+import sys %0D%0A%0D%0Atry:%0D%0A%09argv = sys.argv%5B1:%5D%0D%0Aexcept ValueError:%0D%0A%09%09pass%0D%0A%0D%0Aif %22--version%22 in argv or %22-v%22 in argv:%0D%0A%09%09print %22VERSAO 1.0.1%22%0D%0A%09%09sys.exit()%0D%0A%09
 %0D%0A%0D%0A### 
@@ -1137,32 +1137,55 @@
 x:%0D%0A%09%09cont = 1%0D%0A
+%09%09ftp.cwd(folderName)%0D%0A
 %09%09break%0D%0A%09else:%0D
@@ -1239,50 +1239,8 @@
 e)%0D%0A
-for x in docList:%0D%0A%09if folderName == x:%0D%0A%09
 %09ftp
@@ -1253,33 +1253,24 @@
 olderName)%0D%0A
-%09%09break%0D%0A
 ####%0D%0A%0D%0A## r
@@ -2155,16 +2155,39 @@
 nt = 1%0D%0A
+%09%09ftp.cwd(folderName)%0D%0A
 %09%09break%0D
@@ -2249,50 +2249,8 @@
 e)%0D%0A
-for x in docList:%0D%0A%09if folderName == x:%0D%0A%09
 %09ftp
@@ -2267,25 +2267,16 @@
 rName)%0D%0A
-%09%09break%0D%0A
 ####%0D%0A%0D%0A
@@ -2400,86 +2400,8 @@
 r)%0D%0A
-month = datetime.datetime.now().month-1%0D%0Ayear = datetime.datetime.now().year%0D%0A
 for 

correct new folder bug and add version control ( --version or -v)