Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Assign unique sequence number to a file

Status
Not open for further replies.

ryanc2

MIS
Apr 18, 2003
73
US
How do I rename files from a list to contain a 5 digit unique sequence number in the filename?

example: file.dat to file00001.dat
file.dat to file00002.dat

Also, once the sequence reaches 99999, I need to start over at 00001.

Thanks in advance for any ideas.
 
#----define a file to hold the counter
COUNTFILE=count.txt

#----create the counter if file does not exist
if [ ! -f $COUNTFILE ]
then
echo 0 > $COUNTFILE
fi

#----define a function to update and return the counter
function get_count {
awk '{
c=$1;
if (c==99999) c=0;
c++;
printf "%05d",c
} END {
print c > FILENAME}' $COUNTFILE
}

#----use a loop to rename *.dat files
for file in *.dat
do
mv $file ${file%.dat}$(get_count).dat
done
 
Ygor -

It appears to work beautifully. I'll post if I have any more questions.

Thanks!!
 
Ygor -

One problem that I need to get around. Basically, I need to ftp a list of candidate files. However, before I pull them, I need to assign each a unique sequence number as described in my above post. I have defined the get_count function earlier in the script.

If I do this:

while f=$(line)
do
ftp ${args} ${ip} <<-EOF
user ${id} ${pwd}
rename $f.DAT $f${get_count}.DONE
get $f$(get_count).DONE
quit
EOF
done < ${file}_good.tmp

Then it will again assign a new ssequence number during the &quot;get&quot; command. How can I define the sequence number so I &quot;get&quot; the same file name that I created during the reanme step.

Thanks again for any help.
 
You could assign the sequence number to a variable before the ftp, eg...

while f=$(line)
do
[blue]seqno=$(get_count)[/blue]
ftp ${args} ${ip} <<-EOF
user ${id} ${pwd}
rename $f.DAT $f[blue]$seqno[/blue].DONE
get $f[blue]$seqno[/blue].DONE
quit
EOF
done < ${file}_good.tmp
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top