Help - Search - Members - Calendar
Full Version: replacing spaces in filenames?
The Planet Forums > System Administration > General Support Questions
cumulus
Hello
I have a client that has uploaded over 100 pdf files that have spaces in their names (This example.pdf). Needless to say, they don't display properly.

Does anyone have a suggestion how I can search and replace the spaces in the filenames with an underscore?

Tried sed, but it started to search the document contents, not the filename.

Thanks in advance,
Cumulus
Tex
might check this out and see if you can use or modify to suit.


http://www.xscd.com/pub/mvb/
projo
This may help: You can refer to names with spaces in them by placing the name in quotes.

mv "te m.doc" tem.doc

removes the space. Maybe you can use php and its ability to execute shell commands, or other.
smoker
This is a bit of a hack, but it works.
to use it, copy the code below to a file on your server ie. space_eater.pl

Edit the $start_folder line to reflect the correct directory on your server.

Then chmod the file 755

from a command line type

CODE
perl ./space_eater.pl

( when you are in the scripts directory )


CODE
#!/usr/bin/perl

use Shell qw(ls mv);



################################################################





$start_folder = "/path/to/directory/containing/filenames/with/spaces";  ### < < < edit this line ###



################################################################



$fillgap = "_";         ### this is the replacement character

$count=0;  ### zero the counter

chdir $start_folder;    ### like it says ...



opendir This, ".";

                                                                               

       @isafile = ls;    ### collect the filenames for processing

                                                                               

closedir This;



       if ($#isafile > "0") {                          ### if the listing found any files then ...

                       for $element (@isafile) {

                       chomp $element;                         ### get rid of newline chars

                       chomp $element;

                       $nospace = $element;            ### reassign the filename for processing

                       $element =~ s/ /*/;  ### substitute the space in the name for a wildcard

                           ### so that the mv command works correctly



                                                       

                       $nospace =~ s/ /$fillgap/;      ### This section substitutes a space in the filename

                       $nospace =~ s/ /$fillgap/;      ### for a _ (underscore)

                       $nospace =~ s/ /$fillgap/;      ### done 3 times coz it only does one space per pass

                           ### and there may be more than one space

                           

                       mv ($element, $nospace);    ### rename the file

                       

                       print "$element renamed to $nospacen"; ### report to the user

                       

     $count +=1;      ### increment counter

     }

    }else{

     print "No files present";

     exit;

    }

     

     print "$count filenames have had spaces replaced by underscores. n";

     print "Goodbye n";

     exit;


alan

[edit] Yeah, I know perldork can do much better, if fact I'd like to compare his solution to mine so I can learn ;-)
First things I can see wrong, are
1) no checks to see if the filename needs processing
2) if ($#isafile > "0"){ } should be if @isafile{ } so as to catch a lone file
3) s// should be in a loop dynamically limited by number of spaces found in each name
4) I should have used strict and perl -w

5) Any others ?

[/edit]
amusive.com
Fixed it up for you.

[php]
#!/usr/bin/perl
use Shell qw(ls mv);

# Change this

$start_folder = "/path/to/directory/containing/filenames/with/spaces"; ### < < < edit this line ###

# Everything else should be OK.

$fillgap = "_"; ### this is the replacement character
$count=0; ### zero the counter
chdir $start_folder; ### like it says ...

opendir This, ".";

@isafile = ls; ### collect the filenames for processing
$total = @isafile;

closedir This;


foreach $file (@isafile) {
chomp $file; ### get rid of newline chars
chomp $file;
$origfile = $file;
if ($file =~ s/ /$fillgap/g) {
mv (""" . $origfile . """, $file); ### rename the file
print "$origfile renamed to $filen"; ### report to the user
$count +=1; ### increment counter
}
}

print "$count filenames out of $total have had spaces replaced by underscores. n";
print "Goodbye n";
[/php]
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2010 Invision Power Services, Inc.