![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Aug 2005
Posts: 137
Rep Power: 3
![]() |
Is it possible to glob files with no extensions?
Hi,
I'm trying to make a program that will change all files with a specified file extension to the new specified file extension. A user would call the program in this manner: program_name extension_to_find extension_to_change_to They can also just pass one file extension on the command line and the program will assume that the user wants all files with no extensions to have the specified extension added. The question is, how do I glob for files with no extensions? The following is my code so far. #!/usr/bin/perl
use warnings;
use strict;
if (scalar(@ARGV) == 1)
{
my $newext = shift(@ARGV);
chomp($newext);
my @files = glob("*[^\.]"); #trying to get all files with no extensions
#glob() does not seem to understand regex
print "@files\n"; #checking to see if any files matched
foreach my $file (@files)
{
rename("$file", "$file.$newext");
}
}
elsif (scalar(@ARGV) == 2)
{
my $newext = pop(@ARGV);
my $oldext = pop(@ARGV);
chomp($newext);
my @files = glob("*.$oldext");
foreach my $file (@files)
{
$file =~ m/^(\w*)\.$oldext$/;
rename("$file", "$1.$newext");
}
}
else
{
print "Usage: 6.pl old_extension new_extension\n";
print "Optionally: 6.pl extension_to_add\n";
} |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: New Mexico
Posts: 228
Rep Power: 4
![]() |
glob doesn't like RE.
you'll have to glob("*") and then match on the result set. |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: Aug 2005
Posts: 137
Rep Power: 3
![]() |
Ok... thanks for the advice.
![]() |
|
|
|
|
|
#4 |
|
Professional Programmer
Join Date: Mar 2005
Location: Glasgow, Scotland
Posts: 314
Rep Power: 4
![]() |
Your regexp is incorrect; you can't start with a quantifier. A regexp to test a filename to see if it contains periods (which you'll need if you're filtering the output of glob for files with no extensions) looks like:
^[^\.]+$ This matches any string that doesn't contain a period anywhere in it, which is probably good enough. One hitch - globbing in Perl returns a list of full paths, doesn't it? I don't quite remember but the Tcl glob command does. This means the above regexp is really too simple since any periods in the path will cause the file to be rejected. This should work: ^(?:[^/]*/)*([^\.]+)$ I can't remember if non-capturing parens work in Perl regexps; if not, you don't want the ?: right after the first open paren and the filename is captured by submatch 2, not submatch 1 as it is in the version above, which works as a Tcl ARE. You may also need to backslash-escape the forward slashes if you're delimiting your regexp with forward slashes in Perl. What this expression matches is zero or more sequences of zero or more non-slashes followed by a slash, followed by any sequence of at least one character that doesn't contain a period leading up to the end of the string. One possible problem is that glob may add a trailing slash if the match it's returning is itself a directory, in which case the first part of the above re will consume the name of the file, too, and you'd need this regexp to actually work: ^(?:[^/]*/)*?([^\.]+)/?$ Notice the nongreedy version of * for the quantifier of the first part and the /? at the end (optional slash, not captured as part of the filename). You could then test, if you care, whether the filename is a directory using -d. Actually, come to think of it, it would be more useful if you could capture the whole path, not just the filename, in your regexp; this revised one should do it: ^((?:[^/]*/)*?([^\.]+))/?$ This captures everything but the trailing slash, if one is present, in submatch 1 and just the filename in submatch 2 (submatch 3 if you have to remove the ?: that makes the second set of parens noncapturing). Sorry to ramble on. I love regexps! As usual I haven't tested any of this so hopefully it's not a brainfart. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|