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

Selecting files in directory as part of loop 3

Status
Not open for further replies.

alqsp

Programmer
Dec 12, 2001
9
AU
Scenario:
I need to validate and move all the files in a directory. I need help in selecting each file.

So far I have this

.
FILECOUNT=`ls $INPUTDIR | wc -l`

while [ $FILECOUNT -gt 0 ]
do
<select file>
<validate file>
if valid
move file to valid_dir
else
move file to error_dir
fi
FILECOUNT=`expr $FILECOUNT - 1`
done
.

Basically I need some help in selecting each file, is there a way to select the file that appears alphabetically first in a listing?
 
I presume you want something more complicated than
[tt]
#!/bin/sh
for F in `ls`
do
<validate file $F>
if valid
mv $F valid_dir
else
mv $F error_dir
fi
done
[/tt]
???
One by one, the penguins steal my sanity. X-)
 
The method:
[tt]for F in `ls`[/tt]
May fall foul of a maximum arguments limit. A method that should not suffer from this limit would be:
[tt]ls | while read F[/tt]
Cheers, Neil
 
Hi,

You can try this:

#!/bin/ksh
#
function checkValid {

# Write your function here


}
for files in `ls -1 /<dir_path>`
do
checkValid
if [[ $? = 0 ]]; then
mv $files /valid_dir_path
else
mv $files /invalid_dir_path
fi
done


Thanks,

GP
 
That's about as complicated as it needs to be, thank you all for your help, all suggestions work great.

Al
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top