Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 25th, 2005, 9:57 PM   #1
aznluvsmc
Hobbyist Programmer
 
Join Date: Aug 2005
Posts: 137
Rep Power: 3 aznluvsmc is on a distinguished road
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";
}
aznluvsmc is offline   Reply With Quote
Old Oct 26th, 2005, 10:03 AM   #2
jim mcnamara
Hobbyist Programmer
 
Join Date: Jun 2005
Location: New Mexico
Posts: 228
Rep Power: 4 jim mcnamara is on a distinguished road
glob doesn't like RE.

you'll have to glob("*") and then match on the result set.
jim mcnamara is offline   Reply With Quote
Old Oct 26th, 2005, 6:18 PM   #3
aznluvsmc
Hobbyist Programmer
 
Join Date: Aug 2005
Posts: 137
Rep Power: 3 aznluvsmc is on a distinguished road
Ok... thanks for the advice.
aznluvsmc is offline   Reply With Quote
Old Jan 25th, 2006, 4:38 AM   #4
mackenga
Professional Programmer
 
Join Date: Mar 2005
Location: Glasgow, Scotland
Posts: 314
Rep Power: 4 mackenga is on a distinguished road
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.
mackenga is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:40 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC