Solved

Grouping related files in a folder

Posted on 2013-01-13
22
145 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

895 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

14 Experts available now in Live!

Get 1:1 Help Now