Kyshtynbai has asked for the wisdom of the Perl Monks concerning the following question:
Hi everyone!
I have to process a lot of filenames and directory names which may contain spaces: I want to change spaces with, lets say, _ symbol. I've googled for a solution; and I've found a rather ugly Bash string which however works fine.
I've decided to write a much more good-looking Perl script which would do the same thing. But the only code I was able to compose is the following code:
#!/usr/bin/perl use strict; use warnings; use File::Find; my $dir = "/home/ivan/M1"; find(\&wanted, $dir); sub wanted { my $file = $_; # storing unprocessed filename $_ =~ s/ /_/g; # replacing spaces with _ rename "$File::Find::dir/$file", "$File::Find::dir/$_"; # renaming + files }
I've run it; first time I've run it, it returned an error like this:
Can't cd to (/home/ivan/M1/) Some Dirname With Spaces: no such file or + directory at ./1.pl line 16
But it had processed all the filenames and changed spaces with _. I've run the script second time and it didn't return any mistake and all of my filenames and dirnames were processed.
My question is: what have I done wrong and how to improve this script? I'm a newbe in Perl and, to tell the truth, in Unix, so please don't blame me too hard :) . Thank you in advance.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: File::Find and replacing spaces in filenames.
by Kenosis (Priest) on Dec 22, 2012 at 17:04 UTC | |
by Kyshtynbai (Sexton) on Dec 22, 2012 at 17:36 UTC | |
by 7stud (Deacon) on Dec 22, 2012 at 19:06 UTC | |
by Kyshtynbai (Sexton) on Dec 22, 2012 at 20:03 UTC | |
by 7stud (Deacon) on Dec 23, 2012 at 03:29 UTC | |
Re: File::Find and replacing spaces in filenames.
by Anonymous Monk on Dec 22, 2012 at 19:11 UTC | |
by Kyshtynbai (Sexton) on Dec 22, 2012 at 19:59 UTC | |
by AnomalousMonk (Archbishop) on Dec 22, 2012 at 22:48 UTC | |
Re: File::Find and replacing spaces in filenames.
by mimosinnet (Beadle) on May 17, 2017 at 21:55 UTC |
Back to
Seekers of Perl Wisdom