Script to number incoming files

Hey guys,

I am working on a Cshell script and I am stuck on this one part. I need to be able to copy in files to my directory but give them different names so they don't overwrite each other. For example, my folder already contains FILE.1 I want my script to name the next file copied over FILE.2 and the next one FILE.3 and so on. It should be able to tell what files are there and named the new one the next highest value. So I have FILE.1, FILE.2, and FILE.3 in my directory now, If I copy in another file, my script should rename it FILE.4 This script has been driving me nuts all day I can't figure it out. Here is what I have so far. Thanks in advance. New code would be appreciated because I obviously have no clue what I'm doing in this piece.

while (-d FILE.0)
cp -r FILE FILE.1
 
set ctr = 1
foreach f(*)
   set var1 = 'file $f | awk '{printf "%s\n",$3}''
   if ($var1 == 1) then
      FILE = 'ls $f | cut -f1 -d "."'
      1 = 'ls $f | awk -F "." '{printf "%s\n",$2}''
      mv $f $FILE$ctr.$1
      ctr = 'expr $ctr + 1'
  endif
end
end
#!/bin/sh

i=1

while(a=1)
do
     if test -e  "FILE."$i
     then
       let i+=1
     else
       break
     fi
done

cp -f FILE FILE.$i

In Korn Shell:

#!/bin/ksh

if [ ! $# -eq 2 ]
then
        echo "Usage: $(basename $0) <filename> <target directory>"
        exit 1
fi

fname=$(basename $1)
current=$(pwd)

cd $2

lastval=$(ls $fname* 2>/dev/null |sort | tail -1 | cut -d. -f2)

if [ "$lastval" = "" ]
then
        lastval=1
else
        (( lastval = lastval + 1 ))
fi

cd $current
cp -f $1 $2/$1.$lastval

If your script is for example "mycopyfile", use it like:

./mycopyfile FILE /path/to/files

what exactly does the a represent in this code?

Dummy variable to force the while to loop forever... you exit out of the infinite loop with the "break" statement.

Note: I made a mistake in the "cp" command line option in the example above (corrected and noted).

below may help you some

name=`for i in dir1/*
do
	echo $i | sed 's/^\(.*\/\)\([^0-9]*\)\(.*\)/\2 \3/'
done | sort -n +1 | tail -1 | awk '{print $1""($2+1)}'`
cp file_to_be_copied dir1/$name