Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 27 Nov 2001 03:07:21 -0500
From:      Brian T.Schellenberger <bts@babbleon.org>
To:        Edwin Groothuis <edwin@mavetju.org>, jayyness@mindspring.com
Cc:        freebsd-questions@FreeBSD.ORG
Subject:   Re: Mass Renaming of Files
Message-ID:  <01112703072100.65893@i8k.babbleon.org>
In-Reply-To: <20011127164741.F579@k7.mavetju.org>
References:  <Springmail.105.1006808059.0.84387100@www.springmail.com> <20011127164741.F579@k7.mavetju.org>

index | next in thread | previous in thread | raw e-mail

[-- Attachment #1 --]
On Tuesday 27 November 2001 00:47, Edwin Groothuis wrote:
> On Mon, Nov 26, 2001 at 03:54:19PM -0500, jayyness@mindspring.com wrote:
> > Do any of you know of a script that could be written or possibly existst
> > that would search for and replace spaces with underscores?

I have a script called "unmsdos" that makes names "unix-friendly" (eliminates 
spaces as well as leading dashes, ampersands, parentheses, and other cruft.  
It also invokes my "uncrnl" program to undo CR-NL if it's a text file.  Enjoy.

BTW, is this worth cleaning up and turning into a port sometime?  Opinions 
welcome.

>
> What kind of rename/move are you talking about?
>
> There is, normally, no reason to worry, since you always can use
> "mv 'blaat blaat' /some/other/place" without having to worry about
> spaces.

Well, you can, but *what* a pain.  And when do you do things like piping an 
ls into xargs, it gets the names all wrong.  Spaces in file names are totally 
for the birds.

>
> If you have to do selections in directories regarding files, it
> might be easier to use Midnight Commander or something.
>
> Edwin

-- 
Brian T. Schellenberger . . . . . . .   bts@wnt.sas.com (work)
Brian, the man from Babble-On . . . .   bts@babbleon.org (personal)
                                        http://www.babbleon.org

-------> Free Dmitry Sklyarov!  (let him go home)  <-----------

http://www.eff.org                 http://www.programming-freedom.org 

[-- Attachment #2 --]
#! /usr/bin/perl

if ($ARGV[0] eq '-c')   # Special CD-ROM mode
{
    $cdrom = 1;
}

if ($ARGV[0] eq '-n') 
{
    shift;
    $putname = 1;
}

foreach (@ARGV)
{
    # unmsdos accpets a "quoting" convention of angle-brackets to
    # quote names.  If it has 'em, strip 'em.  If it has the leading,
    # but not the trailing, bracket then save this one and come 
    # around again.  First see if we are continued from before.

    if ($pending)
    {
        $_ = $pending . ' ' .$_;
        $pending = "";
    }

    if (/^</)
    {
        if (/>$/)
        {
            s/^<//;
            s/>$//;
        }
        else
        {
            $pending = $_;
            next;
        }
    }

    $path = $_;
    $path = "."  if (! /\//);
    $path =~ s|/[^/]*$||;
    s|^.*/||;
    $newname = $_;

    if ($cdrom)
    {
        next if (length > 12 || /[a-z]/);  # 12 = 8.3 (12345678.ABC)
        goto justLowercase;
    }

    # If it's one of my "rated" names, seperate out the rating part.
    # Always take out leading paths for file renames.

    $prefix = "";
    if (/^(.*\/)?(\w+-\w+-\w+:)?/)
    {
        $prefix = $&;
        $_ = $';
    }

    # name conversions.
    $newname =~ tr/\x80-\xFF/\x00-\x7F/;
    $newname =~ s/[\x00- ]/_/g;
    $newname =~ s/[[\]]/_/g;
    $newname =~ s/\&/+/g;
    $newname =~ s/\s/_/g;
    $newname =~ s/^~/_/g;
    $newname =~ s/^\+/p_/g;
    $newname =~ s/([^.])~/$1-/g;
    $newname =~ s/;/@/g;
    $newname =~ s/[ `'"!&*$;]/_/g;
    $newname =~ s/\$/_/g;
    $newname =~ s/[:{}|()<>]/-/g;
    $newname =~ s/^-/_/;
    $newname =~ s/\?/q/g;
    $newname =~ s/#/=/g;
    $newname =~ s/\\/_/g;

justLowercase:
    $newname =~ tr/A-Z/a-z/;

    if ($putname)
    {
        print "$newname\n";
        next;
    }

    $newname = $path . "/" . $newname;
    $_ = $path . "/" . $_;

    if ($newname ne $_)
    {
        print "$_ -> $newname\n";
        if (-e $prefix . $newname)
        {
            print STDERR "$newname already exists; can't rename\n";
        }
        else
        {
            if (rename($prefix . $_, $prefix . $newname))
            {
                $_ = $newname;
            }
            else
            {
                print STDERR "Rename to $newname failed.\n";
            }
        }
    }

    # Changes to file contents . . .
    $_ = $prefix . $_;
    if (-T $_ && !$cdrom)
    {
        if (system('uncrnl', $_)/256 == 0)
        {
            print "Uncrnl'ed $_\n";
        }
        else
        {
            print STDERR "Uncrnl $_ failed.\n";
        }
    }
}


[-- Attachment #3 --]
#! /bin/tcsh -f
# set echo

if ("$1" =~ */*) then
    set plc = $1:h
    set file = $1:t
    cd $plc
else
    set file = $1
endif

mv $file }$file}
tr < }$file} > $file -d '\r'
help

Want to link to this message? Use this
URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?01112703072100.65893>