1#! 2#[[BEGIN PROPERTIES]] 3# Type = Command 4# Order = 6.0 5# Interpreter = perl 6# Caption = Exchange 7# Descr =Exchange the source for the target. 8# Descr = 9# Descr =Selection details: 10# Descr = 11# Descr = Source: The source file or directory that shall get the path name of the 12# Descr = target. 13# Descr = 14# Descr = Target: The target file or directory that shall get the path name of the 15# Descr = source. 16# Descr = 17# Descr =NOTE: At first, the target is moved to a temporary name in the form 18# Descr ="<name>.ex-tmp-<number>". Then the source path is moved to the target 19# Descr =path, and finally the temporary path is moved to the source path. If one 20# Descr =of the moves fails, it is tried to restore the original state. But that 21# Descr =does not work in all cases. You may have to repair manually then. 22# Icon = exchange_files.tga 23# Hotkey = Ctrl+E 24#[[END PROPERTIES]] 25 26use strict; 27use warnings; 28BEGIN { require "$ENV{'EM_DIR'}/res/emFileMan/scripts/cmd-util.pl"; } 29 30if (IsFirstPass()) { 31 32 ErrorIfNotSingleSource(); 33 ErrorIfNotSingleTarget(); 34 ErrorIfRootSources(); 35 ErrorIfRootTargets(); 36 37 my $message= 38 "Are you sure to exchange\n". 39 "\n". 40 GetSrcListing(). 41 "\n". 42 "for\n\n". 43 GetTgtListing() 44 ; 45 Confirm("Exchange",$message); 46 47 SecondPassInTerminal("Exchange"); 48} 49 50my @srcs=GetSrc(); 51my @tgts=GetTgt(); 52my $src=$srcs[0]; 53my $tgt=$tgts[0]; 54 55my $tmp; 56for (my $i=0; ; $i++) { 57 $tmp=$tgt.".ex-tmp-$i"; 58 if (!-e $tmp) { 59 last; 60 } 61} 62 63sub Move 64{ 65 return TermRun( 66 "mv", 67 ($Config{'osname'} eq 'linux' or $Config{'osname'} eq 'cygwin') ? ( 68 "-vf" 69 ) 70 : ( 71 "-f" 72 ), 73 "--", 74 $_[0], 75 $_[1] 76 ); 77} 78 79my $e=Move($tgt,$tmp); 80if ($e==0) { 81 $e=Move($src,$tgt); 82 if ($e==0) { 83 $e=Move($tmp,$src); 84 if ($e!=0) { 85 if (!-e $src) { 86 if (Move($tgt,$src)==0) { 87 if (!-e $tgt) { 88 Move($tmp,$tgt); 89 } 90 } 91 } 92 } 93 } 94 else { 95 if (!-e $tgt) { 96 Move($tmp,$tgt); 97 } 98 } 99} 100 101TermSync(); 102 103if ($e==0) { 104 SendSelect($src); 105} 106else { 107 SendUpdate(); 108} 109 110TermEnd($e); 111