File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes using command Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "using command "ls *.report" in java" Watch "using command "ls *.report" in java" New topic
Author

using command "ls *.report" in java

loz rad
Greenhorn

Joined: Oct 16, 2011
Posts: 3
hello I have scrounged the site for a while and I think I should post my problem:
problem, I want to get the list of all files ending in .report eg "hotmail.report" so I am using "ls *.report".
if i type "ls *.report" into bash I get it no worries.
This is the code I am using:


this code gives output: writer = ls: cannot access *.report: No such file or directory.
so it isnt recognising the command *.report to mean all files ending in ".report".
Could someone please give me some suggesstions? Anything appreciated.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

Are you sure that there are *.report files in the current folder? You can print new File(".").getAbsolutePath() to find out what the current folder is.

Also, you can do most of what you want using File.listFiles with a right FileFilter or FilenameFilter, in combination with the methods from File like lastModified() and length().


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3646
    
  15
As Rob says, using the File class is a better way of doing this.
However if you really need to do it using ls, then the problem you are having is that it is the *nix shell that interprets the *.report part of the command, so you need to actually run a shell and pass "ls" and "*.report" as arguments to it. I'm afraid I don't know how to do this in *nix, but in Windows it would be something like
cmd /C dir *.report
I'm sure similar questions have been asked before so you might find the answer by searching or you can wait until a *nix user reads your post.

Joanne
loz rad
Greenhorn

Joined: Oct 16, 2011
Posts: 3
Hello Rob, yes I am sure there are files ending in ".report" in my current folder as running the command in bash gives me two filenames.
I'll checkout FileFilter and see if I can search for files ending in .report (because they can have any name, i need to search for the .report).
Thanks for the lead I'll try it out.
Harsha Smith
Ranch Hand

Joined: Jul 18, 2011
Posts: 287
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3646
    
  15
Harsha Smith wrote:

This would require the regex to be compiled every time the accept method is called which could lead to performance problems.
For a simple comparison like this might be better.
Harsha Smith
Ranch Hand

Joined: Jul 18, 2011
Posts: 287
Joanne Neal wrote:name.toLower().endswith(".report");
compilation fails

loz rad
Greenhorn

Joined: Oct 16, 2011
Posts: 3
FINALLYYYYY! Rob you legend!
I used this example here: http://www.java-samples.com/showtutorial.php?tutorialid=384
used the OnlyExt class they made then put this into my code and it works it works it works!

thank you so much.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3646
    
  15
Harsha Smith wrote:
Joanne Neal wrote:name.toLower().endswith(".report");
compilation fails


I didn't want to make it too easy for the OP
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: using command "ls *.report" in java