#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Posts
    2
    Rep Power
    0

    Script to display File name in a directory and not sub-directories


    I am trying to write a simple Korn Shell script that displays the file name of a csv file. I don't want it to display any csv files name in sub-directories that are under the directory that has the csv file name I want to display

    My script name is called test_datafilename.ksh It contains the following

    #!/usr/bin/ksh
    DIR=/appl/pub/downloads/hrdata/data/Legacy/CombinedFile
    FILE=`find $DIR -name "*.csv" |awk -F/ '{print $NF}'`
    if [ -f $DIR/$FILE ]
    then
    print "$FILE was found"
    else
    print "Nothing Found"

    fi

    The file name that is located in the Directory is CombinedOriginationwithUpdates_04-2012.csv
    and there is a file named
    CombinedOriginationwithUpdates_02-2012.csv
    in the sub-directory that I do not want to display.

    However, when I run the script on my UNIX system it always returns the Else statement from my IF/Then/Else above

    /scripts>./test_datafilename.ksh
    Nothing Found

    What am I doing wrong?
  2. #2
  3. Come play with me!
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,756
    Rep Power
    9397
    How about a simple
    Code:
    FILE=`ls -1 $DIR/*.csv | head -1`
    (note that FILE will be the full path - not just the basename; if you want just that you can chain it with an "xargs basename")

    Comments on this post

    • aitken325i agrees
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Posts
    2
    Rep Power
    0
    Thanks that did the trick. Sometimes you get so engrossed in what you are doing you don't see the obvious answer.

IMN logo majestic logo threadwatch logo seochat tools logo