Daily Snippet, Developer to developer

python’s time:replace your file

import os.path
from os.path import basename
import shutil
from xml.dom import minidom

xml_path = str(raw_input('Please enter xml file path : '))

xml_path = xml_path.replace('\\','\\\\')

##contoh input xml path = 'D:\ASB\replace_file_data.xml'
xmldoc = minidom.parse(xml_path)
rootNode = xmldoc.getElementsByTagName('ROOT')

path = rootNode[0].firstChild.data
print "path = " + path

filesNode = xmldoc.getElementsByTagName('FILE')
for fileNode in filesNode:
    source_file = fileNode.firstChild.data
    include = fileNode.attributes["include"].value
    print "source_file=" + source_file
    print "include=" + include

    if include == "1":
        file_name = basename(source_file)
        print "Begin process replace file_name=" + file_name
        for (path, dirs, files) in os.walk(path,False):
            for f in files:
                if file_name == f:
                    if source_file != path + '\\' + f:
                        shutil.copyfile(source_file, path + '\\' + f)
                        print "replace "+ path + '\\' + f
        print "---------success----------"

File Xml yang digunakan sebagai datasource (ugly xml, but it’s just work) :

<?xml version="1.0" ?>
<DocumentElement>
  <CONFIG>
    <KEY>DefaultRoot</KEY>
    <ROOT>D:\ASB\emap\Makasar\1.6.0.1\</ROOT>
	<UPDATE_FILE>
		<FILE include="1">D:\ASB\emap\Makasar\1.6.0.1\Spatial Dialogs\bin\Spatial Dialogs.dll</FILE>
		<FILE include="0">D:\ASB\emap\Makasar\1.6.0.1\GlobalClass\bin\GlobalClass.dll</FILE>
	</UPDATE_FILE>
  </CONFIG>
</DocumentElement>
Standard

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s