Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

using command "ls *.report" in java

 
loz rad
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20492
54
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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().
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
loz rad
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Joanne Neal
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote:name.toLower().endswith(".report");
compilation fails

 
loz rad
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Harsha Smith wrote:
Joanne Neal wrote:name.toLower().endswith(".report");
compilation fails


I didn't want to make it too easy for the OP
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic