Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 15 Jan 2002 06:54:33 -0500
From:      Brian T.Schellenberger <bts@babbleon.org>
To:        "J.S." <johann@broadpark.no>, freebsd-questions@FreeBSD.ORG
Subject:   Re: Filename tool?
Message-ID:  <0dbf70255110f12FE6@Mail6.nc.rr.com>
In-Reply-To: <20020114205116.3183e89a.johann@broadpark.no>
References:  <20020114205116.3183e89a.johann@broadpark.no>

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

--------------Boundary-00=_XEAZKSMB54C4INK23EO1
Content-Type: text/plain;
  charset="iso-8859-1"
Content-Transfer-Encoding: 8bit


I have one I wrote that I've posted here before.  Just a little (?) perl 
scirpt.  It's "unmsdos" and it wants to fix the cr-nl problem as well while 
it's at it; you can either use my uncrnl (also attached) or just disable this 
feature.  It only tries to uncrnl text files.

Let me know if you find it useful.




On Monday 14 January 2002 02:51 pm, J.S. wrote:
> Hi.
>
> I have large collection of files within many branches of filetypes, and
> before they enter my 'main' collection I have to manually change the
> filenames so that I am satisfied with the way they are.
>
> E.g.:
>
> Artist_-_Dis'z_Deh_Name_Of_My_Song_(My_Remix)-GROUP.mp3
>
> I rename to:
>
> artist-disz_deh_name_of_my_song-my_remix-group.mp3
>
> Is that 100% POSIX compliant or what? =)
>
> Anyway, I was wondering if there existed a tool which can organize both
> files and directories the same way I do. Perhaps even better?
>
> To Unsubscribe: send mail to majordomo@FreeBSD.org
> with "unsubscribe freebsd-questions" in the body of the message

-- 
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 

--------------Boundary-00=_XEAZKSMB54C4INK23EO1
Content-Type: application/x-perl;
  name="unmsdos"
Content-Transfer-Encoding: 8bit
Content-Description: Undo MS-DOS/WIndows filename weirdness
Content-Disposition: attachment; filename="unmsdos"

#! /usr/bin/perl
#
# By Brian T. Schellenberger   (bts@babbleon.org)
# Please maintain this credit but otherwise, use as you see fit.
# (If it gets substantially transmorgified, it would probably be
# best to just say "based on . . . "
#

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";
        }
    }
}


--------------Boundary-00=_XEAZKSMB54C4INK23EO1
Content-Type: application/x-shellscript;
  name="uncrnl"
Content-Transfer-Encoding: 8bit
Content-Description: Undo CR-NL
Content-Disposition: attachment; filename="uncrnl"

#! /bin/tcsh -f
# set echo
# By Brian T. Schellenberger
# probably pointless but I didn't know about fromdos or whatever the
# standard tool is when I wrote it.

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'

--------------Boundary-00=_XEAZKSMB54C4INK23EO1--

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-questions" in the body of the message




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