SteveS832001 has asked for the wisdom of the Perl Monks concerning the following question:

I need a program/script that would check file names to see if they have change, and if they have changed change them back.

Like i have about 150 files with the ext. of .AUW The ext will change ever so often to .AU~ I need this program/script to run in the background and change the ext when ever it changes

2006-03-12 Retitled by Arunbear, as per Monastery guidelines
Original title: 'Help'

  • Comment on Monitoring file names for changes in extension

Replies are listed 'Best First'.
Re: Monitoring file names for changes in extension
by Tanktalus (Canon) on Mar 12, 2006 at 22:17 UTC
Re: Monitoring file names for changes in extension
by blue_cowdawg (Monsignor) on Mar 12, 2006 at 23:03 UTC
        Like i have about 150 files with the ext. of .AUW The ext will change ever so often to .AU~

    Like, ummm... what have you tried and failed?

    Consider this option:

    # # handwaving enabled opendir(MYDIR,"/path/to/where/I/want/to/monitor") or die $!; while(my $fname=readdir(DIR)){ next unless $fname =~ m@.*\~$/ | react here... |
    Use the opendir/readir combination to read the direcotry you are monitoring and do a pattern match on the tilde at the end of the file name and react accordingly.

    Now that I've shown you my code... show me yours!


    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
Re: Monitoring file names for changes in extension
by borisz (Canon) on Mar 12, 2006 at 23:01 UTC
    ok, here is a start: untested
    #!/usr/bin/perl my $dir = shift or die "need a directory name"; opendir my $fd, $dir or die $!; for ( readdir $fd ) { next unless /\.AU~$/; my $old = "$dir/$_"; next unless -f $old; ( my $new = $old ) =~ s/~$/W/; rename $old => $new unless -e $new; } closedir $fd; #thx fletch
    Boris
Re: Monitoring file names for changes in extension
by rafl (Friar) on Mar 12, 2006 at 23:20 UTC
    Depending on your platform I'd suggest Linux::Inotify2 or maybe Linux::Inotify, which allows you to monitor files and directories for changes and react on them without being forced to check everything yourself from time to time.
Re: Monitoring file names for changes in extension
by spiritway (Vicar) on Mar 13, 2006 at 02:49 UTC

    I'm not sure what sort of files have the extension of .AUW, but it sounds like the relevant program is renaming the file for some reason. One possibility is simply to create a backup in case of a crash or other problem. While it's certainly possible to rename these files, you might want to look further into why they're getting changed - or why the original file (with the.AUW extension) isn't present.

Re: Monitoring file names for changes in extension
by smokemachine (Hermit) on Mar 13, 2006 at 15:19 UTC
    perl -e 'rename $_, (/^(.*)\./g)[0].".AUW" while <./*.AU~>'