windowsbatch-filemoverobocopy

Robocopy Windows 11 how to move target folder inside destination folder


I'm trying to move multiple "marked" folders in a directory inside a copy folder (that may or may not already exist). Robocopy isn't moving the targeted folder inside the destination folder

This is what I tried

SetLocal EnableDelayedExpansion
for /F "delims=" %%a in ('dir /b *P') do (
 set "Name=%%a"
 set "Folder=!Name:~0,6!"
 robocopy /MOVE /E "%%a" !Folder!\ 
)
cmd /k

This code worked well in Windows 10, but in Windows 11 it deletes the "marked (*P)" folder without moving it inside the destination folder (which shares the same first 6 digits), regardless of whether the folder already exist or not

I tried searching for a changelog between win10 and win11 versions of robocopy to see if there was a change in the MOVE command or any other parameter

I tried changing diverse parts of the code too (even managing to create blank space named folders at some point) to see if any other part of the code had changed between win10 and win11, but still suspect its robocopy since in a test directory where only "333333 P" exist with an empty text file inside, I ran the .bat and a new "333333" folder appeared (with the file inside, basically just a rename of the folder, which just undoes me "marking" it) when in win10 it moved "333333 P" inside "333333" (worked as intended)

15/5/2023 More info: I'm using a "database" of folders and files that have the folders numbered from "10000" onwards to around "186000" (now). I wanted to move al folders inside the same numbered folder, but marked with a P (Past) so as to differentiate what I'm working with vs what was worked before. I first began doing it manually, but soon realized how massive it actually was, so I repurposed a simple renaming script I had and began researching how to massively move only desired items into multiple folder. I first tried using "xcopy" but it didn't work as intended, then searching on the internet I found robocopy, and wrote that initial batch.

That batch worked fine in my computer at home in a test directory with about 10 folders (Windows 10), moving all marked folders and their contents (empty directories included for archiving purposes) inside a newly created folder. EX: out of all the folders, i renamed "3", "5", "11" to "3 P", "5 P", "11 P", and wanted the batch to move those inside a new folder created with the same ID (so a new "3", "5", "11" folder is created, and inside is "3 P", "5 P", "11 P" respectively, with all the files and directories it had)

When I rewrote the batch in my computer at work, in another test directory I made, the batch no longer worked as it did at home. First I suspected folder ownership and administrator rights, but I have them in my work computer too. That is when I began suspecting changes in robocopy from win10 to win11, but found no documentation or changelog anywhere, so I'm not even sure if that is the case


Solution

  • The task can be done with a batch file with following command lines:

    @echo off
    setlocal EnableExtensions DisableDelayedExpansion
    for /F "delims=" %%I in ('dir "* P" /AD-L /B 2^>nul') do for /F %%J in ("%%I") do %SystemRoot%\System32\robocopy.exe "%%I" "%%J\%%I" /E /MOVE /NDL /NFL /NJH /NJS /R:0 >nul
    endlocal
    

    Example:

    The directory C:\Temp contains following directories and files:

    There is executed MoveFolders.cmd with C:\Temp as current directory.

    The result in C:\Temp is:

    C:\Temp\3 P\Subfolder 1\File 1.txt and C:\Temp\3 P\File 2.txt are not moved by ROBOCOPY because of the destination directories contain already the two files. The directories C:\Temp\3 P and C:\Temp\3 P\Subfolder 1 are not deleted for that reason.

    This enhanced code deletes the files and directories which exist already in the destination directories.

    @echo off
    setlocal EnableExtensions DisableDelayedExpansion
    for /F "delims=" %%I in ('dir "* P" /AD-L /B 2^>nul') do for /F %%J in ("%%I") do (
        %SystemRoot%\System32\robocopy.exe "%%I" "%%J\%%I" /E /MOVE /NDL /NFL /NJH /NJS /R:0 >nul
        if not errorlevel 2 if exist "%%I\" rd /Q /S "%%I"
    )
    endlocal
    

    The result in C:\Temp is now:

    It is advisable on running the batch file that none of the directories to move is the current directory of any running process and none of the files to move is opened by a running application as in this case ROBOCOPY would fail to move those files and folders.

    To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.

    There should be additionally read the SS64 documentation for ROBOCOPY.exe and ROBOCOPY Exit Codes.