[Solved] Copy Folder Structure/Tree (not files) option in Context Menu

Discussion in 'Scripting' started by Mr.X, May 3, 2022.

  1. Mr.X

    Mr.X MDL Guru

    Jul 14, 2013
    8,563
    15,574
    270
    Is it possible to create reg tweaks + scripts able to work as an option in the right-click menu?
    I know xcopy and robocopy can do that from a cmd prompt but would be great to have it like a normal right-click copy-paste when working with folders and subfolders on Windows.

    Any help would be greatly appreciated.
     
  2. kaljukass

    kaljukass MDL Guru

    Nov 26, 2012
    2,919
    1,121
    90
    A little explanation would be needed. Do I understand correctly from the title that you only want to copy folders and don't want files in folders, ie you only want to get empty folders on new place?
    If you want to copy or move all selected folders and subfolders, etc. with all files with just one right-click to new place, then it exists in Windows by default, only beginning from Windows 7 and on all higher versions it is hidden.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  3. Mr.X

    Mr.X MDL Guru

    Jul 14, 2013
    8,563
    15,574
    270
    Correct
    Really?
    Didn't know it
     
  4. Mr.X

    Mr.X MDL Guru

    Jul 14, 2013
    8,563
    15,574
    270
    Unhide it... Please?
    So I can learn :rolleyes:
     
  5. kaljukass

    kaljukass MDL Guru

    Nov 26, 2012
    2,919
    1,121
    90
    The simplest way is make reg file and run it
    Code:
    Windows Registry Editor Version 5.00
    
    [HKEY_CLASSES_ROOT\AllFilesystemObjects\shellex\ContextMenuHandlers\CopyToFolder]
    @="{C2FBB630-2971-11D1-A18C-00C04FD75D13}"
    
    [HKEY_CLASSES_ROOT\AllFilesystemObjects\shellex\ContextMenuHandlers\MoveToFolder]
    @="{C2FBB631-2971-11D1-A18C-00C04FD75D13}"
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  6. Enthousiast

    Enthousiast MDL Tester

    Oct 30, 2009
    41,606
    74,947
    450
    When google is used anyway, you could have done that yourself:D:D
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  7. GezoeSloog

    GezoeSloog knows a guy, who knows another guy.

    Feb 10, 2012
    841
    7,159
    30
    Code:
    Windows Registry Editor Version 5.00
    
    [HKEY_CLASSES_ROOT\Directory\shell\CopyFolderTree]
    @="Copy Folder Tree"
    "Icon"="imageres.dll,153"
    
    [HKEY_CLASSES_ROOT\Directory\shell\CopyFolderTree\command]
    @="xcopy.exe \"%1\" \"%1 - Copy\"\\ /t /e"
    
    
     
  8. Mr.X

    Mr.X MDL Guru

    Jul 14, 2013
    8,563
    15,574
    270
    #8 Mr.X, May 4, 2022
    Last edited: May 4, 2022
    (OP)
    Thank you man this is exactly what I want.
    I love the auto-paste feature it has but do you think is possible to chose the paste location before it actually paste the folder tree?

    Edit: Looking at xcopy commands I think it's not possible. No command allows such function at all.
    Perhaps a script using powershell or anything else could help but it gets much more complicated. Dunno.
     
  9. Mr.X

    Mr.X MDL Guru

    Jul 14, 2013
    8,563
    15,574
    270
    Tkhanks but no. I know this since ages. @GezoeSloog solution is what I wanted and needed.
     
  10. Mr.X

    Mr.X MDL Guru

    Jul 14, 2013
    8,563
    15,574
    270
    Code:
    @="xcopy.exe \"%1\" \"%1 - Copy\"\\ /t /e"
    This line does not copy hidden or system files/objects so I added the /h parameter and it works.
    Code:
    @="xcopy.exe \"%1\" \"%1 - Copy\"\\ /t /e /h"

    Also I tried robocopy, it works too:
    Code:
    Windows Registry Editor Version 5.00
    
    [HKEY_CLASSES_ROOT\Directory\shell\CopyFolderTree]
    @="Copy Folder Tree"
    "Icon"="imageres.dll,153"
    
    [HKEY_CLASSES_ROOT\Directory\shell\CopyFolderTree\command]
    @="robocopy.exe \"%1\" \"%1 - Copy\"\\ /xf * /e"

    Also I noticed, comparing both programs on my crappy old Win7 laptop, copying a Folder Structure containing 401 folders and subfolders running
    xcopy takes 45 sec
    and
    robocopy takes 19 sec !!

    That's a huge difference.
     
  11. Mr.X

    Mr.X MDL Guru

    Jul 14, 2013
    8,563
    15,574
    270
    #11 Mr.X, May 4, 2022
    Last edited: May 6, 2022
    (OP)
    At the end I decided to keep both programs for copying a folder structure: xcopy and robocopy, just in case of need.
    So I did two reg tweaks for them.

    RoboCopy
    Add tweak
    Code:
    Windows Registry Editor Version 5.00
    
    [HKEY_CLASSES_ROOT\Directory\shell\RoboCopyFolderTree]
    @="RoboCopy Folder Tree"
    "Icon"="imageres.dll,153"
    
    [HKEY_CLASSES_ROOT\Directory\shell\RoboCopyFolderTree\command]
    @="robocopy.exe \"%1\" \"%1 - RoboCopy\"\\ /xf * /e"
    Remove tweak
    Code:
    Windows Registry Editor Version 5.00
    
    [-HKEY_CLASSES_ROOT\Directory\shell\RoboCopyFolderTree]

    Xcopy
    Add tweak
    Code:
    Windows Registry Editor Version 5.00
    
    [HKEY_CLASSES_ROOT\Directory\shell\XcopyFolderTree]
    @="Xcopy Folder Tree"
    "Icon"="imageres.dll,153"
    
    [HKEY_CLASSES_ROOT\Directory\shell\XcopyFolderTree\command]
    @="xcopy.exe \"%1\" \"%1 - Xcopy\"\\ /t /e /h"
    Remove tweak
    Code:
    Windows Registry Editor Version 5.00
    
    [-HKEY_CLASSES_ROOT\Directory\shell\XcopyFolderTree]


    Just save four codes in four different files with a .reg extension. That's it.

    Thanks and all the credit to @GezoeSloog for initial solution.
     
  12. wilenty

    wilenty MDL Senior Member

    Jan 15, 2014
    266
    475
    10
    In the first post you asked for a help:
    because you want to learn:
    so, @GezoeSloog gave you the solution:
    and then you share the solution as yours:
    Everything okay with you @Mr.X (mystery of Mexico)?
    Maybe (at least) you will mention the users who helped for you with this "case"?
     
  13. Flipp3r

    Flipp3r MDL Expert

    Feb 11, 2009
    1,848
    839
    60
    Thanks @Mr.X I've never considered using Robocopy. I've only used it in WinPE a long long time ago.
    @wilenty , geez you must be board.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  14. Mr.X

    Mr.X MDL Guru

    Jul 14, 2013
    8,563
    15,574
    270
    Yeah, and RoboCopy is quite faster than Xcopy.
    Nah more likely on his period. :tooth:
     
  15. Dark Dinosaur

    Dark Dinosaur X Æ A-12

    Feb 2, 2011
    1,734
    2,026
    60
    #15 Dark Dinosaur, May 6, 2022
    Last edited: May 6, 2022
    I don't understand why use robocopy
    From what I understand it's support mult thread and mirroring
    Normal user don't need to use mirroring, Only for backup purpose. ( I use goodsync for mirroring )
    Multi thread not worth a lot, there is bandwidth limit for SSD / hdd
    Add more holes to barrel will not make it faster :D
    You Just add more pressure on poor controller
    ( All this talking is about avarage user only )

    - - - - - - - - - - - - - - - - - - - - - - - - - -

    After lot of testing ….
    Sometimes it faster,
    sometimes not !


    Code:
    
    Source Folder :: "C:\Program Files\Microsoft Office"
    Target Folder :: "C:\Users\Admin\Desktop\Test"
    
    Process :: robocopy /e /NFL /NDL /NJH /NJS /nc /ns /np /r:0 /ia:RASHCNETO
    Start :: 16:55:18.35 --- End :: 16:55:25.37 --- Total 00:00:07.02
    
    Process :: xcopy /e /q /g /h /r /y /c
    Start :: 16:55:26.83 --- End :: 16:55:31.82 --- Total 00:00:04.99
    
    Source Folder :: "C:\Windows\System32"
    Target Folder :: "C:\Users\Admin\Desktop\Test"
    
    Process :: robocopy /e /NFL /NDL /NJH /NJS /nc /ns /np /r:0 /ia:RASHCNETO
    Start :: 16:55:33.01 --- End :: 16:55:53.23 --- Total 00:00:20.22
    
    Process :: xcopy /e /q /g /h /r /y /c
    Start :: 16:55:58.04 --- End :: 16:56:33.16 --- Total 00:00:35.12
    
    Source Folder :: "C:\Windows\SysWOW64"
    Target Folder :: "C:\Users\Admin\Desktop\Test"
    
    Process :: robocopy /e /NFL /NDL /NJH /NJS /nc /ns /np /r:0 /ia:RASHCNETO
    Start :: 16:56:37.06 --- End :: 16:56:44.63 --- Total 00:00:07.57
    
    Process :: xcopy /e /q /g /h /r /y /c
    Start :: 16:56:45.82 --- End :: 16:56:51.23 --- Total 00:00:05.41
    
    Code:
    @cls
    @echo off
    setlocal enabledelayedexpansion
    
    set Source_Folder="C:\Program Files\Microsoft Office"
    Set Target_Folder="%~dp0Test"
    call :Test
    
    set Source_Folder="C:\Windows\System32"
    Set Target_Folder="%~dp0Test"
    call :Test
    
    set Source_Folder="C:\Windows\SysWOW64"
    Set Target_Folder="%~dp0Test"
    call :Test
    
    goto :eof
    
    :CalcDiff
    rem "Lean and Mean" TIMER with Regional format, 24h and mixed input support
    rem https://stackoverflow.com/a/43968482
    
    set timer_set=%1
    set timer_end=%2
    for /f "tokens=1-6 delims=0123456789" %%i in ("%timer_end%%timer_set%") do (set CE=%%i&set DE=%%k&set CS=%%l&set DS=%%n)
    set "TE=!timer_end:%DE%=%%100)*100+1!"     & set "TS=!timer_set:%DS%=%%100)*100+1!"
    set/A "T=((((10!TE:%CE%=%%100)*60+1!%%100)-((((10!TS:%CS%=%%100)*60+1!%%100)" & set/A "T=!T:-=8640000-!"
    set/A "cc=T%%100+100,T/=100,ss=T%%60+100,T/=60,mm=T%%60+100,hh=T/60+100"
    set "value=!hh:~1!%CE%!mm:~1!%CE%!ss:~1!%DE%!cc:~1!" & if "%~2"=="" echo/!value!
    set "_tdiff=%value%" & set "timer_set=" & goto :eof
    goto :eof
    
    :Test
    echo:
    echo Source Folder :: %Source_Folder%
    echo Target Folder :: %Target_Folder%
    
    if exist %Target_Folder% 2>nul rd/s/q %Target_Folder%
    if not exist %Target_Folder% md %Target_Folder%
    echo:&echo Process :: robocopy /e /NFL /NDL /NJH /NJS /nc /ns /np /r:0 /ia:RASHCNETO
    set "pStart=%time: =0%"
    >nul 2>&1 robocopy %Source_Folder% %Target_Folder% /e /NFL /NDL /NJH /NJS /nc /ns /np /r:0 /ia:RASHCNETO
    set "pEnd=%time: =0%"
    call :CalcDiff !pStart! !pEnd!
    echo Start :: !pStart! --- End :: !pEnd! --- Total !_tdiff!
    
    if exist %Target_Folder% 2>nul rd/s/q %Target_Folder%
    if not exist %Target_Folder% md %Target_Folder%
    echo:&echo Process :: xcopy /e /q /g /h /r /y /c
    set "pStart=%time: =0%"
    >nul 2>&1 xcopy %Source_Folder% %Target_Folder% /e /q /g /h /r /y /c
    set "pEnd=%time: =0%"
    call :CalcDiff !pStart! !pEnd!
    echo Start :: !pStart! --- End :: !pEnd! --- Total !_tdiff!
    
    if exist %Target_Folder% rd/s/q %Target_Folder%
    goto :eof
    
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  16. Mr.X

    Mr.X MDL Guru

    Jul 14, 2013
    8,563
    15,574
    270
    Cause when researching I stumbled with RoboCopy being capable of copying folder structure/tree like Xcopy does. Then I found it is faster doing such job than Xcopy.

    Btw this thread is about copying folder structure/tree only not folders with files within.
     
  17. Mr.X

    Mr.X MDL Guru

    Jul 14, 2013
    8,563
    15,574
    270
  18. Carlos Detweiller

    Carlos Detweiller Emperor of Ice-Cream

    Dec 21, 2012
    5,441
    5,867
    180
    Just out of interest: How did you test it? Testing one after the other without rebooting in between will invalidate the test as disk caches come into play. Caches have a huge impact.
     
  19. AveYo

    AveYo MDL Expert

    Feb 10, 2009
    1,782
    5,244
    60
    No matter how you test it, robocopy should always be faster because it skips some of the checks for ACLs. It does it even better when explicitly using option /B (or /ZB) - there's no other built-in way able to use Backup mode (nt.dll) like that.
    Unfortunately, robocopy has a fundamental flaw with soft/hard links, so it's quite dangerous to be used with windows stuff or even files in the user profile directory where such soft/hard links are present (even more so in windows 11).
    Data-loss dangerous, overwriting / deleting source and target files instead of just the soft/hard links themselves (the options to skip links are useless here). Else it would be the gold standard for file copy (and deletion via /MIR) under windows.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  20. Dark Dinosaur

    Dark Dinosaur X Æ A-12

    Feb 2, 2011
    1,734
    2,026
    60
    #20 Dark Dinosaur, May 6, 2022
    Last edited: May 6, 2022
    @BAU about '/ZB' option
    made for poor network connection, if I am not wrong.

    Edit.
    - robocopy not reliable as xcopy. [didn't copy all files - C:\Windows\System32]
    - I have 2 Sec less in robocopy, in each raw :)
    Code:
    
    Source Folder :: C:\Program Files\Microsoft Office
    Target Folder :: C:\Users\Admin\Desktop\Test
    
    Process            :: xcopy
    Args               :: /e /q /g /h /r /y /c
    Total time         :: 00:00:06.77
    Source Folder Size :: 2335.84457969666
    Target Folder Size :: 2352.12826347351
    
    Process            :: robocopy
    Args               :: /e /NFL /NDL /NJH /NJS /nc /ns /np /r:0 /w:0 /ia:RASHCNETO /ZB
    Total time         :: 00:00:03.81
    Source Folder Size :: 2335.84457969666
    Target Folder Size :: 2352.12826347351
    
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    
    Source Folder :: C:\Windows\Microsoft.NET
    Target Folder :: C:\Users\Admin\Desktop\Test
    
    Process            :: xcopy
    Args               :: /e /q /g /h /r /y /c
    Total time         :: 00:00:04.22
    Source Folder Size :: 584.37485408783
    Target Folder Size :: 584.37485408783
    
    Process            :: robocopy
    Args               :: /e /NFL /NDL /NJH /NJS /nc /ns /np /r:0 /w:0 /ia:RASHCNETO /ZB
    Total time         :: 00:00:02.10
    Source Folder Size :: 584.37485408783
    Target Folder Size :: 584.37485408783
    
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    
    Source Folder :: C:\Windows\System32
    Target Folder :: C:\Users\Admin\Desktop\Test
    
    Process            :: xcopy
    Args               :: /e /q /g /h /r /y /c
    Total time         :: 00:00:33.05
    Source Folder Size :: 7783.73624229431
    Target Folder Size :: 7618.03702354431
    
    Process            :: robocopy
    Args               :: /e /NFL /NDL /NJH /NJS /nc /ns /np /r:0 /w:0 /ia:RASHCNETO /ZB
    Total time         :: 00:00:17.81
    Source Folder Size :: 7783.73624229431
    Target Folder Size :: 6104.65696144104
    
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    
    Source Folder :: C:\Windows\SysWOW64
    Target Folder :: C:\Users\Admin\Desktop\Test
    
    Process            :: xcopy
    Args               :: /e /q /g /h /r /y /c
    Total time         :: 00:00:06.10
    Source Folder Size :: 1111.45311737061
    Target Folder Size :: 1111.45311737061
    
    Process            :: robocopy
    Args               :: /e /NFL /NDL /NJH /NJS /nc /ns /np /r:0 /w:0 /ia:RASHCNETO /ZB
    Total time         :: 00:00:04.08
    Source Folder Size :: 1111.45311737061
    Target Folder Size :: 1111.45311737061
    
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    
    Press any key to continue . . .
    
     

    Attached Files:

    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...