Link to home
Start Free TrialLog in
Avatar of Sat80
Sat80

asked on

Load different files and arrange them to one file


Hi Experts,

I have a question which is a bit complex for me as it load different text files and arrange them to one.

The idea is that, I have a different text file like this:
test1.txt
test2.txt
test3.txt
The number of the files is unknown, but I know that the file name will start by "test" and then a number starts from 1 and then the ext ".txt". Each file will include four numbers like this:
test1.txt:.
1
2
3
4

Open in new window


test2.txt:
2
6
9
8

Open in new window


test3.txt
9
10
12
13

Open in new window


The idea is that, I want to load all the files and then output one file (output.txt) with specific format. The furcation will first load the first file which is test1.txt
1
2
3
4

Open in new window

Save it to output.txt file and then load the second file and try to match the numbers in test2 to test1 so as you can see there is match which is number 2. It then loads test2.text to the output file starting from number 2 in the file. So, the result is that:
Output.txt
1
2	/2
3	/6
4	/9
	/8

Open in new window


And then load the other files with the same idea, so the final result will be like this:
1
2	/2
3	/6
4	/9	/9
	/8	/10
		/12
		/13

Open in new window

And so one. To summarise the idea, load the first text file and output the content to an output file (output.txt) and then load second file and try to match the first number with the previous data on (output.txt) when it finds matches (and it has to find on it might be the first, second, third or fourth number) then it will add the second file just next to the matched number.

Hope I have explained the idea clearly and please let me know if there is any question! Thanks in advance
Note: I am using vb.net 2008
Regards
SOLUTION
Avatar of nepaluz
nepaluz
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Sat80
Sat80

ASKER

nepaluz, thanks for ur reply,
Yes you are right, your method is good, but as you know still the search and match is one the complex issue in the question.
Actually there is just a small issue.

This will list all files in the directory C:\  from test1.txt to test9.txt
Dim FileList = Directory.GetFiles("C:\", "test?.txt") 

Open in new window


This will list ALL files begining test and ending .txt
Dim FileList = Directory.GetFiles("C:\", "test*.txt") 

Open in new window


If you want to narrow that down, I believe it can be done! (Master Gates MUST have thought of it)
Avatar of Mike Tomlinson
How big do the numbers in test?.txt go?

If they go above 9, and you want them in numeric order, then you'll need to SORT the list of files by parsing out the numbers and converting them to ints so you can have them in the right order.  (Otherwise you'd get an alphanumeric sort...1 followed by 10, will come before 2 etc...)

Will there ALWAYS be a match in the previous file?  What should happen if there isn't?


Avatar of Sat80

ASKER

nepaluz, thanks.

Idle_Mind, I appreciate your reply :)
The numbers in each line is less than 9 and might be between 4 and 5 numbers only. And about the match, most of them yes there is a match, but yes in some cases there is not match and in that case we can start from the end of file such as:

test1.txt:
1
2
3
4

Open in new window



test2.txt
5
6
7
8

Open in new window



The the output.txt file should be like this:
1
2
3
4
	/5
	/6
	/7
	/8

Open in new window


Thanks
Cool...what about the FILENAMES though?  Will the number in the FILENAME go above 9?

The reason I ask is that "test10.txt" would come before "test2.txt" using a normal sort routine.

Do you need them sorted based on the NUMBER in the FileName?
test1.txt
test2.txt
...
test10.txt
...oh, and will you match ONLY in the PREVIOUS file?
(we should only look for a match in the previous {rightmost} "column" and not any others)
Avatar of Sat80

ASKER

Hi, the file number is unknown so it might be above 10.
Yes we only match each file with the previous one so:
match test2.txt with test1.txt
match test5.txt with test4.txt

Each file will try to match to the previous one which is also the first column in the left in output.txt file
Thanks
Avatar of Sat80

ASKER

Hi Idle_Mind, sry to bother you, but I hope you can help me with this as always :) thanks.
Went out to a movie with the family.  I'll see if I can whip something up later tonight after the kids go to bed...  =)
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Sat80

ASKER

Hi Idle_Mind, hope you have enjoyed the movie with your family :)
Thank you for the code as it's working perfectly. You are really a professional programmer :) thanks again.
Regards
Avatar of Sat80

ASKER

Thanks
I was sleepy when I wrote that so please test with a wide variety of inputs and let me know if you find any bugs!  =O