[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

loop to read through txt files names in a directory

Posted on 2005-05-16
6
Medium Priority
?
190 Views
Last Modified: 2010-03-31
ok so persay i have these files

CRS Jun05 exclude.txt
CRM Jun05 exclude.txt
FDR Jun05 exclude.txt

i need to loop through, split and assign each part to a variable

for example

v_agency1_name = crs
v_agency1_date = June05
v_agency1_process = exclude
v_agency2_name = crm
v_agency2_date = June05 etc etc.

so i basically need to read all the .txt files in a given directory (/data) and append to variables. it would have to loop through till all the files are read then exit the loop
ive asked a previous question similar to this but some requirements have changed

currently im using

FilenameFilter filter = new FilenameFilter() {
                  public boolean accept(data, String name) {
                        return name.toLowerCase().endsWith(".txt");
                  }
      };
      String[] fileNames = new File(data).list(filter); // (where directoryName is your starting dir)
      String csvFile = fileNames[0]; // range check it though

      //split filename and assign variables
      String fileName = csvFile;
      String parts = fileName.split("[ .]");
               System.out.println(parts[0] + "-" + parts[1] + "-" + parts[2] + "-" + parts[3]);
      
could someone show me how to do this?

Thanks

0
Comment
Question by:daveleblanc
  • 4
  • 2
6 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 14013103
You seem to be getting no further forward - why is that - you've already asked at least two questions on this..?
0
 

Author Comment

by:daveleblanc
ID: 14013133
they've changed some requirements, notice im using your suggestion which is fine
but instead of one filename, its all that are listed in the directory

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 14013165
YOu just need to loop through the array 'fileNames' and do what you're doing already
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:daveleblanc
ID: 14013256
can you suggest a good site to find examples at

all im finding is bull****...

im am so ****ed on this application
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 200 total points
ID: 14013293

FilenameFilter filter = new FilenameFilter() {
       public boolean accept(data, String name) {
            return name.toLowerCase().endsWith(".txt");
       }
};
String[] fileNames = new File(data).list(filter); // (where directoryName is your starting dir)

for(int i = 0;i < fileNames.length;i++) {
      String csvFile = fileNames[i]; // range check it though
      //split filename and assign variables
      String fileName = csvFile;
      String parts = fileName.split("[ .]");
      System.out.println(parts[0] + "-" + parts[1] + "-" + parts[2] + "-" + parts[3]);        
 
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 14156781
:-)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
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
Course of the Month18 days, 20 hours left to enroll

834 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