Search code examples
batch-filemove

Moving Directories


I have created a txt file with a list of directories I need to move to a new location. However there is a large number of directories, and trying to copy and paste just some will take forever.

How can I use this txt file to move only those directories to a new folder? I want to also keep each directories sub-directories in tact.

Thanks.


Solution

  • @ECHO OFF
    SETLOCAL
    SET "sourcedir=U:\sourcedir"
    SET "destdir=U:\destdir"
    FOR /f "delims=" %%a IN ('dir /b/ad "%sourcedir%\t w o" ') DO MOVE "%sourcedir%\t w o\%%a" "%destdir%\%%a"
    
    GOTO :EOF
    

    This is an example of how I would do it. The dir command produces a directory list of the subdirectories of "%sourcedir%\t w o" which is simply a test directory. The delims= ensures the entire directory name from the list is applied to %%a.

    If your list in afile.txt is something like

    u:\somedirectory to move
    u:\someotherdirectory to move
    u:\moveme
    

    then

    FOR /f "delims=" %%a IN (afile.txt) DO ECHO(MOVE "%%a" "%destdir%\%%~nxa"
    

    should list the proposed moves. Remove the echo( to actually do the move (after checking...)