[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 152
  • Last Modified:

Grouping related files in a folder

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
zachvaldez
Asked:
zachvaldez
  • 12
  • 9
2 Solutions
 
deepu chandranCommented:
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
 
CodeCruiserCommented:
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
 
zachvaldezAuthor Commented:
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
Technology Partners: 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!

 
CodeCruiserCommented:
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
 
zachvaldezAuthor Commented:
Will you be able to contribute code? Particularly when creating a folder as it loops.
0
 
CodeCruiserCommented:
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
 
zachvaldezAuthor Commented:
How is the dTable constructed?
0
 
CodeCruiserCommented:
Its the datatable filled from database.
0
 
zachvaldezAuthor Commented:
The dtable in your code was not defined.
so I didn't know how it got there
0
 
CodeCruiserCommented:
You got it now? Did you try the code?
0
 
zachvaldezAuthor Commented:
I got curlys on the dtable ? So I think dtable has to be stored somewhere in memory to be used.
0
 
zachvaldezAuthor Commented:
It's not putting inside the folder but instead adding the file to same level of the folder.
0
 
CodeCruiserCommented:
dTable needs to be declared as DataTable and filled with data from your database.
0
 
zachvaldezAuthor Commented:
that's what I thought and I did exactly that.
0
 
zachvaldezAuthor Commented:
well i created a dataset, then i created a datatable.
0
 
CodeCruiserCommented:
and ...
0
 
zachvaldezAuthor Commented:
the files do not merged to the folder...
0
 
CodeCruiserCommented:
Show the full code that you have now.
0
 
zachvaldezAuthor Commented:
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
 
CodeCruiserCommented:
Change this line

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

to

 IO.File Move(File, Folder & "\" & Path.GetFileName(File))
0
 
zachvaldezAuthor Commented:
Perfecto Code Cruiser!
0
 
zachvaldezAuthor Commented:
I like this method.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

  • 12
  • 9
Tackle projects and never again get stuck behind a technical roadblock.
Join Now