Copy a Directory to Network Share

Hi Everyone,

Please refer the attached script for copying a directory to the Network Share.

Note:

  • This script should be Run as System User.
  • Please edit parameters such as src_path, dest_fname, share_path, share_user, share_pass for copying the directory according to your requirement.
  • "share_user" - This is said to be the network share username for access
  • "share_pass" -This is said to be the network share password for access
  • "src_path" - Provide the path of the folder where the file residing
  • " share_path" - Provide the path for the network shared folder
  • "des_fname" - This is said to be the destination folder name which has not been exist earlier on the share path


src_path=r'C:\file_share'  #source folder path need to be copied 
share_path=r'\\PARKER-PC\script'  # Destination share path
des_fname='share'   # please provide the name of the folder that does not exist earlier in the give share path
share_user="Parker"  # share path user name 
share_pass="COMODO" #share path password

import ctypes
class disable_file_system_redirection:

    _disable = ctypes.windll.kernel32.Wow64DisableWow64FsRedirection
    _revert = ctypes.windll.kernel32.Wow64RevertWow64FsRedirection
    def __enter__(self):
        self.old_value = ctypes.c_long()
        self.success = self._disable(ctypes.byref(self.old_value))
    def __exit__(self, type, value, traceback):
        if self.success:
            self._revert(self.old_value)

import os
import shutil
cmd='NET USE "'+share_path+'" /USER:'+share_user+'  "'+share_pass+'"'
tar_path=r'\\PARKER-PC\script'+'\\'+des_fname
print 'Login to network share'
with disable_file_system_redirection():
    print os.popen(cmd).read()

print 'Copying files to local machine....'
if os.path.isdir(src_path):
    shutil.copytree(src_path,tar_path)
    print 'Script execution completed successfully'
else:
    print '%s is not found'%src_path


Sample output:

20170615-Copy-a-Directory-to-Network-Share.json (1.94 KB)