Solved

How to check file's first name?

Posted on 2006-06-23
11
226 Views
Last Modified: 2010-03-31
I have a situation in which I only have a file's first name(NOT EXTENSION) like 'ms0045' that is coming from database.
Now there may or may not be a file in a known directory with that name.  I want to check if 'ms0045' exists in the directory with any extension or not.

how can i do that.
0
Comment
Question by:jaipur07
  • 5
  • 4
  • 2
11 Comments
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 250 total points
ID: 16969624
Just iterate the directory

boolean found = false;
String[] files = new File(x).list();
for(int i = 0;i < files.length && !found;i++) {
      found = files[i].getName().startsWith(searchName);
 
}
0
 
LVL 30

Accepted Solution

by:
Mayank S earned 250 total points
ID: 16969634
File f = new File ( "filepath/filename.extension" ) ;
String fullFileName = f.getName () ;
String fileName = fullFileName.substring ( 0, fullFileName.lastIndexOf ( "." ) ) ;

should do it, I guess....
0
 

Author Comment

by:jaipur07
ID: 16969640
startsWith will return all the names like ms00451212', 'ms00455555'

isn't it?
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16969643
(I mean just getting the name of a file - not searching)
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16969665
CEHJ's and mine combined should do both.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 30

Expert Comment

by:Mayank S
ID: 16969671
>> String[] files = new File(x).list();

Use listFiles () if you want to check for only files, not directories.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16969674
>>startsWith will return all the names like ms00451212', 'ms00455555'

Yes. If that's a problem you can do


found = files[i].getName().startsWith(searchName) && searchName.equals(files[i].getName().substring(0, files[i].getName().lastIndexOf('.'));
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16969680
(You can remove the first condition before &&)
0
 

Author Comment

by:jaipur07
ID: 16969737
let me try and will get back to yo usoon
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16969807
Note that there is small optimization in my approach that you don't have to call getName () multiple times saving a method-call :)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16969830
:-)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
network + 7 80
groupNoAdj 7 86
groupSumClump challenge 9 102
windows explorer path to command prompt 5 33
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This video teaches viewers about errors in exception handling.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now