|
-
Mar 20th, 2012, 03:49 PM
#1
Thread Starter
New Member
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.
-
Mar 20th, 2012, 05:00 PM
#2
Re: Copying file
Thread moved from the 'ASP/VBScript' forum to the 'VB.Net' (VB2002 and later) forum
-
Mar 20th, 2012, 07:07 PM
#3
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?
-
Mar 20th, 2012, 09:27 PM
#4
Addicted Member
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.
-
Mar 21st, 2012, 07:01 AM
#5
Re: Copying file
Well, yeah there is other ways but I wanted to see LINQ.
Oh well.
-
Mar 21st, 2012, 09:55 AM
#6
Re: Copying file
 Originally Posted by proneal
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
-
Mar 21st, 2012, 11:03 AM
#7
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
-
Mar 21st, 2012, 11:13 AM
#8
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.
-
Mar 21st, 2012, 11:32 AM
#9
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
-
Mar 21st, 2012, 03:10 PM
#10
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|