Here Is my code
Public Sub MoveAllFolders(ByVal fromPathInfo As DirectoryInfo, ByVal toPath As String)
Dim toPathInfo = New DirectoryInfo(toPath)
If (Not toPathInfo.Exists) Then
toPathInfo.Create()
End If
'move all folders
For Each dir As DirectoryInfo In fromPathInfo.GetDirectories()
dir.MoveTo(Path.Combine(toPath, dir.Name))
Next
End Sub
MoveAllFolders(“D:\Users\TheUser!\Desktop\dd”, “D:\Users\TheUser!\Desktop\dd\Folders)
My goal is to move all folder inside a folder into a folder named Folders.
so If I do it on desktop all the folders in desktop will go to “Folders”
but I get an error “The process cannot access the file because it is being used by another process.”
so this code can’t work this way, so is there any way to do what I wanna do?
Thanks alot!
You are moving your target-directoy into itself.
You could check if the destination-path contains the source-directory’s
FullName.But this method would be quite hacky. Consider a folder ‘”D:\abc1’ and a folder ‘”D:\abc2’.
Containswould returntruein this case even if the folder “abc1” and “abc2” are not the same.This should work better:
You could use this function in this way:
Edit: updated according to your last comment (untested).