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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checking if files already exists in a directory

Status
Not open for further replies.

HHG

Technical User
Nov 8, 2003
68
GB
Can anyone help

in a directory I have a list of files.
import directory
abc.txt
def.txt
ghf.txt

transfer directory contains.
abc1.txt
abc2.txt
def1.txt
def2.txt etc..

Whats the best way to check that the import directory doesn't already have this file. If it does I want to save it into the next transfer area.

Any ideas on how I might achieve this, that would be a great help.
Thanks in advance.
 
Code:
for file in $(cd /path/to/import/dir;ls)
do
  if [ -f /path/to/store/dir/$file ] 
  then
    mv /path/to/import/dir/$file /path/to/other/dir
  else
    mv /path/to/import/dir/$file /path/to/store/dir
  fi
done

Ceci n'est pas une signature
Columb Healy
 
Hi Columb thanks for this. Sorry, but please can you explain a little further about your suggestion. Only you have to/import, to/store and to/other

I have the following directories

PREIMP - This will hold all the files in here.
1111abc.txt
2222abc.txt
3333def.txt
44444def.txt etc..

I only want to move the earliest file into the IMPORT directory - I don't want to move any files the IMPORT directory if the previously moved still exists.

IMPORT - The files in here will be processed by an application batch run and will delete once they have been processed.

IMPORT directory holds
abc.txt
def.txt

The script to loop again and look in PREIMP and rename moving files for the next import

Any offer of help with examples would be most appreciated.
Thanks in advance
 
Sorry, I misunderstood the problem
The key, as I see it, is identifying that abc.txt relates to 1111abc.txt
so
Code:
for file in $(cd PREIMP;ls)
do
  shortfile=$(file##*[0-9]} #shortfile is file with all characters up to the last number stripped off
  if [ ! -f IMPORT/$shortfile ] # If we don't have one already
  then
    mv PREIMP/$file IMPORT/$shortfile # move and rename the file
  fi
done
Is that better?

Ceci n'est pas une signature
Columb Healy
 
Thanks for this I managed to get this working using length then doing a if statement.

e.g.echo $i | nawk '{print substr($1,13,length($1))}'`

Thanks again
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top