0

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.

  • 2
    Well, I would say look again at your script and see if you can find the issues `:)` let me start of by saying your for loop is wrong Not sure why you added the parenthesis around the path. `for /d /r upgrade_assets\new_files %%A in (*) do (` – Gerhard Jul 04 '20 at 08:09
  • 1
    @GerhardBarnard You're right, I probably just copied that from another attempt on accident. The loop does function now, however I still have issues going more than one folder deep with that loop. I've edited the post to mention this. Also, I'm not sure why I'm being downvoted, was something wrong with my post outside of code being wrong? – watchbensononyoutube Jul 04 '20 at 17:59
  • I cannot help with the downvoting, but people here can get a bit pedantic. I will have a look at your script a bit later on my laptop as I am traveling now. – Gerhard Jul 04 '20 at 18:45

1 Answers1

0

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.

Gerhard
  • 22,678
  • 7
  • 27
  • 43
  • I tested and it worked perfect. But as far as I understand, robocopy actually *copies* the files and then deletes the original. Because of how big updates can get (and because some people apparently keep their drive filled to the brim) I'd prefer a method that does an actual move command where it just changes the file table. I'm probably gonna have to fall back on robocopy anyway because people are gonna get pissed demanding the update soon if not already happening, but if I can replace that method in a later update that'd be amazing. – watchbensononyoutube Jul 05 '20 at 16:56