Results 1 to 10 of 10

Thread: Copying file

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2011
    Posts
    2

    Copying file

    How can i copy the files that name start with "BUSAE". Full name of the file is BUSAE_211S2.TXT. I would like to copy the file into one directory and paste it into different directory. How can i do it with vb..Below is the code i found but how can i work around the code?

    Code:
    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 ***********Exception 
                Console.WriteLine(PathEx.Message) 
            End Try 
        End Sub 
    End Module.

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Copying file

    Thread moved from the 'ASP/VBScript' forum to the 'VB.Net' (VB2002 and later) forum

  3. #3
    Fanatic Member proneal's Avatar
    Join Date
    May 2011
    Posts
    762

    Re: Copying file

    I'm not familiar with linq code.
    I could do it another way but it would be nice to see this work correctly.
    I did try it and tried different ways and still shows 0 FILES for console output.
    I am stumped and I do not like it one bit, d##n it!

    Come on gurus, where are you?

  4. #4
    Addicted Member EilaDoll's Avatar
    Join Date
    Dec 2011
    Posts
    147

    Re: Copying file

    Code:
            For Each file In IO.Directory.GetFiles(Your directory to copy from)
                Dim File_Name As String
                File_Name = My.Computer.FileSystem.GetName(file)
                If File_Name.StartsWith("BUSAE") Then
                    IO.File.Copy((location) & File_Name, (where to save) & File_Name)
                End If
            Next
    Try that, replace "location" with the address the file is already at.
    Last edited by EilaDoll; Mar 20th, 2012 at 09:33 PM. Reason: Wrote the code a little off, lmao.

  5. #5
    Fanatic Member proneal's Avatar
    Join Date
    May 2011
    Posts
    762

    Re: Copying file

    Well, yeah there is other ways but I wanted to see LINQ.
    Oh well.

  6. #6
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: Copying file

    Quote Originally Posted by proneal View Post
    Well, yeah there is other ways but I wanted to see LINQ.
    Oh well.
    Here you go
    Code:
            Dim files = From Fn As String In IO.Directory.GetFiles(Your directory to copy from) _
                        Let shortname = My.Computer.FileSystem.GetName(Fn) _
                        Where shortname.StartsWith("BUSAE") Select shortname
    
            For Each f As String In files
                 IO.File.Copy((location) & f, (where to save) & f)
            Next
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  7. #7
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008
    Location
    WY
    Posts
    1,227

    Re: Copying file

    Here's an example using method syntax and Array.ForEach rather than a For Each Next clause.

    I've also refactored out finding files with a prefix and getting the short name of a file.

    Code:
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            CopyFiles("C:\Temp\", "C:\Temp\Output\", "BUSAE")
        End Sub
    
        Private Shared Sub CopyFiles(dirName As String, destDirName As String, filePrefix As String)
            Dim shortName As Func(Of String, String) = Function(f) My.Computer.FileSystem.GetName(f)
            Dim findFilesWithPrefix As Func(Of String, Boolean) = _
                Function(f) shortName.ToString().StartsWith(filePrefix)
    
            Dim files = IO.Directory.GetFiles(dirName).Where(findFilesWithPrefix).Select(shortName).ToArray()
            Array.ForEach(files, Sub(file) IO.File.Copy(dirName & file, destDirName & file))
        End Sub

  8. #8
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: Copying file

    Cool I also attempted to conjure one with the method syntax and failed Couldn't figure out how to get it to return it with the type as String rather than that funky Iterator type.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  9. #9
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008
    Location
    WY
    Posts
    1,227

    Re: Copying file

    A couple of different ways of refactoring things for reusability. Overkill for a small example like this but might make it cleared if someone needs to do something like this in a larger project.

    Code:
        Private Sub Form2_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            CopyFiles("C:\Temp\", "C:\Temp\Output\", "BUSAE")
        End Sub
    
        Private Shared Sub CopyFiles(dirName As String, destDirName As String, filePrefix As String)
            Dim files = IO.Directory.GetFiles(dirName) _
                        .Where(FindFilesByPrefix(filePrefix)) _
                        .Select(GetFilesByShortName()).ToArray()
            Array.ForEach(files, CopyFiles(dirName, destDirName))
        End Sub
    
        Private Shared Function FindFilesByPrefix(ByVal filePrefix As String) As Func(Of String, Boolean)
            Return Function(f) My.Computer.FileSystem.GetName(f).StartsWith(filePrefix)
        End Function
    
        Private Shared Function GetFilesByShortName() As Func(Of String, String)
            Return Function(f) My.Computer.FileSystem.GetName(f)
        End Function
    
        Private Shared Function CopyFiles(ByVal dirName As String, ByVal destDirName As String) As Action(Of String)
            Return Sub(file) IO.File.Copy(dirName & file, destDirName & file)
        End Function
    Code:
        Private Shared ReadOnly GetFilesByShortName As Func(Of String, String) = Function(f) My.Computer.FileSystem.GetName(f)
        Private Shared ReadOnly FindFilesWithPrefix As Func(Of String, String, Boolean) = _
            Function(f, p) GetFilesByShortName(f).StartsWith(p)
    
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            CopyFiles("C:\Temp\", "C:\Temp\Output\", "BUSAE")
        End Sub
    
        Private Shared Sub CopyFiles(dirName As String, destDirName As String, filePrefix As String)
            Dim files = IO.Directory.GetFiles(dirName) _
            .Where(GetFiles(filePrefix)) _
            .Select(GetFilesByShortName).ToArray()
            Array.ForEach(files, CopyFiles(dirName, destDirName))
        End Sub
    
        Private Shared Function GetFiles(ByVal filePrefix As String) As Func(Of String, Boolean)
            Return Function(f) FindFilesWithPrefix(f, filePrefix)
        End Function
    
        Private Shared Function CopyFiles(ByVal dirName As String, ByVal destDirName As String) As Action(Of String)
            Return Sub(file) IO.File.Copy(dirName & file, destDirName & file)
        End Function

  10. #10
    Fanatic Member proneal's Avatar
    Join Date
    May 2011
    Posts
    762

    Re: Copying file

    Great examples that work.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width