http://qs1969.pair.com?node_id=11127809


in reply to Renaming all files in a directory

hello Aldebaran,

you can avoid the oneliner BEGIN block using the Maori-farewell like in perl -M'5; $counter=0' -nle ...

But you can avoid the counter at all:

>perl -nlE "say sprintf qq(caption%03s.txt),$count++" one.txt two.txt + three.txt caption000.txt caption001.txt caption002.txt

PS you get the filename currently processed in $ARGV so you can play with it:

>perl -nlE "say sprintf q(caption%03s.txt),$1 if $ARGV =~ /.*([\d]+).* +/" one01.txt one02.txt caption001.txt caption002.txt

PPS note that perl -n and perl -p both skip empty files!

ls -l -rw-rw-r-- 1 io io 0 feb 2 13:28 one.txt -rw-rw-r-- 1 io io 0 feb 2 13:28 three.txt -rw-rw-r-- 1 io io 20 feb 2 13:37 two.txt perl -nlE 'say "processed $ARGV" if eof' *.txt processed two.txt

PPPS after choroba's nice below response I cant resist to update the title and to try it:

perl -MTie::Scalar -M"5;{package SN; use parent -norequire => 'Tie::S +tdScalar';sub FETCH {print qq(Opening ${$_[0]}); ${$_[0]}}}; tie $ARG +V, SN" -nlE "say qq(\tprocessed $ARGV) if eof" 001.txt 002.txt 003.txt Opening 001.txt Opening 002.txt Opening 002.txt processed 002.txt Opening 003.txt

L*

There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.