Cut / move files (more than 2 minute old file) from one path to another path using C# and VB.Net

Last Reply on Jul 04, 2016 09:36 AM By Shashikant

Posted on Jul 04, 2016 09:03 AM

Dear All, i want to cut all files 2 minute before from the current time(more than 2 minute old file) and paste to another path

please see my code

Imports System
Imports System.Data
Imports System.Data.Sql
Imports System.Data.SqlClient
Imports System.Math
Imports System.Xml
Imports System.Configuration
Imports System.IO
Imports System.Net
Imports System.Timers
Imports System.Globalization
Public Class Form1
    
    Dim myReader As SqlDataReader
    Dim SQL As String
    Dim dr As SqlDataReader



    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


    End Sub
    Private Function fixQuotes(ByVal theString As String) As String
        fixQuotes = Replace(theString, "'", "''")
    End Function
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Call Copyinifiles()
         Timer1.Enabled = True
    End Sub

    Private Sub Copyinifiles()
        Dim FileToCopy As String
        Dim NewCopy1 As String


        Try

            Dim fileEntries As String() = Directory.GetFiles("\\192.192.1.26\f1\", "*.ini")
            For Each FileToCopy In fileEntries
                Dim ImageName = FileToCopy.Split("\")

                NewCopy1 = "\\192.192.2.22\e$\f1\" & ImageName(5)
                If (System.IO.File.Exists(NewCopy1)) = True Then
                    'MessageBox.Show("File already Exists")
                Else
                    System.IO.File.Copy(FileToCopy, NewCopy1)
                    'MessageBox.Show("File Copied")
                End If



            Next
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try

        Label1.Text = "ini Files Folder Last Updated at " & Now()
    End Sub
  
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Timer1.Interval = 120000
        Call Copyinifiles()
    End Sub
End Class

 

You are viewing reply posted by: Shashikant on Jul 04, 2016 09:36 AM.
Posted on Jul 04, 2016 09:36 AM

Please refer below code.

C#

string sourceFile = "D:\\document.xml";
string destinationFile = "D:\\test.txt";
if (File.Exists(sourceFile))
{
    FileInfo file = new FileInfo(sourceFile);
    if (file.CreationTime < DateTime.Now.AddMinutes(-2) || file.LastWriteTime < DateTime.Now.AddMinutes(-2))
    {
        System.IO.File.Move(sourceFile, destinationFile);
    }
}

VB

Dim sourceFile As String = "D:\document.xml"
Dim destinationFile As String = "D:\test.txt"
If File.Exists(sourceFile) Then
	Dim file__1 As New FileInfo(sourceFile)
	If file__1.CreationTime < DateTime.Now.AddMinutes(-2) OrElse file__1.LastWriteTime < DateTime.Now.AddMinutes(-2) Then
		System.IO.File.Move(sourceFile, destinationFile)
	End If
End If

I hope this will help you out.