Is there a way to copy directories recursively inside a .bat file? Is an example of this available?
Look into xcopy, which will recursively copy files and subdirectories.
There are examples, 2/3 down the page. Of particular use is:
To copy all the files and subdirectories (including any empty subdirectories) from drive A to drive B, type:
xcopy a: b: /s /e
Copy-Item -Recurse
in PowerShell instead too. –
Graffito /i
option: "If Source is a directory or contains wildcards and Destination does not exist, xcopy assumes destination specifies a directory name and creates a new directory. Then, xcopy copies all specified files into the new directory. By default, xcopy prompts you to specify whether Destination is a file or a directory." (from MS online documentation) –
Cockboat After reading the accepted answer's comments, I tried the robocopy command, which worked for me (using the standard command prompt from Windows 7 64 bits SP 1):
robocopy source_dir dest_dir /s /e
/E
imply /S
? copy subdirectories, including Empty ones.
–
Berkly /S
and /E
seem to imply opposite things according to the robocopy /?
help - s is "not empty ones" e is "empty ones". I think you should just choose one. –
Barela robocopy
returns an exit code of 1 if one or more files were successfully copied. –
Deledda /S
and /E
- /E
says copy subdirectories, including Empty ones
. It doesn't say only empty ones. –
Mummer I wanted to replicate Unix/Linux's cp -r
as closely as possible. I came up with the following:
xcopy /e /k /h /i srcdir destdir
Flag explanation:
/e
Copies directories and subdirectories, including empty ones.
/k
Copies attributes. Normal Xcopy will reset read-only attributes.
/h
Copies hidden and system files also.
/i
If destination does not exist and copying more than one file, assume destination is a directory.
I made the following into a batch file (cpr.bat
) so that I didn't have to remember the flags:
xcopy /e /k /h /i %*
Usage: cpr srcdir destdir
You might also want to use the following flags, but I didn't:
/q
Quiet. Do not display file names while copying.
/b
Copies the Symbolic Link itself versus the target of the link. (requires UAC admin)
/o
Copies directory and file ACLs. (requires UAC admin)
You may write a recursive algorithm in Batch that gives you exact control of what you do in every nested subdirectory:
@echo off
call :treeProcess
goto :eof
:treeProcess
rem Do whatever you want here over the files of this subdir, for example:
copy *.* C:\dest\dir
for /D %%d in (*) do (
cd %%d
call :treeProcess
cd ..
)
exit /b
Windows Batch File Looping Through Directories to Process Files?
Pure batch *.bat snippet for recursive folder with files copy. Without xcopy/robocopy and other external tools
It is loop over directories, concatenate destination path with relative source path and copy folders. Relative path get from the absolute path without source part (cutted by source path length).
Set sourcedir path for the source directory
Set destdir path for the destination directory
(1) rmdir /s /q %destdircopy% will remove all files in the destination folder
For example, this code will remove directory "build\src" and copy all folders with files from "src\" to "build\src"
@ECHO OFF
SET "sourcedir=src"
SET "destdir=build"
SET "destdircopy=%destdir%\%sourcedir%"
@REM (1) ------------------------------------------------------- CLEAR BEFORE COPY
rmdir /s /q %destdircopy%
@REM (2) ------------------------------------------------------- GET SOURCE PATH LENGTH
Setlocal EnableDelayedExpansion
set "files=0"
pushd %sourcedir%
set ABS_PATH=%CD%
popd
echo "%sourcedir% absolute path is %ABS_PATH%"
call :length srclen "%ABS_PATH%"
@REM (3) ------------------------------------------------------- FOR SUBDIRECTORIES LOOP
for /f "tokens=*" %%G in ('dir /b /s /a:d "%sourcedir%"') do (
set /a files += 1
call :length len "%%~fG"
setlocal enabledelayedexpansion
SET _path=%%~fG
SET _startchar=%srclen%
SET /A _length=!len!-%srclen%
CALL SET _substring=%%_path:~!_startchar!,!_length!%%
set currentpath=%%G
MKDIR %destdircopy%!_substring!
copy !currentpath! %destdircopy%!_substring!
)
copy %sourcedir% %destdircopy%
goto :EOF
@REM (4) ------------------------------------------------------- PATH STRING LENGTH
:length <return_var> <string>
setlocal enabledelayedexpansion
if "%~2"=="" (set ret=0) else set ret=1
set "tmpstr=%~2"
for %%I in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do (
if not "!tmpstr:~%%I,1!"=="" (
set /a ret += %%I
set "tmpstr=!tmpstr:~%%I!"
)
)
endlocal & set "%~1=%ret%"
goto :EOF
@REM (5) www.ildar.in/code/snippets/batch_folder_copy_recurcive.bat
© 2022 - 2024 — McMap. All rights reserved.