locked
Pasting files into current date folder RRS feed

  • Question

  • User-220722638 posted

    I have a program that creates folders with current date. I want to paste my files into folder that uses current date as name of the folder. My code below works great but i have to provide the folder name that the files will be paste into. How can i paste files into folder that uses current date as folder name?

     

    Imports System.IO
    Imports System.Xml.Linq
    Module Module1
    
        Sub Main()
    
            Try
    
    
                For Each file In IO.Directory.GetFiles("C:\Users\Work\Documents\Morning Report\Copy\")
                    Dim File_Name As String
                    File_Name = My.Computer.FileSystem.GetName(file)
                    If File_Name.StartsWith("BUSAE") Then
                        IO.File.Copy(("C:\Users\Work\Documents\Morning Report\Copy\") & File_Name, ("C:\Users\Work\Documents\Morning Report\Paste\") & File_Name)
                    End If
                Next
                MessageBox.Show("doesnt exist")
            Catch UAEx As UnauthorizedAccessException
                Console.WriteLine(UAEx.Message)
            Catch PathEx As PathTooLongException
                Console.WriteLine(PathEx.Message)
            End Try
        End Sub
    End Module
    Wednesday, March 21, 2012 1:31 AM

Answers

  • User197322208 posted

    try

    Dim strDate as string

    strDate=DateTime.Now.ToString("yyyyMMdd")

    if not Directory.Exists("C:\Users\Work\Documents\Morning Report\Paste\" & strDate) then

    Directory.Create("C:\Users\Work\Documents\Morning Report\Paste\" & strDate)

    end if

    For Each file In IO.Directory.GetFiles("C:\Users\Work\Documents\Morning Report\Copy\")
     Dim File_Name As String
                    File_Name = My.Computer.FileSystem.GetName(file)
                    If File_Name.StartsWith("
    BUSAE") Then
                        IO.File.Copy(("
    C:\Users\Work\Documents\Morning Report\Copy\") & File_Name, ("C:\Users\Work\Documents\Morning Report\Paste\" & strDate & "\") & File_Name)
                    End If




    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 21, 2012 1:59 AM
  • User-578657687 posted

    Hi,

    It gives me Syntax error when i typed "Directory.GetFiles andF FIle.Delete"

    The both references below illustrate the both methods. Please have a check:

    http://msdn.microsoft.com/en-us/library/system.io.file.delete.aspx

    http://msdn.microsoft.com/en-us/library/07wt70x2.aspx#Y1101

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 27, 2012 11:43 PM

All replies

  • User197322208 posted

    try

    Dim strDate as string

    strDate=DateTime.Now.ToString("yyyyMMdd")

    if not Directory.Exists("C:\Users\Work\Documents\Morning Report\Paste\" & strDate) then

    Directory.Create("C:\Users\Work\Documents\Morning Report\Paste\" & strDate)

    end if

    For Each file In IO.Directory.GetFiles("C:\Users\Work\Documents\Morning Report\Copy\")
     Dim File_Name As String
                    File_Name = My.Computer.FileSystem.GetName(file)
                    If File_Name.StartsWith("
    BUSAE") Then
                        IO.File.Copy(("
    C:\Users\Work\Documents\Morning Report\Copy\") & File_Name, ("C:\Users\Work\Documents\Morning Report\Paste\" & strDate & "\") & File_Name)
                    End If




    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 21, 2012 1:59 AM
  • User-220722638 posted

    It works greate. Thanks alot. What if i want to delete the files after it get copied from the directory, how would i have to code it?

    Wednesday, March 21, 2012 11:02 PM
  • User197322208 posted

    Directory.GetFiles andF FIle.Delete

    Thursday, March 22, 2012 12:53 AM
  • User-220722638 posted

    It gives me Syntax error when i typed "Directory.GetFiles andF FIle.Delete"

    Thursday, March 22, 2012 3:57 PM
  • User-578657687 posted

    Hi,

    It gives me Syntax error when i typed "Directory.GetFiles andF FIle.Delete"

    The both references below illustrate the both methods. Please have a check:

    http://msdn.microsoft.com/en-us/library/system.io.file.delete.aspx

    http://msdn.microsoft.com/en-us/library/07wt70x2.aspx#Y1101

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 27, 2012 11:43 PM