Batch file to copy directories recursively [closed]
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 13 days ago .
Is there a way to copy directories recursively inside a .bat file? Is an example of this available?
4 Answers 4
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:
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):
You may write a recursive algorithm in Batch that gives you exact control of what you do in every nested subdirectory:
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
/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:
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)
How to recursively copy files using Windows command line
I’d like to avoid using a batch file, if possible.
Based on this answer to a question about recursive renaming or moving, I’ve came up with the following command (for copying all files named web.foo.config to web.config in the same directory):
However, that just caused every instance of web.foo.config to create and then overwrite .\web.config, not the web.config in the found path. So I tried:
This has the unfortunate effect of copying the files to a file named «%y». Is there a way to force %y to be evaluated after it’s set. or a better method altogether?
1 Answer 1
Copying all files named web.foo.config to web.config in the same directory
Since you don’t want to use a batch file for this operation then from an elevated command prompt, you can use the below to complete this.
This assumes the directory you are in when you run the command from the command prompt is the one which will be traversed through recursively doing the copy command of the found files.
I left the asterisk ( * ) from the beginning of the web.foo.config file name but you can add that where needed if it’s really needed to find files with that naming pattern.
Using Copy Example
Using Xcopy Example
Further Resources
In addition, substitution of FOR variable references has been enhanced. You can now use the following optional syntax:
How to recursively copy files and directories
With C++, is it possible to recursively copy files and directories from one path to another
- without having to use any additional libraries?
- and with platform independent functions?
Considering the following filesystem
- all files and directories or
- certain files and directories
from src to another directory target .
I’ve created a new question since the questions I’ve found are platform specific and don’t include filtering:
1 Answer 1
Yes, it is possible to copy a complete directory structure using nothing else than std C++ . beginning with C++17 and its std::filesystem which includes std::filesystem::copy .
- Copying all files can be done using copy_options::recursive :
- To copy a certain subset of files using a filter, recursive_directory_iterator can be utilized:
When calling the second method like
and the given filesystem is in the working directory of the process, then the result is
You could also pass the copy_options as parameter to CopyRecursive() for even more flexibility.
A list of some of the functions from std::filesystem which were used above:
- relative() substracts the paths and heeds symlinks (it uses path::lexically_relative() and weakly_canonical() )
- create_directories() creates a directory for every element of the given path that does not already exist
- current_path() returns (or changes) the absolute path of the current working directory
- path::stem() returns the filename without the final extension
- path::generic_string() gives the narrow string with platform independent directory separator /
For production code, I recommend to pull the error handling out of the utility functions. For error handling, std::filesystem provides two methods:
Also take into consideration, that std::filesystem might not be available on all platforms
Recursive copy in windows
Simple, flexible file copy utility
- Recursively copy whole directory hierarchies
- Choose which files are copied by passing a filter function, regular expression or glob
- Rename files dynamically, including changing the output path
- Transform file contents using streams
- Choose whether to overwrite existing files
- Choose whether to copy system files
- Filters out junk files by default
- Uses graceful-fs and mkdirp to avoid filesystem errors
- Emits start, finish and error events for each file that is processed
- Optional promise-based interface
Node-style callback interface
copy(src, dest, [options], [callback])
Recursively copy files and folders from src to dest
Name | Type | Required | Default | Description |
---|---|---|---|---|
src | string | Yes | N/A | Source file/folder path |
dest | string | Yes | N/A | Destination file/folder path |
options.overwrite | boolean | No | false | Whether to overwrite destination files |
options.expand | boolean | No | false | Whether to expand symbolic links |
options.dot | boolean | No | false | Whether to copy files beginning with a . |
options.junk | boolean | No | false | Whether to copy OS junk files (e.g. .DS_Store , Thumbs.db ) |
options.filter | function , RegExp , string , array | No | null | Filter function / regular expression / glob that determines which files to copy (uses maximatch) |
options.rename | function | No | null | Function that maps source paths to destination paths |
options.transform | function | No | null | Function that returns a transform stream used to modify file contents |
options.results | boolean | No | true | Whether to return an array of copy results |
options.concurrency | number | No | 255 | Maximum number of simultaneous copy operations |
options.debug | boolean | No | false | Whether to log debug information |
callback | function | No | null | Callback, invoked on success/failure |
The value returned by the copy function implements the EventEmitter interface, and emits the following events:
Event | Handler signature |
---|---|
copy.events.ERROR | function(error, ErrorInfo) |
copy.events.COMPLETE | function(Array ) |
copy.events.CREATE_DIRECTORY_START | function(CopyOperation) |
copy.events.CREATE_DIRECTORY_ERROR | function(error, CopyOperation) |
copy.events.CREATE_DIRECTORY_COMPLETE | function(CopyOperation) |
copy.events.CREATE_SYMLINK_START | function(CopyOperation) |
copy.events.CREATE_SYMLINK_ERROR | function(error, CopyOperation) |
copy.events.CREATE_SYMLINK_COMPLETE | function(CopyOperation) |
copy.events.COPY_FILE_START | function(CopyOperation) |
copy.events.COPY_FILE_ERROR | function(error, CopyOperation) |
copy.events.COPY_FILE_COMPLETE | function(CopyOperation) |
. where the types referred to in the handler signature are as follows:
Recursive Copy of Directory
On my old VPS I was using the following code to copy the files and directories within a directory to a new directory that was created after the user submitted their form.
However now on my new VPS it will only create the main directory, but will not copy any of the files to it. I don’t understand what could have changed between the 2 VPS’s?
14 Answers 14
Try something like this:
Iterator iterate through all folders and subfolders and make copy of files from $source to $dest
Could I suggest that (assuming it’s a *nix VPS) that you just do a system call to cp -r and let that do the copy for you.
I have changed Joseph’s code (below), because it wasn’t working for me. This is what works:
[EDIT] added test before creating a directory (line 7)
Here’s what we use at our company:
This function copies folder recursivley very solid. I’ve copied it from the comments section on copy command of php.net
The Symfony’s FileSystem Component offers a good error handling as well as recursive remove and other useful stuffs. Using @OzzyCzech’s great answer, we can do a robust recursive copy this way:
Note: you can use this component as well as all other Symfony2 components standalone.
OzzyCheck’s is elegant and original, but he forgot the initial mkdir($dest); See below. No copy command is ever provided with contents only. It must fulfill its entire role.
Here’s a simple recursive function to copy entire directories
I guess you should check user(group)rights. You should consider chmod for example, depending on how you run (su?)PHP. You could possibly also choose to modify your php configuration.
hmm. as that’s complicated ))
$file — somthing like that www/folder/ahah/file.txt
There were some issues with the functions that I tested in the thread and here is a powerful function that covers everything. Highlights:
No need to have an initial or intermediate source directories. All of the directories up to the source directory and to the copied directories will be handled.
Ability to skip directory or files from an array. (optional) With the global $skip; skipping of files even under sub level directories are handled.
Full recursive support, all the files and directories in multiple depth are supported.