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.In reply to File::Find and replacing spaces in filenames. by Kyshtynbai
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |