View Full Version : Replacing Spaces in filenames
Kevin 101
03-23-2005, 11:46 AM
hey
I've got a script that sits inside all of my directories...all it does is produce a simple index web page with a list of all of the files inside that directory that have certain file extensions with links to them.
Problem is there are a number of people that add files to these directories and they are constantly leaving spaces in the filenames and thus the links don't work.
Is there anyway of having my script (or a seperate one) that goes through the files first, checks to see if any have gaps in the file names and replaces the gaps with underscores.
I presume i need something like:
$dir =~ s/\ /_/g;
but can't for the life of me get it to work.
Any ideas??
Ta
101
hinokata
03-23-2005, 12:37 PM
Do you actually want the file renamed or just displayed so that it has an underline?
Kevin 101
03-23-2005, 12:40 PM
Thanks, I need to rename the file itself, just to put in an underscore if it has a space anywhere in the filename.
Ta
hinokata
03-23-2005, 01:05 PM
This is the quickest way I can think off.
#!/usr/bin/perl -w
use File::Path;
chdir( "c:\\pathToDir" );
@files = glob( "c:\\pathToDir\*" );
foreach $files (@files)
{
$old = $files;
$files =~ s/\s/_/g;
rename( $old, $files);
}
Kevin 101
03-23-2005, 02:39 PM
Nice one thanks
I keep getting an error saying:
Global symbol "@files" requires explicit package name
Do I need to tell it what is in @files beforehand?
my @files = (pathtodir);
Thanks
hinokata
03-23-2005, 05:30 PM
Without seeing your code I would say it's one of two things:
1. You forgot to import File::Path
2. You threw your scoping off
One thing you should do is run it with -w as well and add "use strict;" and post the all of the errors;
Post your code(or relevant parts) and I'll see what I can do for you.
Kevin 101
03-24-2005, 10:42 AM
thanks
This is the original code, all it does is create an index poage with a list of links to all of the files in that directory.
<code>
#!/usr/bin/perl -w
use strict;
print "Content-type: text/html\n\n";
my $upload_dir = '../../GSS';
my @ext = ('sxc', 'sxw', 'pdf');
my @i = ();
$new_filename = $i =~ s/\s/_/g;
print "<h2><center><b>Welcome to GSS!!</b></center></h2><br><br>\n";
print "<b>HR Reference</b>\n";
opendir(DIR, $upload_dir) or die print "Fail to open dir: $!\n";
while (defined(my $dir = readdir(DIR))) {
for(my $i = 0; $i <= $#ext; $i++) {
if($dir =~ /\.$ext[$i]$/) {
print "<table><tr><td bgcolor=#e8e8e8><a href=../$dir>$dir</a></td></tr></table><br><br>\n";
}
}
}
</code>
What I want it to do is to search through the directory, check that none of the filenames have spaces in them and if they have to replace spaces with an underscore.
This is what I have from what you sent me earlier:
<code>
use File::Path;
chdir( "../GSS" );
@files = glob( "../GSS/*" );
foreach $files (@files)
{
$old = $files;
$files =~ s/\s/_/g;
rename( $old, $files);
}
</code>
Thanks
101
Kevin 101
03-24-2005, 10:52 AM
sorry that last bit should be:
<code>
use File::Path;
chdir( "../../GSS" );
@files = glob( "../../GSS/*" );
foreach $files (@files)
{
$old = $files;
$files =~ s/\s/_/g;
rename( $old, $files);
}
</code>
Thanks
I'm a numptie. This was meant to be a new thread
rwedge
03-25-2005, 05:01 AM
Untested, but this should list and handles spaces. A slight tweak of the posted code. Can you check and rename the file as it is being uploaded?
#!/usr/bin/perl -w
use CGI::Carp qw(fatalsToBrowser);
use strict;
my $upload_dir = '../../GSS';
print "Content-type: text/html\n\n";
print qq~
<center><h2><b>Welcome to GSS!!</b></h2></center><br><br>
<b>HR Reference</b>
~;
if (-d $upload_dir) {
my ($dir);
opendir(DIR, $upload_dir) or die "Failed to open dir: $!\n";
while (defined($dir = readdir(DIR))) {
if($dir =~ /\.sxc|\.sxw|\.pdf$/i) {
&ck_name;
print qq~
<table><tr><td bgcolor="#e8e8e8"><a href="../$dir">$dir</a></td></tr></table><br><br>
~;
}
}
closedir DIR or die "Failed to Close $upload_dir: $!\n";
}
else { print "Sorry, $upload_dir could not be found<br><br>"; }
sub ck_name {
if ($dir =~ /\s/g) {
my $old = $dir;
$dir =~ s/\s/_/g;
rename($old, $dir);
}
}
/Bob
Kevin 101
04-07-2005, 03:02 PM
Thanks
I get:
Software error:
Global symbol "$dir" requires explicit package name at /webhome/hrweb_KMS/GSS/cgi-bin/GSS3.cgi line 27.
Global symbol "$dir" requires explicit package name at /webhome/hrweb_KMS/GSS/cgi-bin/GSS3.cgi line 28.
Global symbol "$dir" requires explicit package name at /webhome/hrweb_KMS/GSS/cgi-bin/GSS3.cgi line 29.
Global symbol "$dir" requires explicit package name at /webhome/hrweb_KMS/GSS/cgi-bin/GSS3.cgi line 30.
Execution of /webhome/hrweb_KMS/GSS/cgi-bin/GSS3.cgi aborted due to compilation errors.
For help, please send mail to this site's webmaster, giving this error message and the time and date of the error.
doesn't seem to like the 'sub ck_name' bit at the bottom.
I managed to sort this another way anyways, but thanks for your reply!
I would try with this at the upper part of the script:
my $dir= "";
and I don't know why this would be but I found it does the job for making subs work:
In the headers of your script, put:
&put_ck_name;
hth
Bazz
rwedge
04-11-2005, 04:21 AM
Sorry about the late reply Kevin, the error you get is from running the script in strict mode. It is not happy with how $dir was declared evidently. I did not get this error from Perl when I ran it.
- short reason for error - Under strict scalers must be declared. The usage of 'my'... ie my ($one, $two) or my $name = 'value' etc.
Maybe because I declared my ($dir); inside the if statement the sub may treat it as local causing the gloabal warning. It may also be because of the parenthesis and the expectation of a list.
To fix it declare my $dir globaly at the top of the script:#!/usr/bin/perl -w
use CGI::Carp qw(fatalsToBrowser);
use strict;
my $upload_dir = '../../GSS';
my $dir;
............../Bob
Jeff Mott
04-12-2005, 08:20 PM
Kevin 101, the code hinokata gave you earlier will do fine. All you need to do is declare @files with my.use File::Path;
chdir( "c:\\pathToDir" );
my @files = glob( "c:\\pathToDir\*" );
foreach $files (@files)
{
$old = $files;
$files =~ s/\s/_/g;
rename( $old, $files);
}
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.