Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6080333
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:02:56+00:00 2026-05-23T11:02:56+00:00

Here Is my code Public Sub MoveAllFolders(ByVal fromPathInfo As DirectoryInfo, ByVal toPath As String)

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T11:02:57+00:00Added an answer on May 23, 2026 at 11:02 am

    You are moving your target-directoy into itself.

    You could check if the destination-path contains the source-directory’s FullName.

    If Not toPath.Contains(fromPathInfo.FullName) Then
        dir.MoveTo(IO.Path.Combine(toPath, dir.Name))
     End If
    

    But this method would be quite hacky. Consider a folder ‘”D:\abc1’ and a folder ‘”D:\abc2’. Contains would return true in this case even if the folder “abc1” and “abc2” are not the same.

    This should work better:

    Public Sub MoveAllFolders(ByVal fromDir As IO.DirectoryInfo, ByVal toDir As IO.DirectoryInfo, Optional ByVal excludeList As List(Of String) = Nothing)
        If (Not toDir.Exists) Then
            toDir.Create()
        End If
        'move all folders
        For Each dir As IO.DirectoryInfo In fromDir.GetDirectories()
            Dim targetPath = IO.Path.Combine(toDir.FullName, dir.Name)
            If Not toDir.FullName = dir.FullName _
                AndAlso Not IsParentDirectory(toDir, dir) _
                AndAlso Not IO.Directory.Exists(targetPath) _
                AndAlso (excludeList Is Nothing _
                  OrElse Not excludeList.Contains(dir.FullName, StringComparer.InvariantCultureIgnoreCase)) Then
                Try
                    dir.MoveTo(targetPath)
                Catch ioEx As IO.IOException
                    'ignore this directory'
                Catch authEx As UnauthorizedAccessException
                    'ignore this directory'
                Catch ex As Exception
                    Throw
                End Try
            End If
        Next
    End Sub
    
    Public Shared Function IsParentDirectory(ByVal subDir As IO.DirectoryInfo, ByVal parentDir As IO.DirectoryInfo) As Boolean
        Dim isParent As Boolean = False
        While subDir.Parent IsNot Nothing
            If subDir.Parent.FullName = parentDir.FullName Then
                isParent = True
                Exit While
            Else
                subDir = subDir.Parent
            End If
        End While
        Return isParent
    End Function
    

    You could use this function in this way:

    Dim excludePathList As New List(Of String)
    excludePathList.Add("C:\Temp\DoNotMoveMe1\")
    excludePathList.Add("C:\Temp\DoNotMoveMe2\")
    MoveAllFolders(New IO.DirectoryInfo("C:\Temp\"), New IO.DirectoryInfo("C:\Temp\temp-sub\"), excludePathList)
    

    Edit: updated according to your last comment (untested).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In the following code: public class A { public A():this(null){} public A(string b){/*code here*/}
Here is my code : public static void main(String[] args) { // System.setProperty( //
here is my example code: Public Class Parent Private _TestProperty As String Private WithEvents
Here is a sample code: public class TestIO{ public static void main(String[] str){ TestIO
Here's the code snippet: public static void main (String[]arg) { char ca = 'a'
Here's my binding source object: Public Class MyListObject Private _mylist As New ObservableCollection(Of String)
Given the following code Public Shared Sub DoAsyncAction() Using asmxProxy As New MyAsmxServiceProxy() AddHandler
So here is my code: public MyClass (int y) { super(y,x,x); //some code }
I have this class called SiteAsyncDownload.cs Here's the code: public class SiteAsyncDownloader { WebClient
Here's the sample code: public static void col (int n) { if (n %

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.