[CLUE-Tech] removing spaces from filenames?
Timothy C. Klein
teece at silverklein.net
Sun Nov 17 16:18:11 MST 2002
* Dave Price (davep at kinaole.org) wrote:
> Hi,
>
> I have a _bunch_ of files that were created on a windoze system with
> embedded spaces in their names. Can anyone recommend a technique to
> programmatically rename the files - replacing ' ' with '_' on both the
> file and dir names?
>
> I am thinking perl, but I am not sure what sysntax I would need to use
> to search and rename.
>
> TIA
I keep the following little script around to rename stuff. Could
probably be done in one line, but I was learning perl when I first wrote
it, so I did it fully bloated.
Just change the variables $glob_pat, $out_pat, and $replace_pat at the
top to make it do different stuff. I changed it for space-to-underscore
conversion, but I haven't tried it. You probably want to test it on
some temp stuff before you risk erasing a bunch of files :-)
Tim
#!/usr/bin/perl
use strict;
use warnings;
# Change these to set up the desired replacement
my $glob_pat = "*";
my $out_pat = " ";
my $replace_pat = "_";
foreach my $file (glob "$glob_pat") {
my $newfile = $file;
$newfile =~ s/$out_pat/$replace_pat/;
if (-e $newfile) {
warn "Can't rename $file to $newfile: $newfile exists.\n";
} elsif (rename $file, $newfile) {
## Sucess, the work is already done.
} else {
warn "Rename of $file to $newfile failed: $!\n";
}
}
--
==============================================
== Timothy Klein || teece at silverklein.net ==
== ---------------------------------------- ==
== "Hello, World" 17 Errors, 31 Warnings... ==
==============================================
More information about the clue-tech
mailing list