Solved

Grouping related files in a folder

Posted on 2013-01-13
22
147 Views
Last Modified: 2013-02-12
I have txt files in my c:\submit folder but Id like to group them based on the file name. This file name is an ID but Id like to use their description . I can get the description of the  id from a table on the database.
 
In the C:\submit folder, I have the text files as

5679-A.txt
5679-B txt
5679-c.txt

3452-A.txt
3452-B.txt
3452-c.txt

... etc

ID 5679 in the table has description of Science
ID  3452 in the table  has description of Technology

The result Im looking for should be displayed as follows:

A folder for each group and its associated text files inside the folder

as follows

Science
     5679-A.txt
      5679-B.txt
      5679-C. txt
Technology
    3452-A.txt
    3452-B.txt
     3452-C.txt

What code is use to loop thru the files, grouping it to a folder?
0
Comment
Question by:zachvaldez
  • 12
  • 9
22 Comments
 
LVL 8

Expert Comment

by:deepu chandran
ID: 38773382
Hi,

Refer Below sample codes

string[] lstFile = Directory.GetFiles("c:\submit ");

http://weblogs.asp.net/israelio/archive/2004/06/23/162913.aspx
http://www.codeproject.com/Articles/10409/Recursion-using-C

-- Deepu
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38773958
So you want to create a subfolder with the description from database for given ID and then move all files with that ID into that folder?

You received help with this previously. Where have you got to with that?
0
 

Author Comment

by:zachvaldez
ID: 38774236
I got that part in the bag but this is different .
In this case,I have a series of files having the same Name in this case is the ID but  distinct because a hyphen(-) and a letter A,B,C..
.is appended. There is a description of the id in a table in Sql server which I'd like to use as subfolder name but inside this folder will be the files which still use the id like 5482-A.txt
5482-B.txt,etc..
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38774291
How many records do you have in the table which has description for IDs? Depending on that, you can take either of following approaches

1) Fill a datatable with ID and Description.
    Loop through files in the folder and
    a) Get the Description from datatable using RowFilter
    b) Create the subfolder
    c) Move files into that subfolder

2) Loop through the files in folder and
    a) Get the Description from database using a connection and command object
    b) Create the subfolder
    c) Move files into that subfolder
0
 

Author Comment

by:zachvaldez
ID: 38776260
Will you be able to contribute code? Particularly when creating a folder as it loops.
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 400 total points
ID: 38783376
For option 1

Dim ID, Folder As String
Dim Files As String() = Directory.GetFiles("path")
For Each File As String In Files
      ID = Path.GetFileName(File).Split("-")(0)
      dTable.DefaultView.RowFilter = "ID=" & ID  
      If dTable.DefaultView.Count = 1 Then
          Folder = "C:\Files\" & dTable.DefaultView.Item(0).Item("Description")
          If Directory.Exists(Folder) = False Then
              Directory.Create(Folder)
          End If
          IO.File.Move(File, Folder & Path.GetFileName(File))
      End If
Next





Note that this is untested code and you may have to tweak it.
0
 

Author Comment

by:zachvaldez
ID: 38784648
How is the dTable constructed?
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38784737
Its the datatable filled from database.
0
 

Author Comment

by:zachvaldez
ID: 38785395
The dtable in your code was not defined.
so I didn't know how it got there
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38786141
You got it now? Did you try the code?
0
 

Author Comment

by:zachvaldez
ID: 38787734
I got curlys on the dtable ? So I think dtable has to be stored somewhere in memory to be used.
0
 

Author Comment

by:zachvaldez
ID: 38789358
It's not putting inside the folder but instead adding the file to same level of the folder.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38789550
dTable needs to be declared as DataTable and filled with data from your database.
0
 

Author Comment

by:zachvaldez
ID: 38793452
that's what I thought and I did exactly that.
0
 

Author Comment

by:zachvaldez
ID: 38796638
well i created a dataset, then i created a datatable.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38800995
and ...
0
 

Author Comment

by:zachvaldez
ID: 38815361
the files do not merged to the folder...
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38818291
Show the full code that you have now.
0
 

Author Comment

by:zachvaldez
ID: 38868077
Sorry for late response, I was on vaca

In the button click..

Dim idata as new Bookmark
Dim dsLcode = idata.GetBookmarkcode
Dim spath as string = "C:\Filing\"
Dim dTable as New Datatable

Dim ID,Folder as string

Dim Files as string()=Directory.Getfiles(spath)

For each file as string in Files
  ID=Path.GetfileName(file).Spli("-")(0)
  dTable.Defaultview.Rowfilter = "UnitId=" & ID
  If dTable.Defaultview.count = 1 then
      Folder= "C:\Filing\Files"\ & dTable.DefaultView.Item("RAM")
     If Directory.Exists(Folder) = False then
        Directory.CreateDirectory(Folder)
    End If

   IO.File Move(File,Path.GetFileName(File))
End IF

Next
0
 
LVL 83

Assisted Solution

by:CodeCruiser
CodeCruiser earned 400 total points
ID: 38869173
Change this line

 IO.File Move(File,Path.GetFileName(File))

to

 IO.File Move(File, Folder & "\" & Path.GetFileName(File))
0
 

Author Comment

by:zachvaldez
ID: 38882002
Perfecto Code Cruiser!
0
 

Author Closing Comment

by:zachvaldez
ID: 38882008
I like this method.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

839 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