21951b28dc22bf5cb4285c6d5b9f4aa730e2e631
[hooke.git] / contrib / mfp_igor_scripts / FMjoin.py
1 # Copyright (C) 2010-2011 Alberto Gomez-Casado
2 #                         W. Trevor King <wking@drexel.edu>
3 #
4 # This file is part of Hooke.
5 #
6 # Hooke is free software: you can redistribute it and/or modify it
7 # under the terms of the GNU Lesser General Public License as
8 # published by the Free Software Foundation, either version 3 of the
9 # License, or (at your option) any later version.
10 #
11 # Hooke is distributed in the hope that it will be useful, but WITHOUT
12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 # or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General
14 # Public License for more details.
15 #
16 # You should have received a copy of the GNU Lesser General Public
17 # License along with Hooke.  If not, see
18 # <http://www.gnu.org/licenses/>.
19
20 '''
21 FMjoin.py
22 Copies all .ibw files contained in a folder and its subfolders into a single folder. Useful for force maps.
23
24 Usage: 
25 python FMjoin.py origindir destdir
26 '''
27
28 import os
29 import shutil
30 import sys
31
32 def main(*args):
33         if len(sys.argv) < 2:
34                 print 'You must at least specify origin and destination folders.'
35                 return 0
36         origin=sys.argv[1]
37         dest=sys.argv[2]
38    
39         if os.path.exists(origin):
40                 if os.path.exists(dest):
41                         if os.listdir(dest)!=[]:
42                                 print 'Destination folder is not empty! Use another folder.'
43                                 return 0
44                 else:
45                         print 'Destination folder does not exist, will create it'
46                         os.mkdir(dest)
47         else:
48                 print 'You provided a wrong origin folder name, try again.'
49         
50         origin=os.path.abspath(origin)
51         dest=os.path.abspath(dest)
52         
53         for root, dirs, files in os.walk(origin):
54                 for filename in files:
55                         if filename.split('.')[1]!="ibw":
56                                 continue
57                         filepath=os.path.join(root,filename)
58                         #to avoid overwriting, we collapse unique paths into filenames
59                         rawdest=filepath.split(os.path.commonprefix([origin, filepath]))[1]
60                         rawdest=rawdest.replace('/','') #for linux
61                         rawdest=rawdest.replace('\\','') #for windows
62                         destfile=os.path.join(dest,rawdest)
63                         print 'Copying '+rawdest
64                         shutil.copy(filepath,destfile)
65     
66         return 0
67  
68 if __name__ == '__main__':
69     sys.exit(main(*sys.argv))
70
71