Updated copyright blurbs in all files to '# Copyright'
[hooke.git] / contrib / mfp_igor_scripts / FMjoin.py
1 # Copyright
2
3 '''
4 FMjoin.py
5 Copies all .ibw files contained in a folder and its subfolders into a single folder. Useful for force maps.
6
7 Usage: 
8 python FMjoin.py origindir destdir
9 '''
10
11 import os
12 import shutil
13 import sys
14
15 def main(*args):
16         if len(sys.argv) < 2:
17                 print 'You must at least specify origin and destination folders.'
18                 return 0
19         origin=sys.argv[1]
20         dest=sys.argv[2]
21    
22         if os.path.exists(origin):
23                 if os.path.exists(dest):
24                         if os.listdir(dest)!=[]:
25                                 print 'Destination folder is not empty! Use another folder.'
26                                 return 0
27                 else:
28                         print 'Destination folder does not exist, will create it'
29                         os.mkdir(dest)
30         else:
31                 print 'You provided a wrong origin folder name, try again.'
32         
33         origin=os.path.abspath(origin)
34         dest=os.path.abspath(dest)
35         
36         for root, dirs, files in os.walk(origin):
37                 for filename in files:
38                         if filename.split('.')[1]!="ibw":
39                                 continue
40                         filepath=os.path.join(root,filename)
41                         #to avoid overwriting, we collapse unique paths into filenames
42                         rawdest=filepath.split(os.path.commonprefix([origin, filepath]))[1]
43                         rawdest=rawdest.replace('/','') #for linux
44                         rawdest=rawdest.replace('\\','') #for windows
45                         destfile=os.path.join(dest,rawdest)
46                         print 'Copying '+rawdest
47                         shutil.copy(filepath,destfile)
48     
49         return 0
50  
51 if __name__ == '__main__':
52     sys.exit(main(*sys.argv))
53
54