Sign in to follow this  
Followers 0
VAN0

Combine directories (move)

4 posts in this topic

#1 ·  Posted (edited)

Hello.

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")
EndFunc

Func move($type = 1)
    create()
    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)
    EndSwitch
EndFunc

move(1)
MsgBox(0, 1, "Move")

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

move(3)
MsgBox(0, 3, "Robocopy move")

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

c:\test\dest\

dir1.2
dir2
-dir2.2
-file1.1.txt
-file2.1.txt
-file2.2.txt
-file2.3.txt
dir3
-dir3.2
-file3.1.txt
-file3.2.txt
-file3.2.txt
file1.1.txt
file1.2.txt
file1.3.txt
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:

c:\test\dest\

dest
-dir2
--dir2.2
--file1.1.txt
--file2.1.txt
--file2.2.txt
--file2.3.txt
-dir3
--dir3.2
--file3.1.txt
--file3.2.txt
--file3.2.txt
dir1.2
file1.1.txt
file1.2.txt
file1.3.txt
Finally the third test "Robocopy move" uses external robocopy command that only available on Windows Vista and newer resulting exactly what I'm after:

c:\test\dest\

dir1.2
dir2.2
dir3.2
file1.1.txt (text4)
file1.2.txt
file1.3.txt
file2.1.txt
file2.2.txt
file2.3.txt
file3.1.txt
file3.2.txt
file3.2.txt
Any suggestions how to avoid using external commands to achieve this?

Thank you.

Edited by VAN0

Share this post


Link to post
Share on other sites



VAN0,

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. :)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

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

 

Share this post


Link to post
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)

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

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

Share this post


Link to post
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
Sign in to follow this  
Followers 0