简体   繁体   中英

How to Auto Copy text file from one folder to another folder in vb 2010 console or windows application

I want to create a program that auto copy text file from one folder to another folder . is it possible to make in windows form in vb.net ? if not what about in console apps ? i tried to search but i didn't find an answer for both. please help me i'm new to to this. I want to copy all the text file that is being save to c:folder1\\test1.text copy to c:folder2\\test1.text then test2.text,test3.text all the text file that are being put in folder1. i want to copy in folder2.
now i only have this code: it will only copy 1 specific textfile with file name test.txt.

enter code here

My.Computer.FileSystem.CopyFile("C:\CopyTo\test.txt", 
"C:\CopyHere\test.txt")

Of course! First of all we need a function that search for files.

Public Sub SearchFiles(ByVal Pattern As String, ByVal Path As String, ByVal FilesFound As ArrayList)
    FilesFound.AddRange(Directory.GetFiles(Path, Pattern))
End Sub

But where we should save the list of files? We can use a Array for it. Also we should define our output and input folder

Dim files As New ArrayList
Dim inDir As String = "input path"
Dim outDir As String = "output path"

We can now call this function like this:

SearchFiles("*.txt", inDir, files)

All .txt files in the folder are now saved in our Array List. But how we can work with it? We can now work with it like this:

Try
  For Each file As String In files
    Dim fName As String = Path.GetFileName(file)
    My.Computer.FileSystem.CopyFile(file , outDir & "\" & fName, overwrite:=False)
  Next
Catch ex As Exception
  Console.WriteLine(ex.ToString)
End Try

This will copy every .txt file that where found in our inDir to our outDir. If something goes wrong then you will see this in the console. Try it out and understand how it works :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM