;+ ; NAME: ; filemove ; PURPOSE: ; Move one file to a new place or name. ; DESCRIPTION: ; ; This routine is meant to provide a common capability of moving files ; regardless of the operating system. The full suite of possibilities ; that each OS can support is _not_ supported on all platforms. In fact, ; there is not even a full range of parameter validation to ensure the ; use of universal features. Instead, you can get this to work on all ; platforms only if you keep it simple. ; ; Note that this routine does NOT check to see if the destination already ; exists. That means this routine will quietly overwrite preexisting files. ; ; Here are some examples of things that should work: ; ; filemove,'file1.dat','file1move.dat' ; Move file1.dat to file1move.dat in the current directory. ; ; filemove,'file1.dat','other' ; If other is a directory, this will move file1.dat to that directory ; If other is a file or does not exist, then other will be replace by ; file1.dat upon completion. ; ; filemove,'*.dat','other' ; Move all files ending in .dat to the directory "other". If other ; is not a directory or does not exist, this will fail. ; ; If you use directory separators (eg., / or \ or : ), you will not be able ; to move that call of filemove to another platform. ; ; Note that the oldfile/newfile inputs can also be string arrays but they ; must match in length. The lists are matched, one-by-one as if filemove ; were being called on each string array element pair in turn. ; ; CATEGORY: ; Utility ; ; CALLING SEQUENCE: ; filemove,oldfile,newfile ; ; INPUTS: ; oldfile - Filename of file that exists ; newfile - Name of file to move to. ; ; OPTIONAL INPUT PARAMETERS: ; ; KEYWORD INPUT PARAMETERS: ; SILENT - If set, suppresses all chatty output to the screen. ; ; OUTPUTS: ; ; KEYWORD OUTPUT PARAMETERS: ; ; COMMON BLOCKS: ; ; SIDE EFFECTS: ; ; RESTRICTIONS: ; MacOs only: ; Unable to move a file at the top level of a disk partition ; ; PROCEDURE: ; ; MODIFICATION HISTORY: ; Written by Marc W. Buie, Lowell Observatory, and Dave Osip, MIT, 2000/03/14 ; verified on MacOS 9 to work for all cases indicated above 2000/03/19 ; 2000/05/08, MWB, fixed minor flaw in Win/DOS operation. Changed from using ; rename to move. ;- pro filemove,oldfile,newfile,SILENT=silent if badpar(oldfile,7,[0,1],CALLER='FILEMOVE (oldfile) ',npts=nold) then return if badpar(newfile,7,[0,1],CALLER='FILEMOVE (newfile) ',npts=nnew) then return if badpar(silent,[0,1,2,3],0,CALLER='FILEMOVE (SILENT) ',default=0) then return if nold ne nnew then begin print,'FILEMOVE: Error! oldfile and newfile must have the same length.' return endif for i=0,nold-1 do begin junk=findfile(oldfile[i],count=count) if count ne 0 then begin if !version.os_family eq 'Windows' then begin spawn,'MOVE /Y '+oldfile[i]+' '+newfile[i] if not silent then $ print,'move /y '+oldfile[i]+' '+newfile[i] endif else if !version.os_family eq 'MacOS' then begin for im=0,count-1 do begin cd,current=pathd file = junk[im] & file2=newfile[i] if rstrpos(file,":") eq -1 then file=findfile(pathd+file) length=strlen(file) findsep=rstrpos(file,":") file1name=strmid(file,findsep+1,length) path1name=strmid(file,0,findsep) testfile2=findfile(pathd+file2, count=tcount) if tcount ne 0 then file2=testfile2 else file2=pathd+file2 length=strlen(file2) findsep=rstrpos(file2,":") file2new=strmid(file2,findsep+1,length) if max(strlen(file2new)) eq 0 then file2new=file1name path2new=strmid(file2,0,findsep)+":" pathd2=path2new file2=file2new ; define applescript to move a file scriptmv=['tell current application to copy variable "file" to file2cp',$ 'set filein to characters 1 thru -2 of file2cp',$ 'set filein to filein as string',$ 'tell current application to copy variable "file2" to newfile',$ 'set fileout to characters 1 thru -2 of newfile',$ 'set fileout to fileout as string',$ 'tell current application to copy variable "pathd2" to path2file2',$ 'set dirout to characters 1 thru -2 of path2file2',$ 'set dirout to dirout as string',$ 'set fdout to dirout&fileout as string',$ 'property destination : ""',$ 'tell application "Finder"',$ 'set destination to folder dirout',$ 'set in_file to file filein',$ 'try',$ 'if file fdout exists then',$ 'delete file fdout',$ 'end if',$ 'set new_file to move in_file to destination with replacing',$ 'if new_file exists then',$ 'set name of new_file to fileout',$ 'else',$ 'display dialog "PROBLEM: File " & in_file & " was not moved!"',$ 'end if',$ 'on error',$ 'display dialog "Could not move " & in_file',$ 'end try',$ 'end tell'] do_apple_script, scriptmv if not silent then print, 'moved '+file+' to '+pathd2+file2 endfor endif else begin spawn,'"mv" '+oldfile[i]+' '+newfile[i] if not silent then $ print,'"mv" '+oldfile[i]+' '+newfile[i] endelse endif else begin if not silent then $ print,'FILEMOVE: Error! Oldfile ',oldfile[i],' not found, skipped.' endelse endfor end