vb.net - Loop through folders and only copy files that match certain criteria -


i writing program copies files needed 1 folder another. however, want copy files fit criteria, more specifically, should copy files not in banned files, banned extensions, or banned folders list stored in array this:

public bannedextensions string() = {".bak", ".bak2", ".cry", ".max", ".psd",         "log", ".lib", "pdb", ".exp"} public bannedfolders string() = {"tools", "editor", "code", "logbackups",        "statoscope", "bintemp", "user", "rc"} public bannedfiles string() = {"editor.exe", "error.bmp", "error.dmp",         "luac.out", "tags.txt"} 

the code should move them temporary directory , zip them , save them location stored in file_name variable.

this code whole:

option strict on public class form1      'define 2 variables used tracking file name , save'     private property root_folder string     private property file_name string      'define variable dictates wether copy file     private property copy boolean      'define files need , not need'     public bannedextensions string() = {".bak", ".bak2", ".cry", ".max", ".psd", "log", ".lib", "pdb", ".exp"}     public bannedfolders string() = {"tools", "editor", "code", "logbackups", "statoscope", "bintemp", "user", "rc"}     public bannedfiles string() = {"editor.exe", "error.bmp", "error.dmp", "luac.out", "tags.txt"}      function copy_requiredfiles() integer         dim integer = 0          'searches directory , it's subdirectories files, "*" stands         'say example want search jpeg files... change "*" "*.jpg"           each filename string in io.directory.getfiles(root_folder, "*", io.searchoption.alldirectories)              copy = true              dim fname string = io.path.getfilename(filename)             dim fextension string = io.path.getextension(filename)             dim ffolder string = io.path.getdirectoryname(filename)              = 0 bannedextensions.length - 1                 if fextension = bannedextensions(i)                     richtextbox1.text = richtextbox1.text & vbnewline & "extension: " & filename                     copy = false                 end if             next             if copy = true                 = 0                 = 0 bannedfolders.length - 1                     if filename = bannedfolders(i) or bannedfolders(i).contains(filename)                         richtextbox1.text = cstr(cbool(richtextbox1.text & vbnewline & "folder: " &                         copy) = false)                     end if                 next                 if copy = true                     = 0                     = 0 bannedfiles.length - 1                         if filename = bannedfolders(i) or bannedfolders(i).contains(filename)                             richtextbox1.text = richtextbox1.text & vbnewline & "folder: " & filename                             copy = false                         end if                     next                 end if             end if             if copy = true                 'copy file selected folder'             end if         next         return 0     end function      'when browse button selecting folder selected'     private sub browse_root_click(sender object, e eventargs) handles browse_root.click         'check if folder dialogue has been opened , has been selected'         if folderbrowserdialog1.showdialog() = dialogresult.ok '             'set textbox , defined variable folder selected'             textbox1.text = folderbrowserdialog1.selectedpath             root_folder = folderbrowserdialog1.selectedpath         end if     end sub      'when browse button save file selected'     private sub button1_click(sender object, e eventargs) handles button1.click         'set options limit save type zip file'         savefiledialog1.filter = "zip files (*.zip)|*.zip"         'check dialogue box has opened succesfully'         if savefiledialog1.showdialog() = dialogresult.ok             'set text box , defined variable file selected'             textbox2.text = savefiledialog1.filename()             file_name = savefiledialog1.filename()         end if     end sub      'when user changes value of textbox update folder select from'     private sub textbox1_textchanged(sender object, e eventargs) handles textbox1.textchanged         root_folder = textbox1.text     end sub      'when user changes value of textbox update file save to'     private sub textbox2_textchanged(sender object, e eventargs) handles textbox2.textchanged         file_name = textbox2.text     end sub      private sub button2_click(sender object, e eventargs) handles button2.click         copy_requiredfiles()     end sub end class   'when browse button selecting folder selected' private sub browse_root_click(sender object, e eventargs) handles browse_root.click     'check if folder dialogue has been opened , has been selected'     if folderbrowserdialog1.showdialog() = dialogresult.ok '         'set textbox , defined variable folder selected'         textbox1.text = folderbrowserdialog1.selectedpath         root_folder = folderbrowserdialog1.selectedpath     end if end sub  'when browse button save file selected' private sub button1_click(sender object, e eventargs) handles button1.click     'set options limit save type zip file'     savefiledialog1.filter = "zip files (*.zip)|*.zip"     'check dialogue box has opened succesfully'     if savefiledialog1.showdialog() = dialogresult.ok         'set text box , defined variable file selected'         textbox2.text = savefiledialog1.filename()         file_name = savefiledialog1.filename()     end if end sub  'when user changes value of textbox update folder select from' private sub textbox1_textchanged(sender object, e eventargs) handles textbox1.textchanged     root_folder = textbox1.text end sub  'when user changes value of textbox update file save to' private sub textbox2_textchanged(sender object, e eventargs) handles textbox2.textchanged     file_name = textbox2.text end sub  private sub button2_click(sender object, e eventargs) handles button2.click     copy_requiredfiles() end sub end class 

i have couple of questions,

first of which, why when run program , print files found shouldn't copy, not show second or third if statements in loop.

second of how preserve folder structure when copying temporary file.

and third is, best way zip temporary folder.

thanks in advance, marcus

one way use custom class in list save info:

class myfile    ' things want ("preserve folder struct"),    ' need more name in textbox:    public property filename string    public property fileext string    public property pathname string     ' cant create new obj without essential info    public sub new(fname string, fext string, pname string)       filename = fname       fileext = fext       pathname = pname    end sub  end class 

or, rather recreate wheel, use system.io.fileinfo , change how iterate files. list store these things:

friend myfilelist list(of myfile) ' or friend myfilelist list(of fileinfo) 

to iterate saving list(of myfile):

dim myf myfile each filename string in io.directory.getfiles      ' if bannedfiles list(of string) instead of array:     if bannedfiles.contains(f.extension) = false          ' create new file object info           myf = new myfile(io.path.getfilename(filename),                           io.path.getextension(filename),                           io.path.getdirectoryname(filename))          ' add list of files          myfilelist.add(f)     end if      if myfilelist.contains(f) = false         ' better version of copy=true         n integer = 0 bannedfolderslist.count - 1            if f.fullpath.contains(bannedfolderslist(n)) = false                ' create new file object above                 myfilelist.add(f)              ' add folder ban            end if         next n     end if      ' repeat banned extensions  (watch out "." vs no dot)     return myfilelist             ' mine function returning                                  ' list(of t) processed elsewhere 

this more conceptual code copy , paste. since want several things result, better save them in other textbox. vs happily show in list mouse.

the folder test need work depending on whether banned string full or partial name. larger point list object tell if have stored item using .contains rather global flag. either list(of fileinfo) or list(of myfileclass) work. latter preserve more of code have.


Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -