简体   繁体   中英

How to merge two folders including subfolders in batch

Context

I'm trying to make a batch script that can be thrown into a pre-existing folder and upgrade it. Imagine a folder that looks like this:

  • domfolder
    • LICENSE
    • merge_new_stuff.bat
    • words.txt (has 6 words)
    • awesomefolder
      • garfield.png
    • upgrade_assets
      • upgradeinfo.txt
      • new_files
        • words.txt (has 7 words)
        • awesomefolder
          • odie.png
          • awesomest
            • MONKEY.txt
        • subfolder
          • subsubfolder
            • numbers.txt

The sole purpose of merge_new_stuff.bat is to take everything inside the new_files folder, and bring it into domfolder, replacing any pre-existing files like words.txt, leaving anything else alone, and getting rid of the upgrade_assets folder. So if everything goes right, the folder structure should like this after running:

  • domfolder
    • LICENSE
    • merge_new_stuff.bat
    • words.txt (has 7 words)
    • awesomefolder
      • garfield.png
      • odie.png
      • awesomest
        • MONKEY.txt
    • subfolder
      • subsubfolder
        • numbers.txt

What I've Tried

First instinct is to just use a simple move command: move /y upgrade_assets\new_files\*
That upgrades words.txt, but it totally ignores the folders. Only part of what I want.
I tried a for loop to see if it'd be different somehow, same results:
for %%A in (upgrade_assets\new_files\*) do move /y %%A

Next, I tried looping through dir results alongside the basic move:
for /f "tokens=*" %%A in ('dir /b /s /a:d "upgrade_assets\new_files"') do move /y "%%A\*.*" "%~dp0%%~nxA"
This almost worked, properly moving odie.png and subfolder\subsubfolder\numbers.txt. But once it tries to move MONKEY.txt, it... decides to make a file called "awesomest" that has the contents of MONKEY.txt? So clearly, it doesn't handle going down pre-existing folders, which is definitely not helpful.

Looking around, I only found one thread that seems close to what I want. Most of the replies mention xcopy/robocopy, but I want to avoid the copy/delete method as much as possible considering the potential size of updates. And when I changed the accepted answer to fit what I'm doing, it still had issues going more than one folder deep.

for /d /r upgrade_assets\new_files %%A in (*) do (
    if exist "%~dp0%%~nA" ( 
        dir "%%A" | find "0 File(s)" > NUL
        if errorlevel 1 move /y "%%A\*.*" "%~dp0%%~nA"
    ) else (
        move /y "%%A" %~dp0
    )
)

I'm pretty sure the issue is centered around %~dp0%%~nA , I need a way to have it read more than just the folder the file being copied is in, which is all %%~nA shows.

Preferably, the solution should be completely independent of what actual files are in the folder, so it can be used with any update. If I need external tools, I would like them to be free/libre software if possible. I'm running Windows 10, but I know this script will be run by people using Windows 7.

Quite frankly this will do exactly what you want.

robocopy .\upgrade_assets\new_files\ . /E /MOVE

It will create the directories and files as well as delete from source once done.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM