user2242999
user2242999

Reputation: 75

Recursively moving files to root folder

I am more used to using unix shell than CMD, and I am not really sure how to get this to work. I have a directory with several other sub-directories that contain .xml files. I would like to move all the files recursively to the root directory. I know with unix this done like so:

find FOLDERPATH -type f -name '*.xml' -exec mv -i {} FOLDERPATH \;

Yet I can't seem to find something that will work in the same way. XCOPY looked promising, but it doesn't copy only the folders, it copies the whole structure, thus I get these sub-directories that I don't want again. Has anyone got any other suggestions?

Upvotes: 3

Views: 4661

Answers (2)

foxidrive
foxidrive

Reputation: 41267

This will work from the CMD prompt. Run it in the folder you want the files to be moved to, and it will process the sub-directories in that folder.

It does not provide a mechanism to handle filename collisions elegantly.

for /R /D %f in (*) do move "%f\*.xml" .

and this will work in a batch file.

@echo off
for /R /D %%f in (*) do move "%%f\*.xml" .

Upvotes: 5

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200493

Try this:

set FOLDERPATH=...
for /R "%FOLDERPATH%" %%f in (*.xml) do move "%%~ff" "%FOLDERPATH%"

Upvotes: 0

Related Questions