VB implementation of recursive copy files and search file code sharing _vb.net

Source: Internet
Author: User

The ability to make a copy folder in a program is easy to write with recursion. Then I want someone (who knows it) to implement a similar, seemingly not smooth, recursive code that throws out the Replication folder:

 public Shared Sub copydirectory (source As String, destination as String) If Directory.ex ists (destination) = False Then Try directory.createdirectory (destination) Catch ex as Exception Logger . Logerror (Logger.SourceType.Application, "Copy Build Process:cannot Create folder:" & destination) return E nd Try end If to each paths as String in directory.getdirectories (source) copydirectory (Paths, Path.Combine (DE Stination, Paths. Substring (Paths. Lastindexofany ({"" C, "/" C}) + 1)) Next for each files as String in Directory.GetFiles (source) Try file.c opy (Files, path.combine, destination, files. Substring (Files. Lastindexofany ({"C,"/"C}) + 1"), True) _copiedfiles + = 1 Catch ex as Exception Logger.logerror (logger.so Urcetype.application, "Copy build process:cannot Copy file: & Files" End Try Next end Sub 

Recursive programs are very simple and beautiful, right? Then I wrote a way to search the file in a folder, and it was recursive, so I threw it out here:

' <summary> ' Search the specified file in the folder and its sub folders and return their full path name.
Empty string if not found. ' </summary> ' <param name= ' fileName ' >the file to search (no folder) .</param> ' &LT;REMARKS&GT;&L t;/remarks> public Shared Function searchfile (folder As String, FileName as String) As String If directory.exists (fol  Der) = False Then return String.Empty fileName = FileName.Trim.ToLower If filename.indexofany ({"C,"/"C}) >=  0 Then filename = getfilename (filename) End If Dim list () as String = Directory.GetFiles (folder) for I as Integer = 0 to list. GetUpperBound (0) If GetFileName (list (i)).
  
  Trim.tolower = fileName Then return list (i) Next Dim directories () as String = directory.getdirectories (folder) For I as the Integer = 0 to directories. GetUpperBound (0) Dim return_file as String = searchfile (Directories (i), fileName) If return_file. Length > 0 Then return return_filE Next return String.Empty end Function 

GetFileName, I wrote it myself. A way to remove a path that leaves only the file name and extension.

These two pieces of code are too simple, so I think there is no place to explain (in fact, is ready to leave the work).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.