Solved

How to check file's first name?

Posted on 2006-06-23
11
213 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:
mayankeagle 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:mayankeagle
ID: 16969643
(I mean just getting the name of a file - not searching)
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 16969665
CEHJ's and mine combined should do both.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 30

Expert Comment

by:mayankeagle
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:mayankeagle
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
triangle challenge 4 79
stringclean challenge 26 56
recursion example 16 70
thymeleaf natural templating vs JSP 2 27
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

760 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

19 Experts available now in Live!

Get 1:1 Help Now