Searching Files

Hi,

I have a file
/db01/dat/march 2006/7001DW06.03B

Please note, between "march 2006" there is a space/tab.

While running the following script, it identifies
/db01/dat/march ----> as first file
2006/7001DW06.03B ---> as second file.

SRC_PATH = /db01/dat
SEARCH_FILENAME = 7001DW06.03B

for i in `find $SRC_PATH -name $SEARCH_FILENAME`
do
IDX=`expr index $i .`

if [ $IDX -ne 0 ]
then
STRTPOS=`expr $IDX - 8`
FILENAME=`expr substr $i $STRTPOS 12`
FILETYPE=`expr substr $FILENAME 5 2`

        if [ "$FILETYPE" = "DW" ] || [ "$FILETYPE" = "dw" ]
        then
             echo Extracting $FILENAME ...
        \#      unzip -jo $i -d $DEST_PATH/$1
        fi

fi
done
echo Done

I need to get this as a single file instead of two files.

Regards,
Ronald.

Your code can be written in a simpler way also, if you can provide us your exact requirement, what I understand from your code is that you have to search for files which have "dw or DW" somewhere in their names and then uncompress those files to a specific directory, is it like that?

For your current problem, you can try something like this:

for i in `find $SRC_PATH -name "$SEARCH_FILENAME"`

Hi,

Following is a replication only.

The directory "/db01/dat/march 2006" contains many files like *DW*.03B and others. But in directory name "march 2006" there is a space in between. (While user creating the folder in ftp through internet explorer, they have created with spaces also).

There is a test files like
/db01/dat/march 2006/7001DW06.03B

My sample script is as follows....

SRC_PATH = /db01/dat
SEARCH_FILENAME = *DW06.03B

for i in `find $SRC_PATH -name $SEARCH_FILENAME`
do
echo $i

IDX=\`expr index $i .\`

if [ $IDX -ne 0 ]
then
	STRTPOS=\`expr $IDX - 8\`
	FILENAME=\`expr substr $i $STRTPOS 12\`
	FILETYPE=\`expr substr $FILENAME 5 2\`

	if [ "$FILETYPE" = "DW" ] || [ "$FILETYPE" = "dw" ]
	then
		echo Extracting $FILENAME ...
		\# unzip -jo $i -d $DEST_PATH/$1
	fi
fi

done
echo Done

While running the above script,
echo $i Displays "/db01/dat/march" and "2006/7001DW06.03B" instead of /db01/dat/march 2006/7001DW06.03B

In /db01/dat/march, the index is zero(0), so i ignore. But in 2006/7001DW06.03B the index is > 0 so I try to extract the file. Since it gets the path($i) as 2006/7001DW06.03B instead of /db01/dat/march 2006/7001DW06.03B it fails.

My requirement is, $i should get /db01/dat/march 2006/7001DW06.03B. Then my problem is solved.

Any good script is most welcome.

Regards,
Ronald.

Did you try:

for i in `find $SRC_PATH -name "$SEARCH_FILENAME"`

i.e. double quotes around variable name? Try to use double quotes around all variable names.

Still you didn't mention your requirement clearly, you are emphasizing on the space in filename problem but not telling what exactly you are trying to do.

Any ways, try double quotes and see the results.

Hi shereenmotor ..

We have a ftp site. User open this ftp site using intertnet explorer and upload compressed file(these files has a pattern). Sometimes they create folders and while creating folders they use white spaces in between like "march 2006".

While extracting I search for the files and extract. My problem is if they created folder having white spaces I can not exttract using the script given above.

Yes, I tried giving the search variable inside double quotes and still not working.

Regards,

Python alternative:

#!/usr//bin/python
import os,zipfile
root = "/db01"
SRC_PATH = os.path.join(root,"dat")
SEARCH_FILENAME = "7001DW06.03B"
filetype  = SEARCH_FILENAME[4:6] #DW
for r,d,fi in os.walk(SRC_PATH):
        if fi[0] == SEARCH_FILENAME:
                fullpath = r + os.sep + SEARCH_FILENAME
                z = zipfile.ZipFile(fullpath ,"r")
                for i, name in enumerate(z.namelist()):
                        outfile = open( fullpath + ".extracted", 'wb')
                        outfile.write(z.read(name))
                        outfile.flush()
                        outfile.close()

Ok, in that case, what we can try to do is that, simply replace each space with a pattern which is extremely unlikely to occur ("__"), run below script to replace white spaces from file and directory names with ("") before you run your own script.

#! /bin/ksh

SRC_PATH = /db01/dat

# first, we fix the directories

for name in $(find $SRC_PATH -type d -print | sed 's/ /____/g')
do
  if [ $name != "." ] ; then
    oldname="$(echo $name | sed 's/____/ /g')"
    newname="$(echo $name | sed 's/____/_/g')"
    if [ "$oldname" != "$newname" ] ; then      
      mv "$oldname" "$newname"
    fi
  fi 
done

#fixing individual files...

for name in $(find $SRC_PATH -type f -print | sed 's/ /____/g')
do
  oldname="$(echo $name | sed 's/____/ /g')"
  newname="$(echo $name | sed 's/____/_/g')"
  if [ "$oldname" != "$newname" ] ; then
    mv "$oldname" "$newname"
  fi
done

is there a better way, instead of changing the directory names!!!

ronald

You can rename back those directory names to original.

I'm sure there would be a better way to handle white spaces in filenames with find, wait until someone else replies, I have seen this problem first time and I could only think of rename directory names and then reverse the rename script after you have done with find command.

Try setting IFS to some character that you would never see in the file or directory names.

Before:

for i in "foo bar baz"; do
  echo $i
done

Output:

foo
bar
baz

After:

IFS="%"
for i in "foo bar baz"; do
  echo $i
done

Output:

foo bar baz

Hi Glenn Arndt,

It worked, Thanks very much.

Ronald.

Hi Glenn,

How would I set IFS to "new line" .

Ronald.

Type IFS=" then hit enter, then type ".

prompt$ IFS="
"
prompt$