Search code examples
batch-filemovexcopy

Batch - Moving folders and contents based on name to a different drive


First post, and I have searched and tried for this but cant seem to find something like this.

Okay, so I have spent some time on this and have hit a wall. I understand and can code this if I am moving on a single drive (with move) but as soon as I try to implement xcopy so that it will work cross drives I begin to struggle.

This is what I have so far. This will look in the directory name for a left parenthesis and if it has it, it will try to move the directory to the other folder. This works correctly.

@echo off

SETLOCAL EnableDelayedExpansion

for /d %%f in ("%C:\Testing\Folder1%\*") do (
SET "folder=%%f"
if /I NOT "!folder:(=!"=="!folder!" move "%%f" C:\Testing\Folder2
)

Example directory names are as follows

Vacation (2004) - Move

Florida Vacation - Dont Move

New York (2014) - Move

Exmaple Folder - Dont Move

Now when I found out move does not work for different drives (D: (source) and E: (destination)) I found out I needed to use Xcopy. When I try to use xcopy instead of move it will never move any files or folders.

I am thinking I might have to mkdir the folder in the new location first and then move everything and delete the old one, but then I would need to substring the directory name from the full path. I probably can do it, but want to verify with you guys before I spend another half day googling how to do it. I have slammed my head into the wall enough for the past few days that I figured some experts could help out.

Attempt with xcopy

@echo off

SETLOCAL EnableDelayedExpansion

for /d %%f in ("%D:\Folder1%\*") do (
SET "folder=%%f"
if /I NOT "!folder:(=!"=="!folder!" xcopy "%%f" E:\Folder2
)

I appreciate any and all help you can give.

Jay


Solution

  • I use the following, perhaps it'll work for you:

    @echo off
    setlocal enabledelayedexpansion
    
    for /d %%i in (C:\TEST\*(*) do (
    xcopy "%%i" "D:\%%~pni" /s /i
    )
    pause
    

    Substitute paths for your relevant directories. Good luck!