Solved

cannot get txt file and move them to new location

Posted on 2006-06-27
20
222 Views
Last Modified: 2010-04-23
   Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click

        Dim SourcePath As String = ("SourcePath")
        Dim DestPath As String = ("DestinationPath")  '<-- Get Destination path also from config
        '        Dim Fls() As String = Directory.GetFiles(SourcePath, ("*.txt"))  '<-- search for only TXT files
        Dim Fls() As String = Directory.GetFiles("SourcePath")
        For Each Fl As String In Fls
            If File.GetCreationTime(Fl) < Today.AddDays(-10) And (Fl.EndsWith(".txt")) Then
                File.Move(Fl, DestPath & Fl.Substring(Fl.LastIndexOf("\"))) '<-- and move to destination folder
            End If
        Next
    End Sub

appconfig file--
      <appSettings>
            <add key="SourcePath" value="C:\test\files" />
            <add key="DestinationPath" value="C:\test\archive" />
      </appSettings>
0
Comment
Question by:Padre_Corleone
  • 10
  • 9
20 Comments
 
LVL 11

Expert Comment

by:MacNuttin
ID: 16994107
Directory.GetFiles("SourcePath")
Does this return a string or an array of strings?
0
 
LVL 7

Author Comment

by:Padre_Corleone
ID: 16994124
Error      1      Value of type '1-dimensional array of String' cannot be converted to 'String'.      C:\Documents and Settings\a\My Documents\Visual Studio 2005\Projects\WindowsApplication1\WindowsApplication1\Form1.vb      11      36      WindowsApplication1

it doesn't work I am using .net 2005
any other suggestions?

0
 
LVL 11

Expert Comment

by:MacNuttin
ID: 16994326
Just as I thought this part is wrong ...
       Dim Fls() As String = Directory.GetFiles("SourcePath")
        For Each Fl As String In Fls

don't have a copy of VS available right now to debug but You need to dim fls as array or dataset or whatever Directory.GetFiles("SourcePath") is. Then write For Each Fl As String In Fls appropriately
0
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.

 
LVL 7

Author Comment

by:Padre_Corleone
ID: 16994350
but i believe that is what i have in my code above right?
0
 
LVL 11

Expert Comment

by:MacNuttin
ID: 16994389
Try:

 Dim Fls() = Directory.GetFiles("SourcePath")
0
 
LVL 11

Expert Comment

by:MacNuttin
ID: 16994468
Go here:
http://www.aspdot.net/aspnet/filedirhandling.aspx

 string[]files =  Directory.GetFiles(path);

Also found this at: http://dotnetjunkies.com/WebLog/dinakar/articles/16284.aspx

Private Sub ShowFiles(path as string)

Dim d() As String

d = System.IO.Directory.GetFiles(path)

Dim en As System.Collections.IEnumerator

en = d.GetEnumerator

While en.MoveNext

MsgBox(CStr(en.Current))

End While

End Sub
0
 
LVL 7

Author Comment

by:Padre_Corleone
ID: 16994507
ok now it works but i get this green underline on my AppSettings

Warning      1      'Public Shared ReadOnly Property AppSettings() As System.Collections.Specialized.NameValueCollection' is obsolete: 'This method is obsolete, it has been replaced by System.Configuration!System.Configuration.ConfigurationManager.AppSettings'      E:\etelenext\Clarient\WindowsApplication2\Form1.vb      66      36      WindowsApplication2


'''my code'''
         'Declare values
        Dim SourcePath As String = AppSettings("SourcePath") ''' green underline
        Dim DestPath As String
        'ok now it works just fine but when I use the AppSettings
        DestPath = AppSettings("DestinationPath") & Today.Day.ToString & "_" & Today.Month.ToString & "_" & Today.Year
        If Not Directory.Exists(DestPath) Then Directory.CreateDirectory(DestPath)

        'Dim DestPath As String = AppSettings("DestinationPath")  '<-- Get Destination path also from config
        Dim Fls() As String = Directory.GetFiles(SourcePath, "*.txt")  '<-- search for only TXT files
        For Each Fl As String In Fls
            If File.GetCreationTime(Fl) > Today.AddDays(-10) Then '
                File.Move(Fl, DestPath & Fl.Substring(Fl.LastIndexOf("\"))) '<-- and move to destination folder
            End If
        Next
    End Sub
0
 
LVL 11

Expert Comment

by:MacNuttin
ID: 16994605
0
 
LVL 7

Author Comment

by:Padre_Corleone
ID: 16994725
''how would I implement this in my code example?
0
 
LVL 11

Expert Comment

by:MacNuttin
ID: 16994790
If you can try to change
Dim SourcePath As String = AppSettings("SourcePath") ''' green underline
To
Dim SourcePath As NameValueCollection = ConfigurationSettings.AppSettings("SourcePath")
0
 
LVL 7

Author Comment

by:Padre_Corleone
ID: 16994831
namevaluecollection not defined erros
0
 
LVL 11

Expert Comment

by:MacNuttin
ID: 16994850
Sorry I meant To
Dim SourcePath As NameValueCollection = ConfigurationManager.AppSettings("SourcePath")
0
 
LVL 11

Expert Comment

by:MacNuttin
ID: 16994904
maybe
Imports System
Imports System.Collections
Imports System.Collections.Specialized


Dim SourcePath As New NamevalueCollection()
SourcePath = ConfigurationManager.AppSetting("SourcePath")
0
 
LVL 7

Author Comment

by:Padre_Corleone
ID: 16994961
yes that took care of the message but i still get a proble running this or should this be in my button event?


 Public Shared ReadOnly Property AppSettings() As NameValueCollection
        Get
            Dim SourcePath As NameValueCollection
            SourcePath = ConfigurationManager.AppSettings("SourcePath")

            Dim DestPath As NameValueCollection

            DestPath = ConfigurationManager.AppSettings("DestPath")

        End Get
    End Property
0
 
LVL 7

Author Comment

by:Padre_Corleone
ID: 17002713
any other suggestions to get this working?
0
 
LVL 11

Expert Comment

by:MacNuttin
ID: 17002808
Tonight I'll install VS.NET2005 and try to solve
0
 
LVL 7

Author Comment

by:Padre_Corleone
ID: 17015568
ok i found the solution.  What I need to do is add to my project a reference the system.configuration from the .net tab.  this takes care of the problem.
0
 
LVL 11

Accepted Solution

by:
MacNuttin earned 500 total points
ID: 17038912
What I didn't help you with this? If not for my comments you'd still be using AppSettings.

Imports System
Imports System.Collections
Imports System.Collections.Specialized


Dim SourcePath As New NamevalueCollection()
SourcePath = ConfigurationManager.AppSetting("SourcePath")
0
 
LVL 7

Author Comment

by:Padre_Corleone
ID: 17038971
I apologize MacNuttin you are correct i don't know what i was thinking with this, I believe the reason why I though otherwise was because I kept getting a issue with reference missing and when i added the reference I work.

Thanks for pointing that out for me and much thanks for your help!

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

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 …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

840 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