Search code examples
batch-filewindows-7movefile-processing

Batch file to recursively move a single subdirectory from multiple parent directories TO another single, defined directory


If that title wasn't confusing enough.. hopefully what I am trying to do is a lot simpler to understand.

Windows 7 just in case it needs to be said.

I have multiple directories within the folder I am working in;

C:\WorkingDir\1
C:\WorkingDir\2
C:\WorkingDir\3
and so on

Within each of these folders (1,2,3,etc) is a single sub-directory and no other files or folders;

C:\WorkingDir\1\5E04AB
C:\WorkingDir\2\4F07FC
C:\WorkingDir\3\9DA04F

I need to move each of those single subdirectories from within the parent folder to a new folder;

C:\NewFolder\5E04AB
C:\NewFolder\4F07FC
C:\NewFolder\9DA04F

That's it! I thought it might be simple, but I can't wrap my head around the variables or a better resource explaining how to use them. I don't use batch files much if at all, so I am very sorry for this cry for help. Hopefully someone more knowledgeable has a simple explanation that can help me out :-)

I found this: http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/percent.mspx?mfr=true

But can someone link me to a resource where I can learn more about batch variables and parameters for future reference?

Thank you thank you thank you

Update:

@endoro Thanks for your response. There must have been a user error the first time I tried to run your code. It is working properly and all is well! Thank you so much!

Update 2 After running the code User1 contributed, It will create my NewFolder directory, but will not copy anything to it. It remains empty. Here is some of the repetitive output in DOS when I run it:

C:\WorkingDir>(
set fldr2=C:\WorkingDir\1\5E04AB
 move "C:\WorkingDir\\" "C:\NewFolder\"
)
The system cannot find the file specified.

C:\WorkingDir>(
set fldr2=C:\WorkingDir\2\4F07FC
 move "C:\WorkingDir\\" "C:\NewFolder\"
)
The system cannot find the file specified.

C:\WorkingDir>(
set fldr2=C:\WorkingDir\3\9DA04F
 move "C:\WorkingDir\\" "C:\NewFolder\"
)
The system cannot find the file specified.`

Solution

  • Please look at the output and remove the echo, if it is OK:

    @echo off &setlocal 
    set "workingdir=WorkingDir"
    md "C:\NewFolder" 2>nul
    
    pushd "%workingdir%"
    cd
    for /d %%i in (*) do for /d %%j in ("%%~i\*") do echo move "%%~fj" "C:\NewFolder\%%~nxj"
    popd