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

StackOverflow https://stackoverflow.com/questions/16066675

Question

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.`
Was it helpful?

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

OTHER TIPS

I haven't been able to test this but:

@echo off
setlocal EnableDelayedExpansion
md "c:\NewFolder\"
cd "C:\WorkingDir\"
for /D /r %%a in ("*") do (
    set fld1=%%a
    cd "%fld1%"
    for /D /r %%b in ("*") do (
        set fld2=%%b
        move "c:\WorkingDir\%fld1%\%fld2%" "C:\NewFolder\%fld2%"
        )
    )

And a good resource for batch:

http://ss64.com/nt/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top