—#!perl
use
strict;
use
warnings;
unless
(
@ARGV
>= 2) {
die
"rename-swap: Please specify at least 2 files\n"
;
}
# make sure all files exist first
for
(
@ARGV
) {
die
"rename-swap: File '$_' doesn't exist\n"
unless
-e
$_
;
}
# create a temporary name that does not exist
my
$tmp
;
while
(1) {
$tmp
=
sprintf
"rename-swap-tmp-%d"
,
rand
()*1_000_000;
last
unless
-e
$tmp
;
}
# start the swapping!
for
(
my
$i
=0;
$i
<
int
(
@ARGV
/2);
$i
++) {
my
$f1
=
$ARGV
[
$i
];
my
$f2
=
$ARGV
[
$#ARGV
-
$i
];
if
(
$f1
eq
$f2
) {
"Skipping swapping $f1 -> $f2 (same file)\n"
;
next
;
}
rename
$f1
,
$tmp
or
die
"rename-swap: Can't rename $f1 -> $tmp: $!\n"
;
rename
$f2
,
$f1
or
die
"rename-swap: Can't rename $f2 -> $f1: $!\n"
;
rename
$tmp
,
$f2
or
die
"rename-swap: Can't rename $tmp -> $f2: $!\n"
;
"Swapping $f1 <-> $f2\n"
;
}
# PODNAME: rename-swap
# ABSTRACT: Swap filenames
__END__
=pod
=encoding UTF-8
=head1 NAME
rename-swap - Swap filenames
=head1 VERSION
This document describes version 0.014 of rename-swap (from Perl distribution App-FileRenameUtils), released on 2023-11-20.
=head1 SYNOPSIS
Swapping two files:
% rename-swap f1 f2
f1 will be renamed to f2 while f2 will be renamed to f1.
Swapping three files:
% rename-swap f1 f2 f3
f1 will be renamed to f3, f3 will be renamed to f1, while f2 will be unchanged.
Swapping four files:
% rename-swap f1 f2 f3 f4
f1 will be renamed to f4, f4 will be renamed to f1, f2 will be renamed to f3, f3
will be renamed to f2.
=head1 DESCRIPTION
WARNING: Be careful when running this script. This script does not support
dry-run (simulation) mode nor undo. It uses C<rename()> so it does not support
moving files across filesystems; it's best to specify only files in the same
directory.
=head1 HOMEPAGE
Please visit the project's homepage at L<https://metacpan.org/release/App-FileRenameUtils>.
=head1 SOURCE
Source repository is at L<https://github.com/perlancar/perl-App-FileRenameUtils>.
=head1 AUTHOR
perlancar <perlancar@cpan.org>
=head1 CONTRIBUTING
To contribute, you can send patches by email/via RT, or send pull requests on
GitHub.
Most of the time, you don't need to build the distribution yourself. You can
simply modify the code, then test via:
% prove -l
If you want to build the distribution (e.g. to try to install it locally on your
system), you can install L<Dist::Zilla>,
L<Dist::Zilla::PluginBundle::Author::PERLANCAR>,
L<Pod::Weaver::PluginBundle::Author::PERLANCAR>, and sometimes one or two other
Dist::Zilla- and/or Pod::Weaver plugins. Any additional steps required beyond
that are considered a bug and can be reported to me.
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2023, 2020, 2019 by perlancar <perlancar@cpan.org>.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=head1 BUGS
Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=App-FileRenameUtils>
When submitting a bug or request, please include a test-file or a
patch to an existing test-file that illustrates the bug or desired
feature.
=cut