1#!
2#[[BEGIN PROPERTIES]]
3# Type = Command
4# Order = 9.0
5# Interpreter = perl
6# Caption = Rename
7# Descr =Rename one or more files or directories. The new name is
8# Descr =asked. When renaming multiple entries, you can change only
9# Descr =the identical parts at the beginning and end of the names.
10# Descr =
11# Descr =Selection details:
12# Descr =
13# Descr =  Source: Ignored.
14# Descr =
15# Descr =  Target: The files and directories to be renamed.
16# Icon = rename_file.tga
17# Hotkey = Backspace
18#[[END PROPERTIES]]
19
20use strict;
21use warnings;
22BEGIN { require "$ENV{'EM_DIR'}/res/emFileMan/scripts/cmd-util.pl"; }
23
24if (IsFirstPass()) {
25
26	ErrorIfNoTargets();
27	ErrorIfRootTargets();
28	ConfirmIfTargetsAcrossDirs();
29
30	my @tgt=GetTgt();
31
32	if (@tgt==1) {
33		my $oldPath=$tgt[0];
34		my ($oldName,$dir)=fileparse($oldPath);
35		my $newName=FilenameEdit(
36			"Rename",
37			"Please enter a new name for:\n\n$oldPath",
38			$oldName
39		);
40		if ($oldName eq $newName) { exit(0); }
41		if (-e catfile($dir,$newName)) {
42			Error("A file or directory with that name already exists.");
43		}
44		SetFirstPassResult($newName);
45		SecondPassInTerminal("Rename");
46	}
47	else {
48		my ($n,$d)=fileparse($tgt[0]);
49		my $p=$n;
50		my $q=$n;
51		my $pl=length($p);
52		my $ql=length($q);
53		my $ml=length($n);
54		for (my $i=1; $i<@tgt; $i++) {
55			($n,$d)=fileparse($tgt[$i]);
56			my $nl=length($n);
57			if ($ml>$nl) { $ml=$nl; }
58			while ($pl>0 && ($nl<$pl || substr($n,0,$pl) ne $p)) {
59				$pl--;
60				$p=substr($p,0,$pl);
61			}
62			while ($ql>0 && ($nl<$ql || substr($n,$nl-$ql,$ql) ne $q)) {
63				$ql--;
64				$q=substr($q,1,$ql);
65			}
66		}
67		while ($pl+$ql>$ml) { $ql--; $q=substr($q,1,$ql); }
68		my $res=Edit(
69			"Multi-Rename",
70			"Please enter the pattern for the new names of multiple\n".
71			"entries. The asterisk (\"*\") stands for the unequal part\n".
72			"in the names - do not remove it.\n",
73			$p.'*'.$q
74		);
75		my $i=index($res,'*');
76		if ($i<0) { Error("You removed the asterisk from the pattern."); }
77		if (index($res,'*',$i+1)>0) { Error("Too many asterisks in the pattern."); }
78		my $rp=substr($res,0,$i);
79		my $rq=substr($res,$i+1);
80		for (my $i=0; $i<@tgt; $i++) {
81			($n,$d)=fileparse($tgt[$i]);
82			$n=$rp.substr($n,$pl,length($n)-$pl-$ql).$rq;
83			CheckFilename($n);
84			if (-e catfile($d,$n)) {
85				Error("A file or directory named \"$n\" already exists.");
86			}
87		}
88		SetFirstPassResult($pl.'*'.$ql.'*'.$res);
89		SecondPassInTerminal("Multi-Rename");
90	}
91}
92
93my @tgt=GetTgt();
94
95if (@tgt==1) {
96	my $oldPath=$tgt[0];
97	my ($oldName,$dir)=fileparse($oldPath);
98	my $newName=GetFirstPassResult();
99	my $newPath=catfile($dir,$newName);
100	my $e=TermRunAndSync(
101		"mv",
102		($Config{'osname'} eq 'linux' or $Config{'osname'} eq 'cygwin') ? (
103			"-v"
104		)
105		: (
106		),
107		"--",
108		$oldPath,
109		$newPath
110	);
111	if (-e $newPath) {
112		SendSelectKS($newPath);
113	}
114	else {
115		SendUpdate();
116	}
117	TermEnd($e);
118}
119else {
120	my @fpr=split(/\*/,GetFirstPassResult().'*x');
121	my $pl=$fpr[0];
122	my $ql=$fpr[1];
123	my $rp=$fpr[2];
124	my $rq=$fpr[3];
125	my @newTgt=();
126	my $e=0;
127	for (my $i=0; $i<@tgt; $i++) {
128		my ($n,$d)=fileparse($tgt[$i]);
129		$n=$rp.substr($n,$pl,length($n)-$pl-$ql).$rq;
130		$n=catfile($d,$n);
131		push(@newTgt,$n);
132		my $e=TermRun(
133			"mv",
134			($Config{'osname'} eq 'linux' or $Config{'osname'} eq 'cygwin') ? (
135				"-v"
136			)
137			: (
138			),
139			"--",
140			$tgt[$i],
141			$n
142		);
143		if ($e) { last; }
144	}
145	$e|=TermSync();
146	if (@newTgt > 0) {
147		SendSelectKS(@newTgt);
148	}
149	else {
150		SendUpdate();
151	}
152	TermEnd($e);
153}
154