🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.perl.misc
Article #331

Re: Perl script question for gurus

#331
From: Scott Henry
Date: Fri, 29 Aug 1997 00:00
48 lines
2209 bytes

[Cc-ed to mark@tmn.com>

>>>>> "c" == chris abraham <chris@purple.tmn.com> writes:

c> I'm trying to rename a few thousand files in sequential order.

c> ie.

c> I have:
c> I want to change it to:

c> blah.txt   -->			00000001.txt
c> blub.txt			-->			00000002.txt
c> this.txt   -->   00000003.txt
c> that.txt   -->			00000004.txt
c> etc.txt				-->			00000005.txt

A quick-n-dirty sketch, you may have to do a little debugging:

#!/usr/bin/perl

$dir = shift || ".";    # first argument is directory
$start = shift || "0";  # second argument is starting number

$fnbase = sprintf "%08d", $start;
open(D, $dir) || die "can't open $dir? $!\n";

@files = map { $_ if (-f $_ and $_ !~ /^0.*txt/)} readdir(D);
closedir(D);

foreach $file (@files) {
        rename($file, $fnbase.".txt");
        $fnbase++;
}
__END__


c> I'll probably be running off a Windows box... I've heard there are
c> differences in the versions of perl.

This one was only tested under Unix.

-- 
 Scott Henry <scotth@sgi.com> /  Help! My disclaimer is missing!
 IRIX MTS,                   /  GIGO *really* means: Garbage in, Gospel Out
 Silicon Graphics, Inc      /  http://reality.sgi.com/scotth/
Technical Details
Message-ID: <yd8u3g8lq0c.fsf@hoshi.engr.sgi.com>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <Pine.SOL.3.96.970825153417.22465A-100000@purple.tmn.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 112333,bbbaaa795486a906
X-Google-Attributes: gid112333,public
X-Google-Thread: f72c1,bbbaaa795486a906
X-Google-Attributes: gidf72c1,public
X-Google-Thread: fa0ae,bbbaaa795486a906
X-Google-Attributes: gidfa0ae,public
X-Google-Thread: 1133f4,bbbaaa795486a906
X-Google-Attributes: gid1133f4,public
X-Google-Thread: 10f591,bbbaaa795486a906
X-Google-Attributes: gid10f591,public
From: Scott Henry <scotth@sgi.com>
Subject: Re: Perl script question for gurus
Date: 1997/08/29
Message-ID: <yd8u3g8lq0c.fsf@hoshi.engr.sgi.com>
X-Deja-AN: 268972677
Sender: scotth@hoshi.engr.sgi.com
X-Disclaimer: These are my opinions only, I cannot speak for SGI
References: <Pine.SOL.3.96.970825153417.22465A-100000@purple.tmn.com>
Organization: SSO Engineering, Silicon Graphics, Inc.
X-URL: http://reality.sgi.com/scotth/
Newsgroups: comp.lang.perl.misc,comp.lang.perl.modules,comp.lang.perl.tk,de.comp.lang.perl,fr.comp.lang.perl