Help Programming in Perl for File Renaming

C

crazycelicagts

Hello everyone. I was wondering if someone can help me write a perl
program that does the following. I have a folder which contains a bunch
of files such as:

[folder]
a,b,c,d,e,f,g,.....

within those files is a line that has a statement, "File name:
TestA"...how do I write a program that goes in each of the files and
extract the "TestA" and rename the file "a" to "TestA" file. So in the
end, it will look like this:

[folder]
TestA, TestB, TestC, TestD, TestE, ....

If anyone can help me out, I would greatly appreciate it. Thanks.
 
L

Lynn

Hello,

Hello everyone. I was wondering if someone can help me write a perl
program that does the following. I have a folder which contains a bunch
of files such as:

Well the first thing I would do is get a list file files that are located
within the directory in question. See http://tinyurl.com/4k4ej

Then I would slurp the file into an array or varable See:
http://tinyurl.com/5c354

find and extract what you want

http://tinyurl.com/6bxa2

and then rename the files in question

http://tinyurl.com/5225f
If anyone can help me out, I would greatly appreciate it. Thanks.

That should get you started, If you run into any problems just
post the code that you have written and someone here will gladly
help you fix it.

Hope this helps

Lynn
 
J

Jon

Hello...thanks for the response. i am a bit confused on slurp the file
into an array...can you give me an example and what not. Thanks.
 
B

babydoe

TMTOWTDI : means it's Larry's way or the hiway.

Indicates the exit sign with a gesture, "Lynn there's the
hiway." It's not so much the turgid examples written in
archaic Perl that you've dredged up, but rather that your
algorithm is needlessly wasteful. It is wasteful because it
slurps the whole file when it only needs to read a part of
the file (and probably only the head.) Your algorithm
should exit a file on the first match.

Don't fret; there's no need to finger point at me : your
gentle narrator is also hitting the hiway, after failing to
provide a good solution. And I do realize I write Perl like
a clown, but from what I've read on this newsgroup I won't
get a good solution unless I put up a bad solution. (Is
that a usenet law?). So here is my attempt :

#!perl
use strict ;
use warnings ;
@ARGV = <*.txt> ;
my %name ;
while (<>) {
# finds last match want an early exit on first match.
$name{$ARGV} = $1 if ( /File name:\s+(\w+)/ ) ;
}
rename $_, $name{$_} for (keys %name) ;
__END__

and a sample file called a.txt :

----%<-----------start of a.txt----------------------


File name: testA


----%<-----------end of a.txt------------------------
 
B

Brian McCauley

And I do realize I write Perl like
a clown, but from what I've read on this newsgroup I won't
get a good solution unless I put up a bad solution. (Is
that a usenet law?). So here is my attempt :

[ snip perfectly good solution ]

I can't fault your solution.

Well, if you really insist we find fault
rename $_, $name{$_} for (keys %name) ;

Could perhaps...

rename $_, $name{$_} or die "rename $_: $!" for (keys %name);

But I'd only bother to do that if this script were to be kept and
reused. If it were a disposable single use script I might not bother.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,189
Members
46,734
Latest member
manin

Latest Threads

Top