Visit Our UNIX and Linux User Community


reading filenames inside a program


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers reading filenames inside a program
# 1  
Old 04-04-2002
reading filenames inside a program

UNIX Sun Ultra60 5.5.1

Hello everybody, I have a problem that seems simple but turns out to be complex (for me at least). My program needs to open a directory (this part is easy), scan each filename and determine whether or not a file with the suffix (.07) exists. So the program would return true if these files existed in the specified dir:

00.01.02.03.01
01.02.01.06.02
03.06.02.06.07 <----
01.01.02.01.00

and false if the extension did not exist. I've tried looking for functions on the internet (like stat() for example) but haven't really found anything that seems like it would work. Is there a specific function that returns the filename into a given string? That would definitely solve all my problems...If anyone has any thoughts at all, I would greatly appreciate it. Thanks!
# 2  
Old 04-04-2002
Try using:

count=`ls *.07 | wc -l`

And then check if count is greater than 0

Hope it helps
# 3  
Old 04-04-2002
Sounds to me like your program is not a unix script. But if it is a unix script, the test suggested by lvalero will get your true/false, or to get each qualifying filename, you can:
Code:
#!/bin/sh
for fn in `ls *.07`
do
  echo "Processing $fn ..."
done
exit 0

Jimbo
# 4  
Old 04-04-2002
>Sounds to me like your program is not a unix script.

Well ... you can always try to execute Unix from inside your programs Smilie

BTW which language are you using to program. If you are using java the answer is very simple Smilie
# 5  
Old 04-04-2002
Programming in C

Thank you for all of your quick responses. I am programming in C, but I'm not sure how to implement scripts. Will that be necessary in this case? Thanks again.

James.
# 6  
Old 04-04-2002
In C you can use readdir() to read a directory. Type "man readdir" for more info.
 

Previous Thread | Next Thread
Test Your Knowledge in Computers #677
Difficulty: Medium
The IEEE does not test equipment for compliance with their standards.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell program question with Filenames and dates

Hi Unix Gurus Let's say I have the input files like the following. I need to pick the files based on my run date. abcd_20180206.csv abcd_20180213.csv abcd_20180220.csv abcd_20180227.csv efgh_20180206.csv efgh_20180220.csv efgh_20180227.csv ijkl_20180206.csv ijkl_20180213.csv... (9 Replies)
Discussion started by: SK123
9 Replies

2. UNIX for Dummies Questions & Answers

Reading filenames with spaces

Hello I've got a certain no. of files in a directory whose names I'm reading and redirecting into a temporary text file using the command below: ls -l | grep ^- | awk '{print $9}'However, whenever the file names contain spaces the above command considers only the part of the file name up to... (5 Replies)
Discussion started by: S. BASU
5 Replies

3. UNIX for Dummies Questions & Answers

Change to different user id inside a program

Hi, There is a process ( built in C/C++) which starts with my user id and I need to execute a specific function with a different user id. Is there any api so that I provide userid, passwd and the next instance the process will have the new user id privileges. - Pranav (3 Replies)
Discussion started by: k_pranava
3 Replies

4. Shell Programming and Scripting

Reading input record from inside nawk

Hi friends, I have small query with reg to awk search pattern.. below is my sample file and code which i tried.. $ cat file.txt xxx,yyyyy,messageID,sha xxxx,errorcode,messageID,name in the above sample file - let assume I know the errorcode(2nd record) using which I want to... (2 Replies)
Discussion started by: Shahul
2 Replies

5. Programming

how to call c executable inside c program??

hi guys i have only basic knowledge of c so guys plz help me ..... i want 2 call c executable which requires file name as argument and i need to modify file contents before calling that executable now my question is how can i call this c executable inside another c program with arguments ?? i... (9 Replies)
Discussion started by: zedex
9 Replies

6. UNIX for Dummies Questions & Answers

Reading inside a loop

Hi I am trying read in side a do statement but it is not working it is just printing abc before read but not stopping in abc for user input Can anybody please help #!/usr/bin/ksh cat sample_file | while read ln_source3 do param=`echo $ln_source3 | nawk... (1 Reply)
Discussion started by: ssuresh1999
1 Replies

7. UNIX for Dummies Questions & Answers

problem in reading inside a while loop

I am not able to read inside a while though i get the message "inside read" the cursor doesnt prompt from the console cat file | while read ln_new_engine_dirs do echo "inside $ln_new_engine_dirs" if then read nn echo "inside read" fi done Thanks in advance (3 Replies)
Discussion started by: ssuresh1999
3 Replies

8. Shell Programming and Scripting

bash: reading filenames from file

Hi, I'm trying to write a script that reads filenames from a file and use these filenames in a loop. The filenames are all on one line and the problem is that these filenames have wildcards like * and braces like in them. Right now what I'm doing is something like this: echo "reading from... (0 Replies)
Discussion started by: warp17
0 Replies

9. Shell Programming and Scripting

Reading filenames with extension .xml

Hi, I want to write a script to read all the filenames with extension .xml in a directory and pass the name of the file, one by one, to another function. Please help me out. Regards. Saurabh (3 Replies)
Discussion started by: bhalotias
3 Replies

10. UNIX for Dummies Questions & Answers

reading long filenames from nero to AIX

One of my colleagues is having an issue moving files between a windows box and the AIX servers in the office. The filenames are being truncated though i don't know to what extent. He's using Nero to burn the CD and I think he mentioned he's using Joliet. I found another thread that shows a... (1 Reply)
Discussion started by: categoryzd
1 Replies

Featured Tech Videos