Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to check file's first name?

Posted on 2006-06-23
11
Medium Priority
?
261 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 1000 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 1000 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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
 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

824 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