On Sat, Jan 22, 2011 at 2:56 PM, Shlomi Fish <[email protected]> wrote:
> Hi Raphael,
>
> On Saturday 22 Jan 2011 11:03:28 raphael() wrote:
> > Hello,
> >
> > qmv.exe (from renameutils) lets you edit multiple filenames in a text
> > editor.
> > I cannot get it to work in cygwin 1.7x (gives fork + jump error ??). So I
> > tried to write it in Perl.
> >
> > qmv's format is simple
> >
> > filename-1 <empty space> filename_1_to_be_edited
> > filename 2 <empty space> filename_2_to_be_edited
> > filename_3 <empty space> filename_3_to_be_edited
> >
> > You edit the names and then it renames the files you have changed.
> Problem
> > I am having is how can I split the two file names by space
> > since file name itself might also have spaces in them.
> >
> > I tried something like this
> >
> > ( $OldFileName, $NewFileName ) = split /\s{5,}/;
> >
>
> 1. You should use my here so the variables will be declared the closest to
> their usage.
>
> 2. You shouldn't use "$_".
>
> 3. Please avoid CamelCase names.
>
> > It works but not always.
>
> When doesn't it work? Can you give a test case?
>
> Regards,
>
> Shlomi Fish
>
> > I also thought to use and alternate anchor like pipe or comma but it
> looks
> > ugly.
> > Beside qmv.exe uses space to separate file names Any help would be
> > appreciated. Thanks.
>
> --
> -----------------------------------------------------------------
> Shlomi Fish http://www.shlomifish.org/
> "The Human Hacking Field Guide" - http://shlom.in/hhfg
>
> Chuck Norris can make the statement "This statement is false" a true one.
>
> Please reply to list if it's a mailing list post - http://shlom.in/reply .
>
Thanks for the reply. CamelCase Names ~ my first time as I saw few people
using it and thought what was the attraction! I am usually _underscore_ guy.
-- CODE --
use strict;
use warnings;
use File::Temp;
use Sort::Naturally;
use Getopt::Std 'getopts';
my ( %opts );
getopts('vfephc:', \%opts);
&usage if $opts{h};
my ( %hash, %files );
my $tmp = File::Temp->new();
my $filename = $tmp->filename();
my @selected_files = $opts{c} ? glob "$opts{c}" : glob "*";
if ( $opts{f} ) { @selected_files = grep { -f $_ } @selected_files }
%files = map { $_ => $_ } @selected_files ;
for my $file ( nsort keys %files ) {
printf { $tmp } "%s %65s\n", $file, $files{$file}; # THIS CAUSES
UNINITIALIZED ERROR IF FILE NAME IS VERY LONG . THE REASON FOR PREVIOUS
QUESTION
} # HOW DO I SEPARATE TWO FILE NAMES?
my $editor = $opts{e} // 'C:\cygwin\bin\vim-nox.exe';
system( "$editor", $filename );
open my $FH, '<', $filename or exit $!;
my @names = <$FH>;
close( $FH );
for ( @names ) {
chomp; # GIVES WARNINGS IF I RUN THE COMMAND UT DON"T EDIT ANYTHING
my ( $OldFileName, $NewFileName ) = split /\s{5,}/; # WAS DECLARED
DIDN'T COPY IT IN MAIL
$hash{$OldFileName} = $NewFileName; # DON'T USE CAMEL CASE
}
for my $key ( keys %files ) {
next if ( $files{$key} eq $hash{$key} );
# skip rename if file with edited name exists
unless ( -e $hash{$key} ) {
if ( $opts{p} or $opts{v} ) {
print "[$key] => [$hash{$key}]\n";
next if $opts{p} ;
}
rename( $key, $hash{$key} );
}
}
sub usage
{
print STDERR << "EOF";
usage: perl qmv.pl -e -p -v -c'*.extension'
-c selection custom files like -c'*.rar'
-e editor
-f files only (default is files and folders)
-h help (this message!)
-p print-only do not rename
-u undo all changes (not implemented yet)
-v verbose similar to -p but renames files
EOF
exit 0;
}
-- CODE END --
See comments for question. Its a mess of code. I know.