311892b61f30d22707bd58c499d49a2c4fa4ac7e
[hooke.git] / contrib / mfp_igor_scripts / FMjoin.py
1 #!/usr/bin/env python
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 Alberto Gomez-Casado (c) 2010, University of Twente (The Netherlands)
12 This program is released under the GNU General Public License version 2.
13 '''
14
15 import os
16 import shutil
17 import sys
18
19 def main(*args):
20         if len(sys.argv) < 2:
21                 print 'You must at least specify origin and destination folders.'
22                 return 0
23         origin=sys.argv[1]
24         dest=sys.argv[2]
25    
26         if os.path.exists(origin):
27                 if os.path.exists(dest):
28                         if os.listdir(dest)!=[]:
29                                 print 'Destination folder is not empty! Use another folder.'
30                                 return 0
31                 else:
32                         print 'Destination folder does not exist, will create it'
33                         os.mkdir(dest)
34         else:
35                 print 'You provided a wrong origin folder name, try again.'
36         
37         origin=os.path.abspath(origin)
38         dest=os.path.abspath(dest)
39         
40         for root, dirs, files in os.walk(origin):
41                 for filename in files:
42                         if filename.split('.')[1]!="ibw":
43                                 continue
44                         filepath=os.path.join(root,filename)
45                         #to avoid overwriting, we collapse unique paths into filenames
46                         rawdest=filepath.split(os.path.commonprefix([origin, filepath]))[1]
47                         rawdest=rawdest.replace('/','') #for linux
48                         rawdest=rawdest.replace('\\','') #for windows
49                         destfile=os.path.join(dest,rawdest)
50                         print 'Copying '+rawdest
51                         shutil.copy(filepath,destfile)
52     
53         return 0
54  
55 if __name__ == '__main__':
56     sys.exit(main(*sys.argv))
57
58