• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to best count files in a String  RSS feed

 
Max Winfield
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
-1
down vote
favorite


I searched for a long time but didnt find a way to count the number of files in a string, which i get from reading the contents of a rar file (list of filenames and folders) seperated in lines.

Example for the String:



As you can see only item 1,2,3 are files and item 4,5 are folders. I want to count only the number of files (in this case 3).

I suppose i should count the lines in the string which contain a "." before an "\" [the String should be scanned from right to left] and put the value into a int variable? [is there a library or method which can do that?].

[I dont need to adress the exception where a file has no . in it]
 
Max Winfield
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
or perhaps does anyone know if there is any other library which can extract the filecount of a rar into an int?
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Max Winfield wrote:I suppose i should count the lines in the string which contain a "." before an "\" [the String should be scanned from right to left] and put the value into a int variable?


Not at all. It's possible for folder names to contain a "." and it's possible for file names to not contain a ".". In your example the context tells you that "Folder2" is a folder because there are examples of it being used as a folder, but you can't rely on that happening all the time.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I guess one option would be to use the previous lines (already processed) to determine if the current line (being process) represents a directory.

Henry
 
Dave Tolls
Ranch Foreman
Posts: 3061
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are you reading the rar file?
Are you using the zip package in Java?

If so, the ZipEntry class has an isDirectory method...
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why did you say downvote in your post? Have you been copying from SO or some other site? If so, please tell people (on both websites) about the cross‑posting.
How do you intend to “count” Strings? Maybe the whole text file is one String. Maybe each line is a String. Maybe the \ characters separate the lines into multiple Strings. We would need to know what you consider to be a String to count.
 
Max Winfield
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I apologize. I found the solution by using raroscope library and getting long values for filesizes instead of the string of filenames. I just dont know how to close a thread on coderanch or mark it as solved.
 
Max Winfield
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
didnt know about crossposting please accept my apologies and thank you to Campbell Ritchie for pointing it out (^_^)
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raroscope? Please explain how that works. Did you want to add the sizes of the files?
 
Max Winfield
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Campbell Ritchie!

I opened another thread because this thread name is about best counting things in Strings and not rars. So i thought the best would be to open a second thread which contains the answer to your question as well and can be easier found by anyone searching for the specific problem. (This threadname adresses another problem although i found a better solution which i adress in the other thread)

https://coderanch.com/t/673353/java/filecount-rar-compressed-file
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!