Script to number incoming files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to number incoming files
# 1  
Old 03-18-2009
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.

Code:
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

# 2  
Old 03-19-2009
Error

Code:
#!/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


Last edited by wabard; 03-19-2009 at 09:53 PM.. Reason: FIXED The cp command line argument to -f from -r (I typed the wrong option, r and f are close on the keyboard .. lol )
# 3  
Old 03-19-2009
In Korn Shell:
Code:
#!/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:
Code:
./mycopyfile FILE /path/to/files

# 4  
Old 03-19-2009
Quote:
Originally Posted by wabard
Code:
#!/bin/sh
 
i=1
 
while(a=1)
do
     if test -e  "FILE."$i
     then
       let i+=1
     else
       break
     fi
done
 
cp -r FILE FILE.$i

what exactly does the a represent in this code?
# 5  
Old 03-19-2009
Quote:
Originally Posted by hootdocta5
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).
# 6  
Old 03-19-2009
below may help you some

Code:
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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to count number of files in directory and write to new file with number of files and their name?

Hi! I just want to count number of files in a directory, and write to new text file, with number of files and their name output should look like this,, assume that below one is a new file created by script Number of files in directory = 25 1. a.txt 2. abc.txt 3. asd.dat... (20 Replies)
Discussion started by: Akshay Hegde
20 Replies

2. Shell Programming and Scripting

Script to count number of files in directories

Hi All! I would like to have a script that will count the number of files at the top of the hour of soome directories and mail the results to me. I was thinking on : a=`/directory/subdirectory/ | wc -l` echo "/directory/subdirectory :$a" b=`/another_dir/subdir/ | wc -l` echo... (12 Replies)
Discussion started by: fretagi
12 Replies

3. Shell Programming and Scripting

reading number of files..one by one in awk script

Hi I have awk script that is reading data from file and printing the result on the monitor....I need to read more than one file .....one by one... and store the result in output file... ---------- Post updated at 06:41 AM ---------- Previous update was at 06:39 AM ---------- please i need... (4 Replies)
Discussion started by: aldreho
4 Replies

4. UNIX for Dummies Questions & Answers

Help: script to monitor incoming files

I have 20 or so files that are ftp'd each day to a particular directory. I want to create a script that will tell me if any of these files fails to arrive or if any additional files arrive. I'm thinking I'd have a list of file names that should arrive each day, and the script would check each days... (2 Replies)
Discussion started by: daveyc82
2 Replies

5. Shell Programming and Scripting

Script to Compare a large number of files.

I have a large Filesystem on an AIX server and another one on a Red Hat box. I have syncd the two filesystems using rsysnc. What Im looking for is a script that would compare to the two filesystems to make sure the bits match up and the number of files match up. its around 2.8 million... (5 Replies)
Discussion started by: zippdawg2001
5 Replies

6. Shell Programming and Scripting

Detecting incoming files without busy polling

Hello, I'd like to handle incoming (uploaded) files from a shell script, ideally without busy polling / waiting (e.g. running a cron task every 15'). Is there a command that would just sleep until a new entry has been created in a directory, allowing scripts such as the following: while... (9 Replies)
Discussion started by: baldyeti
9 Replies

7. Shell Programming and Scripting

script to find the average number or files?

Anyone has a script or command in UNIX that can take 4 to five different numbers and calculate the average? (2 Replies)
Discussion started by: bbbngowc
2 Replies

8. UNIX for Dummies Questions & Answers

run a script from incoming email

hi I have a bit of a problem i need help with. I have a script that runs no problems when i run it manually. as soon as i stick in tester: /app/scripts/run.pl into /etc/aliases & try to run it by sending a mail it doesnt work. the output files are owned by daemon, which i dont like & think... (7 Replies)
Discussion started by: jojo77
7 Replies

9. UNIX for Dummies Questions & Answers

find number of incoming requests to a server

How to find the number of incoming requests to a http server and the maximum requests it can serve? (1 Reply)
Discussion started by: laddu
1 Replies

10. Shell Programming and Scripting

awk script to find the number of files

awk script to find the number of files in a directory with their date less than 15-oct-2006 please help (4 Replies)
Discussion started by: uni_ajay_r
4 Replies
Login or Register to Ask a Question