Jump to content

Combine directories (move)

Recommended Posts


I'm trying move content of several folders into one. DirMove seemed like a perfect solution for speed and simplicity, however the remark

If the destination already exists and the overwrite flag is specified then the source directory will be moved inside the destination.

messes things up so bad that I can't get my brain around how to get around that nonsense.

Here is a test script:


$dir = "c:\test"
$src = "\src"
$dest = "\dest"

Func create()
    DirRemove($dir & $src, 1)
    DirRemove($dir & $dest, 1)
    DirCreate($dir & $src & "\dir1")
    DirCreate($dir & $src & "\dir1\dir1.2")
    DirCreate($dir & $src & "\dir2")
    DirCreate($dir & $src & "\dir2\dir2.2")
    DirCreate($dir & $src & "\dir3")
    DirCreate($dir & $src & "\dir3\dir3.2")

    FileWrite($dir & $src & "\dir1\file1.1.txt", "text1")
    FileWrite($dir & $src & "\dir1\file1.2.txt", "text2")
    FileWrite($dir & $src & "\dir1\file1.3.txt", "text3")
    FileWrite($dir & $src & "\dir2\file1.1.txt", "text4")
    FileWrite($dir & $src & "\dir2\file2.1.txt", "text5")
    FileWrite($dir & $src & "\dir2\file2.2.txt", "text6")
    FileWrite($dir & $src & "\dir2\file2.3.txt", "text7")
    FileWrite($dir & $src & "\dir3\file3.1.txt", "text8")
    FileWrite($dir & $src & "\dir3\file3.2.txt", "text9")
    FileWrite($dir & $src & "\dir3\file3.3.txt", "text10")

Func move($type = 1)
    Switch $type
        Case 1
            DirMove($dir & $src & "\dir1", $dir & $dest, 1)
            DirMove($dir & $src & "\dir2", $dir & $dest, 1)
            DirMove($dir & $src & "\dir3", $dir & $dest, 1)

        Case 2
            DirMove($dir & $src & "\dir1", $dir & $src & $dest, 1)
            DirMove($dir & $src & $dest, $dir, 1)

            DirMove($dir & $src & "\dir2", $dir & $src & $dest, 1)
            DirMove($dir & $src & $dest, $dir, 1)

            DirMove($dir & $src & "\dir3", $dir & $src & $dest, 1)
            DirMove($dir & $src & $dest, $dir, 1)

        Case 3
            RunWait('robocopy /mt /e /move "' & $dir & $src & '\dir1" "' & $dir & $dest &'"', "", @SW_HIDE)
            RunWait('robocopy /mt /e /move "' & $dir & $src & '\dir2" "' & $dir & $dest &'"', "", @SW_HIDE)
            RunWait('robocopy /mt /e /move "' & $dir & $src & '\dir3" "' & $dir & $dest &'"', "", @SW_HIDE)

MsgBox(0, 1, "Move")

MsgBox(0, 2, "Rename then move")

MsgBox(0, 3, "Robocopy move")

The first test "Move" after simply moving each folder from c:testsrc to c:testdest results to


Not what I was after, but it follows the remark, so fine.

The next test "Rename then move" involves first renaming each folder from c:testsrcXXX to c:testsrcdest then move that folder to c:test

The result is also not what I wanted:


Finally the third test "Robocopy move" uses external robocopy command that only available on Windows Vista and newer resulting exactly what I'm after:


file1.1.txt (text4)
Any suggestions how to avoid using external commands to achieve this?

Thank you.

Edited by VAN0
Link to comment
Share on other sites

  • Moderators


I suggest using _FileListToArrayRec to get a list of the files and then looping through the array to move them using the $FC_CREATEPATH flag. That way you create the new folder structure on the fly. :)


Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:


ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area


Link to comment
Share on other sites

Thanks for the suggestion.

I was trying to avoid moving each file individually, because it's very inefficient when it comes to several thousand files within sub directories.

For now I found that robocopy can do this at quiet decent speed (I've updated my original example)

Link to comment
Share on other sites

Melba's answer is excellent and I'm sure it's exactly what you want. I thought it also worth to mention some scripts I've created recently during a big backup. They are too rough and ready - coded on the fly - so I will describe what they do and maybe post something in the future. It's easy to create a library like this if anyone wants, and has the time, to do so.

Using hash functions and file type/name filters:

1. Grab all (or specific) files within a directory structure and move (or copy) unique files to a single folder.

    If duplicate names occur for several files containing different information, you could add the CRC32 8 digit hash to the original name - also making sure no overwrites occur.

    Delete duplicates on the fly or leave the original directory intact.

2. Compare files within two directories A and B.

    Copy all (or specific) files in directory A which are missing from directory B to a single separate folder.

    Again taking care not to duplicate or overwrite the files being copied to the new location.

I think it's most useful to have both the original directory structure intact, whilst having the means to create a resources backup folder. With Melba's functions this should be an easy task, but caution is needed when deleting or dumping files (which may be interdependent) outside their original directories. I didn't actually use Melba's code (there's absolutely nothing wrong with it), but I can appreciate some of the technical aspects of creating a flexible set of functions and the effort that goes into it, after writing these rough and ready snippets to suit my own needs.

Edited by czardas
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...