Python ->
Python script for searching and copying numerous files

The task is try to find all files according to some list in given directory (including sub folders). If file is found - copy it to destination folder (outcome).

# find files in given folder (the first match) "C:\Archive"
# files to search are listed in search_list.txt file
# save found files to destination folder "C:\out_files"

import os
import shutil

def find_file(name, path):
    # find file in given path (the first match)
  for root, dirs, files in os.walk(path):
   if name in files:
    return os.path.join(root, name)


def copy_file(src, dest):
    # copy file from source to destination
  try:
   shutil.copy(src, dest)
    # eg. source and destination are the same file
  except shutil.Error as e:
   print('Error: {0}'.format(e))
  # eg. source or destination doesn't exist
  except IOError as e:
   print('Error: {0}'.format(e.strerror))


def main():
  # main begin

  search_dir = 'C:\Archive'
  destination_folder = 'C:\out_files'

  # open .txt file, 'r'ead mode is default
  # each line in search_list.txt includes file name

  f = open('C:\Archive\search_list.txt')

  while True:
  # begin while loop

   # read each line from .txt file + cut off line break char
   file_name = f.readline().strip('\n')
   # try to find file in search_dir
   file_path = find_file(file_name, search_dir)

   # if file is found, copy it to [out_files] folder
   if file_path:
    copy_file(file_path, destination_folder)

   # Zero length indicates EOF
   if len(file_name) == 0:
    break

   print file_name

  # end of while loop

  # close the file
  f.close()

 # main end

if __name__ == "__main__": main()




sqlexamples.info