0

I need to make a program in Visual Studio that copy files from one folder to another automatically by today's date. I've this code right here that a friends gives me but I don't know how to make it work. Could someone help me pls?

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim lvCreationTime As Date
    Dim lvStr_Diretoria As String = ""

    lvStr_Diretoria = "C:\"
    Dim ficheiros() As String = IO.Directory.GetFiles(lvCreationTime)

    For Each file As String In ficheiros
        ' Do work, example
        lvCreationTime = IO.File.GetCreationTime(file)
        'Dim text As String = IO.File.ReadAllText(file)

        If DateDiff(DateInterval.Day, lvCreationTime, Now) = 0 Then
            'file to comunicate
        End If
    Next
    '
    'If Not IO.File.Exists() Then

    'End If
Steve
  • 213,761
  • 22
  • 232
  • 286
  • I would start looking at what parameters you can pass to [GetFiles](https://learn.microsoft.com/en-us/dotnet/api/system.io.directory.getfiles?msclkid=a1c44706a9c911ec8b1b74b18abb56e5&view=net-6.0). You should notice that you cannot pass a DateTime to that method. Once you have understood the problem then you can look at other methods in the [File class](https://learn.microsoft.com/en-us/dotnet/api/system.io.file?view=net-6.0) that could help you to reach your objective – Steve Mar 22 '22 at 10:22
  • ohh yeah, I've put creation time on getfiles by mistake thankss. – Hélder Costa Mar 22 '22 at 10:36
  • I did the rest like the paths and that but the programm doesn't copy the file – Hélder Costa Mar 22 '22 at 10:37
  • Take a look at : [Upload file to FTP site using VB.NET](https://stackoverflow.com/questions/8809279/upload-file-to-ftp-site-using-vb-net) – Xingyu Zhao Mar 23 '22 at 01:35

1 Answers1

0

you can use the code below

Dim folderA As String = "C:\FolderA"
Dim folderB As String = "C:\FolderB"

'read all files from folderA
Dim infoDir As New DirectoryInfo(folderA)
Dim files As FileInfo() = infoDir.GetFiles()

'use linQ to filter by date; below from Now but you can specify what you want
Dim filter As List(Of FileInfo) = (From f In files Where f.CreationTime.Date.ToShortDateString() = Now.ToShortDateString() Select f).ToList()

' and here you move from folderA to folderB
For Each i In filter
    My.Computer.FileSystem.MoveFile(i.FullName, folderB & "\" & i.Name)
Next