JSI Tip 3935. How do I copy all the files in a directory tree to a flat folder?


If you wish to copy all the files in a directory and its' sub-directories to different directory, without preserving the directory structure:

CopyFlat From To

where From is the full path to the source directory and To is the full path to the target directory.

CopyFlat.bat contains:

@echo off
setlocal
If \{%1\} EQU \{\} goto Syntax
If \{%2\} EQU \{\} goto Syntax
set from=%1
set to=%2
set from=%from:"=%#
set to=%to:"=%#
set from=%from:\#=%
set to=%to:\#=%
set from=%from:#=%
set to=%to:#=%
if not exist "%from%" goto Syntax
if not exist "%to%" goto Syntax
pushd "%to%"
for /f "Tokens=*" %%i in ('dir /b /s /a-d "%from%\*.*"') do call :cpy "%%i"
popd
endlocal
goto :EOF
:Syntax
@echo Syntax: CopyFlat From To
endlocal
goto :EOF
:cpy
copy %1 "%~nx1"



Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish