Solved

Grouping related files in a folder

Posted on 2013-01-13
22
144 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
 
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

762 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now