0

I have a list of files that I want to move from one folder to another location and retain the folder structure in the new location for example I have the following files 1.txt 2.txt 3.txt located as follows

c:\1\1.txt
c:\2\2.txt
c:\3\3.txt

I would like to move the item to d:\1\1.txt and d:\2\2.txt etc...

What is the simple way to do that? I have over than 100k files that need to be moved from the certain folders in the original location to another location while keep the structure

I currently have the list of the items that needs to be moved

  • 5
    Use robocopy. It's built for tasks such as this. – squillman Apr 29 '14 at 2:46
  • 2
    Yup, don't go overboard trying to do everything in powershell just because powershell has the ability to do something. – Zoredache Apr 29 '14 at 2:48
5

Certainly possible with some PowerShell magic, but I think the best thing you can do is use Robocopy for such a task. It is built for that, super-fast (multithreaded), has a lot of error reporting and stuff built-in, and it won't let you down.

1

If you want a Powershell solution, you can try something like this:

# Load folder structure into memory
$folder = "C:\1\"
$files = gci $folder -r  

# Loop through each file in the folder structure
foreach ($file in $files)
{
    # Declare new folder structure by name
    $newpath = ($file.path -replace "^.","D")  # Replace C with D

    # Create the folder if it doesn't exist
    if (!(test-path $newpath)) {mkdir $newpath -f}

    # Copy the file to the new path
    cp $file $newpath$file.name -force
}

It should be a fairly efficient operation, since it only contains a few simple commands.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy