Windows batch file to copy directory

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 15 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 copy a directory structure but only include certain files (using windows batch files)

As the title says, how can I recursively copy a directory structure but only include some files. E.g given the following directory structure:

The files data.zip and info.txt can appear everywhere in the directory structure. How can I copy the full directory structure, but only include files named data.zip and info.txt (all other files should be ignored)?

The resulting directory structure should look like this:

15 Answers 15

You don’t mention if it has to be batch only, but if you can use ROBOCOPY , try this:

EDIT: Changed the /S parameter to /E to include empty folders.

An alternate solution that copies one file at a time and does not require ROBOCOPY:

The outer for statement generates any possible path combination of subdirectory in SOURCE_DIR and name in FILENAMES_TO_COPY . For each existing file xcopy is invoked. FILE_INTERMEDIATE_DIR holds the file’s subdirectory path within SOURCE_DIR which needs to be created in DEST_DIR .

try piping output of find (ie. the file path) into cpio

cpio checks timestamp on target files — so its safe and fast.

Читайте также:  Windows 10 pro terminal server

remove -v for faster op, once you get used to it.

If Powershell is an option, you can do this:

The main disadvantage is it copies all folders, even if they will end up being empty because no files match the filter you specify. So you could end up with a tree full of empty folders, in addition to the few folders that have the files you want.

Thanks To Previous Answers. 🙂

This script named «r4k4copy.cmd»:

It accepts variable of «Source», «Destination», and «FileName». It also can only copying specified type of files or selective filenames.

Any improvement are welcome. 🙂

To copy all text files to G: and preserve directory structure:

With find and cp only:

Similar to Paulius’ solution, but the files you don’t care about are not copied then deleted:

That’s only two simple commands, but I wouldn’t recommend this, unless the files that you DON’T need to copy are small. That’s because this will copy ALL files and then remove the files that are not needed in the copy.

Sure, the second command is kind of long, but it works!

Also, this approach doesn’t require you to download and install any third party tools (Windows 2000+ BATCH has enough commands for this).

Under Linux and other UNIX systems, using the tar command would do this easily.

Then you’d cwd to the target and:

Of course you could pipe the output from the first tar into the 2nd, but seeing it work in steps is easier to understand and explain. I’m missing the necessary cd /to/new/path/ in the following command — I just don’t recall how to do it now. Someone else can add it, hopefully.

Tar (gnutar) is available on Windows too, but I’d probably use the xcopy method myself on that platform.

EDIT: If you want to preserve the empty folders (which, on rereading your post, you seem to) use /E instead of /S.

Using WinRAR command line interface, you can copy the file names and/or file types to an archive. Then you can extract that archive to whatever location you like. This preserves the original file structure.

I needed to add missing album picture files to my mobile phone without having to recopy the music itself. Fortunately the directory structure was the same on my computer and mobile!

  • C:\Downloads\music.rar = Archive to be created
  • X:\music\ = Folder containing music files
  • Folder.jpg = Filename I wanted to copy

This created an archive with all the Folder.jpg files in the proper subdirectories.

This technique can be used to copy file types as well. If the files all had different names, you could choose to extract all files to a single directory. Additional command line parameters can archive multiple file types.

For those using Altap Salamander (2 panels file manager) : in the Options of the Copy popup, just specify the file names or masks. Easy.

I am fine with regular expressions, lazy and averse to installs, so I created a batch file that creates the directory and copies with vanilla DOS commands. Seems laborious but quicker for me than working out robocopy.

  1. Create your list of source files with complete paths, including drive letter if nec, in a text file.
  2. Switch on regular expressions in your text editor.
  3. Add double quotes round each line in case of spaces — search string (.*) replace string «\1» , and click replace all
  4. Create two lines per file — one to create the directory, one to copy the file (qqq will be replaced with destination path) — search string (.*) replace string md qqq\1\nxcopy \1 qqq\1\n and click replace all
  5. Remove the filename from the destination paths – search \\([^\\^»]+)»\n replace \\»\n
  6. Replace in the destination path (in this example A:\src and B:\dest ). Turn OFF regular expressions, search qqq»A:\src\ replace B:\dest\ and click replace all.
Читайте также:  Оптимизация war thunder mac os

md will create nested directories. copy would probably behave identically to xcopy in this example. You might want to add /Y to xcopy to suppress overwrite confirms. You end up with a batch file like so:

repeated for every file in your original list. Tested on Win7.

copying all contents of folder to another folder using batch file?

I have a folder in C:\Folder1

I want to copy all the contents of Folder1 to another location, D:\Folder2

How do I do this using a batch file?

12 Answers 12

xcopy.exe is the solution here. It’s built into Windows.

If you have robocopy,

if you want remove the message that tells if the destination is a file or folder you just add a slash:

xcopy /s c:\Folder1 d:\Folder2\

I see a lot of answers suggesting the use of xcopy. But this is unnecessary. As the question clearly mentions that the author wants THE CONTENT IN THE FOLDER not the folder itself to be copied in this case we can -:

Thats all xcopy can be used for if any subdirectory exists in C:\Folder1

RoboCopy did not work for me, and there are some good solutions here, but none explained the XCopy switches and what they do. Also you need quotes in case your path has spaces in it.

xcopy /i /e «C:\temp\folder 1» «C:\temp\folder 2»

Here is the documentation from Microsoft:

On my PC, xcopy and robocopy need also the path to them, i.e. C:\Windows\System32\xcopy.exe

That’s why I use simply «copy»: copy /y . \Folder1\File.txt . \Folder2\

This is how it is done! Simple, right?

Here’s a solution with robocopy which copies the content of Folder1 into Folder2 going trough all subdirectories and automatically overwriting the files with the same name:

/COPYALL copies all file information
/E copies subdirectories including empty directories
/IS includes the same files
/IT includes modified files with the same name

Note: it can be necessary to run the command as administrator, because of the argument /COPYALL . If you can’t: just get rid of it.

FYI. if you use TortoiseSVN and you want to create a simple batch file to xcopy (or directory mirror) entire repositories into a «safe» location on a periodic basis, then this is the specific code that you might want to use. It copies over the hidden directories/files, maintains read-only attributes, and all subdirectories and best of all, doesn’t prompt for input. Just make sure that you assign folder1 (safe repo) and folder2 (usable repo) correctly.

Читайте также:  Что такое сервер терминалов windows 2012 r2

And, that’s it folks!

Add to your scheduled tasks and never look back.

I have written a .bat file to copy and paste file to a temporary folder and make it zip and transfer into a smb mount point, Hope this would help,

Batch file to copy files from one folder to another folder

I have a storage folder on a network in which all users will store their active data on a server. Now that server is going to be replaced by a new one due to place problem so I need to copy sub folders files from the old server storage folder to new server storage folder. I have below ex:

from \Oldeserver\storage\data & files to \New server\storage\data & files.

9 Answers 9

xcopy.exe is definitely your friend here. It’s built into Windows, so its cost is nothing.

Just xcopy /s c:\source d:\target

You’d probably want to tweak a few things; some of the options we also add include these:

  • /s/e — recursive copy, including copying empty directories.
  • /v — add this to verify the copy against the original. slower, but for the paranoid.
  • /h — copy system and hidden files.
  • /k — copy read-only attributes along with files. otherwise, all files become read-write.
  • /x — if you care about permissions, you might want /o or /x .
  • /y — don’t prompt before overwriting existing files.
  • /z — if you think the copy might fail and you want to restart it, use this. It places a marker on each file as it copies, so you can rerun the xcopy command to pick up from where it left off.

If you think the xcopy might fail partway through (like when you are copying over a flaky network connection), or that you have to stop it and want to continue it later, you can use xcopy /s/z c:\source d:\target .

Batch File to copy folders from a text file to another directory

I am trying to create a batch file to read off a text file and copy the folder with the files to another directory:

When I run this batch file, only the files copy. I want to copy the folders with the files.

IE folder\files to destination\folder\files.

4 Answers 4

Have a look at xcopy .

If the text file contains filenames, try:

If the text file contains only directory names, try:

Do you own Vista or later? Then give robocopy a shot: robocopy «source» «destination» /MIR . Some other options:

Thera are more interesting options.

before you mess up the files you should probably test it out IE.

this will make a basic folder setup to test it out with. after that if it works apply it to your files path.

The solutions above seem to copy the directory contents to the root of the destination. And have problems with folders that have spaces in them. try the lines below to solve that.

Directories that don’t exist will be created with this command and it will overwrite files when they exist in the destination.

The delims= part makes sure that the directories read from the textfiles can contain spaces

Оцените статью