[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Grouping related files in a folder

Posted on 2013-01-13
22
Medium Priority
?
151 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: 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!

 
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 1600 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 1600 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

650 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