12 Replies - 2811 Views - Last Post: 15 April 2014 - 06:09 PM Rate Topic: -----

#1 sensizbenlik  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 26
  • Joined: 14-April 14

how to move all files in folder another location

Posted 15 April 2014 - 01:34 PM

Private Sub form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        Dim table As New DataTable
        table.Columns.Add("ZipCode")
        table.Columns.Add("State")
        table.Columns.Add("City")
        Dim directory = "c:\yeni"
        Dim dosyalar() As System.IO.FileInfo
        Dim dirinfo As New System.IO.DirectoryInfo(directory)
        dosyalar = dirinfo.GetFiles("*.csv", IO.SearchOption.AllDirectories)

        For Each dosya In dosyalar
            ListBox1.Items.Add(dosya)
            Console.WriteLine("File: " + dosya.FullName)

        Next


        For Each f As FileInfo In dosyalar


36:         Try
                Dim connect As New SqlConnection("Data Source=(localdb)\Projects;Initial Catalog=srkn;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False")

2:
                If connect.State = ConnectionState.Closed Then connect.Open()
4:
                Dim parser As New FileIO.TextFieldParser(f.FullName)
                parser.Delimiters = New String() {";"}
                parser.HasFieldsEnclosedInQuotes = True
                parser.TrimWhiteSpace = True
                parser.ReadLine()
                Do Until parser.EndOfData = True
                    table.Rows.Add(parser.ReadFields())
                Loop
                Dim Sql As String = "INSERT INTO atblZipCode (ZipCode,State,City) VALUES (@zipcode, @state,@city)"
                Dim SqlconnectionString As String = "Data Source=(localdb)\Projects;Initial Catalog=srkn;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False;"
                Using connection As New SqlClient.SqlConnection(SqlconnectionString)

                    Dim cmd As New SqlClient.SqlCommand(Sql, connection) ' create command objects and add parameters
                    With cmd.Parameters
                        .Add("@zipcode", SqlDbType.VarChar, 15, "ZipCode")
                        .Add("@state", SqlDbType.VarChar, 20, "State")
                        .Add("@city", SqlDbType.VarChar, 30, "City")
                    End With

                    Dim adapter As New SqlClient.SqlDataAdapter()
                    adapter.InsertCommand = cmd

                    '--Update the original SQL table from the datatable
                    Dim iRowsInserted As Int32 = _
                    adapter.Update(table)


           
                End Using   Finally

            End Try
        Next
End Sub end class



Quote

i write this code to import all csv in folder(C:\yeni).every day new csv files adding to folder automaticly and so, i want to move the older csv files to another folder (C:\arsiv etc.) can u give me an idea how to do that ?

This post has been edited by modi123_1: 15 April 2014 - 01:43 PM
Reason for edit:: use the 'code' button in the editor and not the inline.


Is This A Good Question/Topic? 0
  • +

Replies To: how to move all files in folder another location

#2 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1726
  • View blog
  • Posts: 5,704
  • Joined: 25-September 09

Re: how to move all files in folder another location

Posted 15 April 2014 - 01:43 PM

The File Class of System.IO Namespace has a Move Method
Was This Post Helpful? 0
  • +
  • -

#3 sensizbenlik  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 26
  • Joined: 14-April 14

Re: how to move all files in folder another location

Posted 15 April 2014 - 01:53 PM

Quote

i try this http://msdn.microsof...(v=vs.110).aspx .there are 4 csv files but only get 3 of them .then i delete one csv file.then get 2 csv from 3 csv files in folder

Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13568
  • View blog
  • Posts: 54,136
  • Joined: 12-June 08

Re: how to move all files in folder another location

Posted 15 April 2014 - 01:57 PM

You don't need to surround your text with the quote tags.

Have you looked at the IO.Directory's "get files" method? You know, the one that gives you an array of strings for all the files in the directory?

http://msdn.microsof...=vs.110%29.aspx
Was This Post Helpful? 1
  • +
  • -

#5 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1726
  • View blog
  • Posts: 5,704
  • Joined: 25-September 09

Re: how to move all files in folder another location

Posted 15 April 2014 - 02:01 PM

