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

How do I check if a file exist in a directory?

Status
Not open for further replies.

Josewr

Technical User
Feb 28, 2005
15
US
I am requesting, through AWK, for the operator to enter a filename:

Ans = 0

BEGIN {while (Ans == 0) {

{printf "Enter file Name: "
getline name < "-"
}
{printf "This is the name you entered: "
print name
printf "Is that correct? 1, 0 "
getline Ans < "-"
print Ans
}

}
etc..

How do I check if the file exists in the current directory before I operate on it so my progaram won't crash?

Thanks
 
Simply by checking the return value of getline

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Thanks, but:
Suppose I enter "myfile.asc" but the actual file in the directory is "Myfile.asc" or "myfi1e.asc" (Note the numeral "1" in place of the letter "l")
My question is "Is there a way for the program to check that the name exists in that directory?
I want the program to check it rather than me doing it visually.
This may clarify my question.

Thanks again
 
I think PHV's suggestion is still what you want? Something along the lines of if (getline < name) { process_file } else { complain_to_user }?

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top