locked
Copy files RRS feed

  • Question

  • User-220722638 posted

    Can i copy files into folders without providing full name of the file? Different files will be distributed into different folder, my files have current date at the end, it also has split before the date. I would like to copy the files name before the split. Here is an example

    Name of my file is "BUSAE_CurrentDate" which is same as BUSAE_031312.

    This is what i am doing now and it works but i would like to copy file without providing full name of the file.

    My.Computer.FileSystem.CopyFile( _
        "C:\data\BUSAE_031312.txt", _
        "C:\transfer\BUSAE_031312.txt", _
        FileIO.UIOption.AllDialogs, _
        FileIO.UICancelOption.DoNothing)

    Tuesday, March 13, 2012 4:00 PM

Answers

  • User-220722638 posted

    So finally i found a way to resolve it. This is what i came up with..It copies file name that StartWith BUSAE and paste it into different directory. If anyone is trying to resolve similar problem you can use my code below.

     

    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("Congrats")
            Catch UAEx As UnauthorizedAccessException
                Console.WriteLine(UAEx.Message)
            Catch PathEx As PathTooLongException
                Console.WriteLine(PathEx.Message)
            End Try
        End Sub
    End Module
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 21, 2012 1:17 AM

All replies

  • User2097818059 posted

    You just need to generate the filenames:

    dim dataFileName as String  = String.Format("C:\data\BUSAE_{0:ddMMyyyy}.txt", Now)
    dim transferFileName as String = String.Format("C:\transfer\BUSAE_{ddMMyyyy}.txt", Now)
    My.Computer.FileSystem.CopyFile( _
        dataFileName, _
        transferFileName, _
        FileIO.UIOption.AllDialogs, _
        FileIO.UICancelOption.DoNothing)
    Tuesday, March 13, 2012 5:35 PM
  • User-220722638 posted

    Instead of date format can i use any other format? what if it was BUSAE_313S1, how can i get around that?

    Tuesday, March 13, 2012 6:00 PM
  • User2097818059 posted

    You need to have some way to calculate that. If S1 is just sometimes at the end then you could search for that file in your directory and copy it using the found name.

    You can use FileInfo.EnumerateFiles

    http://msdn.microsoft.com/en-us/library/dd413343.aspx

    Tuesday, March 13, 2012 6:13 PM
  • User-220722638 posted
    BUSAE_313S1 the S1 is just some at the end but 313 is the month and the day. It gets change every day, if I ran the report today it will display, BUSAE_314S1
    Wednesday, March 14, 2012 12:28 PM
  • User2097818059 posted

    1. Use FileInfo.EnumerateFiles in the data directory that start with BUSAE_{0:ddMM}*. You will need to lookup the exact syntax.

    2. Enumerate thru the found files and copy each one into your target directory.

    Also, If you want to avoid copying twice you need to delete the original, rename it or record it somewhere. Deleting being the easiest. but dont delete until copy is successfull.

    Wednesday, March 14, 2012 12:42 PM
  • User2097818059 posted

    Here is a good example of it on MSDN

    http://msdn.microsoft.com/en-us/library/dd413343.aspx

    Wednesday, March 14, 2012 12:42 PM
  • User-220722638 posted

    How do i work around this code? I want to be able to copy the file that StartWith "BUSAE" and paste it into different directory. Thanks for your help so far.

     
     
    Imports System.IO
    Imports System.Xml.Linq
    Module Module1
    
        Sub Main()
    
            Try
                Dim files = From chkFile In Directory.EnumerateFiles("c:\copy\", "*.txt", _
                                                     SearchOption.AllDirectories)
                            From line In File.ReadLines(chkFile)
                            Where line.StartsWith("BUSAE")
                            Select New With {.curFile = chkFile, .curLine = line}
    
                For Each f In files
                    Console.WriteLine("{0}\t{1}", f.curFile, f.curLine)
                Next
                Console.WriteLine("{0} files found.", _
                  files.Count.ToString())
            Catch UAEx As UnauthorizedAccessException
                Console.WriteLine(UAEx.Message)
            Catch PathEx As PathTooLongException
                Console.WriteLine(PathEx.Message)
            End Try
        End Sub
    End Module
    Sunday, March 18, 2012 2:36 PM
  • User-220722638 posted

    So finally i found a way to resolve it. This is what i came up with..It copies file name that StartWith BUSAE and paste it into different directory. If anyone is trying to resolve similar problem you can use my code below.

     

    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("Congrats")
            Catch UAEx As UnauthorizedAccessException
                Console.WriteLine(UAEx.Message)
            Catch PathEx As PathTooLongException
                Console.WriteLine(PathEx.Message)
            End Try
        End Sub
    End Module
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 21, 2012 1:17 AM