rahul jain
rahul jain

Reputation: 173

Copy all files from several folders to one folder in same directory

I am trying to copy all files from several folders to one folder in the same directory. I have create a Batch file, which contains

MD PATCHCON
for /R %cd% %%f in (*.*) do copy %%f %cd%\PATCHCON
pause

If I put this on the desktop, it runs successfully; if I run the same code in the dir it's not working.

This code also copies my batch file in consolidate folder<patchcon> so I also want to add a code line that does not copy my batch file.

Upvotes: 1

Views: 158

Answers (1)

foxidrive
foxidrive

Reputation: 41224

You have a couple of issues which you may not have realised.

Firstly it will fail in folders or files that have a space or & in the name or path.

The other issue is that it will try to copy some files in the PATCHCON folder twice.

This should solve those problems and remove the batch file itself from the folder.

@echo off
MD "..\PATCHCON"
for /R "%cd%" %%f in (*) do copy "%%f" "..\PATCHCON" >nul
del "..\PATCHCON" "%~nx0"
move "..\PATCHCON" . >nul
echo done.
pause

Upvotes: 1

Related Questions