Saturday, March 24, 2012

loop to next folder using array?

Hello

I've been working on an application in which I am uploading multiple files to a web server from a given directory. With the help of the members on this forum I have been directed to some extremely helpful information so I'm coming to the well again. I want to take my automaic upload on step further, what I want to try and do is have the code automatically jump to the next directory below it in the tree for example.

Folder

|_ folder1

|_ folder2

I am currently able to get all files from "folder1" but I want to have it automatically jump to folder2 and get those files as well. The folders will always increment by 1 but the number of folders will vary from disk to disk. The code I am using to accomplish what I am currently doing looks like this.

Sub KPacs(ByVal senderAs Object,ByVal eAs System.EventArgs)

Dim pathAsString ="d:\dicom\s00001\ser00001"

Dim targetAsString ="c:\images\"'sets current directory to the source path

My.Computer.FileSystem.CurrentDirectory = path

'loops through the files in the directoryForEach foundFileAsStringIn My.Computer.FileSystem.GetFiles(My.Computer.FileSystem.CurrentDirectory)' strips off the driectory

foundFile = Microsoft.VisualBasic.Right(foundFile,

Len(foundFile) - 25)'copies the files

My.Computer.FileSystem.CopyFile(foundFile, target & foundFile,

True)Next

This works but when it is done I want it to go to ser00002 to get all of the files from there and then to ser00003 and so on until it runs out of directories to pull files from.

Is there any reason you need to specify the name? Can't you just use a for each with a .GetFolders ?

Otherwise, you'll need to make a recursive query where the name of the folder is stored in a variable. You can just take the last character (.Substring(file.Lenght -1, 1)) and convert it to an Int and add 1, then append it back together and you have your new folder name.


Hey,

you need to get a collection of subfolders and loop throug each folder:

Dim targetAs String ="c:\images\"'gets a collection of all subdirs of a given directoryDim dirsAs Collections.ObjectModel.ReadOnlyCollection(OfString) = My.Computer.FileSystem.GetDirectories("d:\dicom\s00001\")'loops through the subdir collectionFor Each dirAs String In dirs'sets current directory to the current item from the subdir collection My.Computer.FileSystem.CurrentDirectory = dir'loops through the files in the directoryFor Each foundFileAs String In My.Computer.FileSystem.GetFiles(My.Computer.FileSystem.CurrentDirectory)' strips off the driectory foundFile = Microsoft.VisualBasic.Right(foundFile, Len(foundFile) - 25)'copies the files My.Computer.FileSystem.CopyFile(foundFile, target & foundFile,True)Next Next

FileSystem has a CopyFolder method that will copy an entire folder with subfolders... will that work?:


If copyfolder doesn't meet your needs, you can always do it manully with recursion:

1Sub KPacs(ByVal senderAs Object,ByVal eAs System.EventArgs)23Dim pathAs String ="c:\test\from"45Dim targetAs String ="c:\test\to"67 CopyFolder(path, target)89End Sub1011 Sub CopyFolder(ByVal srcFolderAs String,ByVal destFolderAs String)1213Dim newFolderAs String1415'loops through the files in the directory16For Each foundFileAs String In My.Computer.FileSystem.GetFiles(srcFolder)1718'copies the files19 My.Computer.FileSystem.CopyFile(foundFile, destFolder &"\" & System.IO.Path.GetFileName(foundFile),True)2021Next2223'loops through the files in the directory24For Each foundFolderAs String In My.Computer.FileSystem.GetDirectories(srcFolder)2526' create subfolder on target27 newFolder = destFolder &"\" & foundFolder.Substring(foundFolder.LastIndexOf("\") + 1)28 My.Computer.FileSystem.CreateDirectory(newFolder)2930'copies the files31 CopyFolder(foundFolder, newFolder)3233Next3435 End Sub

Be careful if you use fafar's solution as it will not copy subdirectories deeper than one level. This is why recursion is required.

0 comments:

Post a Comment