Yesterday I gave you a tip on getting all the files in a folder for your parser to move to your database, using that same technique, you should be able to also use that same concept to fill the Move arguments as needed. You will of course, need to supply the path and append the filenames accordingly. As far as what you don't move. Well, that is something you will need to check. Personally if this is to automate the process so that old records aren't parsed into your database, I would first move any existing files that have been parsed, move in the new file to be parsed and repeat the process as needed.
Was This Post Helpful? 1
  • +
  • -

#6 sensizbenlik  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 26
  • Joined: 14-April 14

Re: how to move all files in folder another location

Posted 15 April 2014 - 02:04 PM

Quote

in my code i get all csv from a folder then add them to my datatable .a system is getting every day csv input to folder. so when i run the program what csv files are in folder ,copy them to database.but i dont want use the used csv files in the next runnig so i want to move them to another folder .

Was This Post Helpful? 0
  • +
  • -

#7 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13568
  • View blog
  • Posts: 54,136
  • Joined: 12-June 08

Re: how to move all files in folder another location

Posted 15 April 2014 - 02:08 PM

Okay.. then move them after you save them.
Was This Post Helpful? 1
  • +
  • -

#8 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1726
  • View blog
  • Posts: 5,704
  • Joined: 25-September 09

Re: how to move all files in folder another location

Posted 15 April 2014 - 03:30 PM

sensizbenlik, please keep your questions in the forum. The PM system is used for personal messages, not programming help. We like to keep these question in the forums so that others may learn from your solution.

You sent this code to me:
My.Computer.FileSystem.MoveDirectory(f.DirectoryName, "C:\aaa")


I have to ask why you are not using the method of the link I gave earlier?

modi123_1's advice is sound, instead of trying to move all but 1 file, move all files to the new folder after the parsing is done. This way, you build a new file, it is the only file in the folder, parse it and then your code should move it to the backup folder leaving your parsing folder empty. Now you will not duplicate parsing any csv file to your database.

With the method of File.Move, it is nothing more than passing the f.FullName for the source and then you would specify a "<drive>:\<folder>\" and append f.Name to the destination string.

I haven't looked at FileSystem.MoveDirectory but with Microsoft's naming conventions, it leads me to believe that this is for moving a folder and it's contents which doesn't sound like a solution for what you want.
Was This Post Helpful? 0
  • +
  • -

#9 sensizbenlik  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 26
  • Joined: 14-April 14

Re: how to move all files in folder another location

Posted 15 April 2014 - 04:05 PM

thnks for replying. do you mean ?
  For Each f As FileInfo In dosyalar
  Dim path As String = "c:\yeni\f.name"
        Dim path2 As String = "c:\aaa\f.name"

Try
 If File.Exists(path2) Then

File.Move(path, path2)
End If
end try




Was This Post Helpful? 0
  • +
  • -

#10 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1726
  • View blog
  • Posts: 5,704
  • Joined: 25-September 09

Re: how to move all files in folder another location

Posted 15 April 2014 - 04:13 PM

OK, assuming you're using the For Each from Yesterday's code, then it only pulls the files in the folder, I don't see a reason to use If File.Exists

path2 is wrong unless you want to always literally name the file "f.name" (I don't think that's what you want to do). Instead you need to append f.name to "c:\aaa\"
Was This Post Helpful? 1
  • +
  • -

#11 sensizbenlik  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 26
  • Joined: 14-April 14

Re: how to move all files in folder another location

Posted 15 April 2014 - 04:50 PM

i do what you say but it doesnt work :(
Was This Post Helpful? 0
  • +
  • -

#12 sensizbenlik  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 26
  • Joined: 14-April 14

Re: how to move all files in folder another location

Posted 15 April 2014 - 05:57 PM

when i add end of the parse code

connect.close

then

i can move the folder but only one of csv parse into datatable
Was This Post Helpful? 0
  • +
  • -

#13 sensizbenlik  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 26
  • Joined: 14-April 14

Re: how to move all files in folder another location

Posted 15 April 2014 - 06:09 PM

i do it i put my code after finally next then it work thanks.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1