Windows batch copy all files and folders

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/bat to copy folder and content at once

I’m writing a batch script that does a copy. I want to script it to copy an entire folder. When I want to copy a single file, I do this

If I have a folder with this structure, is there a command to copy this entire folder with its contents all at once while preserving the exact structure.

5 Answers 5

if you have xcopy , you can use the /E param, which will copy directories and subdirectories and the files within them, including maintaining the directory structure for empty directories

xcopy is deprecated. Robocopy replaces Xcopy. It comes with Windows 8, 8.1 and 10.

robocopy has several advantages:

  • copy paths exceeding 259 characters
  • multithreaded copying

I suspect that the xcopy command is the magic bullet you’re looking for.

It can copy files, directories, and even entire drives while preserving the original directory hierarchy. There are also a handful of additional options available, compared to the basic copy command.

If your batch file only needs to run on Windows Vista or later, you can use robocopy instead, which is an even more powerful tool than xcopy , and is now built into the operating system. It’s documentation is available here.

I’ve been interested in the original question here and related ones.

For an answer, this week I did some experiments with XCOPY.

To help answer the original question, here I post the results of my experiments.

I did the experiments on Windows 7 64 bit Professional SP1 with the copy of XCOPY that came with the operating system.

For the experiments, I wrote some code in the scripting language Open Object Rexx and the editor macro language Kexx with the text editor KEdit.

XCOPY was called from the Rexx code. The Kexx code edited the screen output of XCOPY to focus on the crucial results.

The experiments all had to do with using XCOPY to copy one directory with several files and subdirectories.

The experiments consisted of 10 cases. Each case adjusted the arguments to XCOPY and called XCOPY once. All 10 cases were attempting to do the same copying operation.

Here are the main results:

(1) Of the 10 cases, only three did copying. The other 7 cases right away, just from processing the arguments to XCOPY, gave error messages, e.g.,

with no files copied.

Of the three cases that did copying, they all did the same copying, that is, gave the same results.

Читайте также:  Windows 10 classic viewer

(2) If want to copy a directory X and all the files and directories in directory X, in the hierarchical file system tree rooted at directory X, then apparently XCOPY — and this appears to be much of the original question — just will NOT do that.

One consequence is that if using XCOPY to copy directory X and its contents, then CAN copy the contents but CANNOT copy the directory X itself; thus, lose the time-date stamp on directory X, its archive bit, data on ownership, attributes, etc.

Of course if directory X is a subdirectory of directory Y, an XCOPY of Y will copy all of the contents of directory Y WITH directory X. So in this way can get a copy of directory X. However, the copy of directory X will have its time-date stamp of the time of the run of XCOPY and NOT the time-date stamp of the original directory X.

This change in time-date stamps can be awkward for a copy of a directory with a lot of downloaded Web pages: The HTML file of the Web page will have its original time-date stamp, but the corresponding subdirectory for files used by the HTML file will have the time-date stamp of the run of XCOPY. So, when sorting the copy on time date stamps, all the subdirectories, the HTML files and the corresponding subdirectories, e.g.,

can appear far apart in the sort on time-date.

Hierarchical file systems go way back, IIRC to Multics at MIT in 1969, and since then lots of people have recognized the two cases, given a directory X, (i) copy directory X and all its contents and (ii) copy all the contents of X but not directory X itself. Well, if only from the experiments, XCOPY does only (ii).

So, the results of the 10 cases are below. For each case, in the results the first three lines have the first three arguments to XCOPY. So, the first line has the tree name of the directory to be copied, the ‘source’; the second line has the tree name of the directory to get the copies, the ‘destination’, and the third line has the options for XCOPY. The remaining 1-2 lines have the results of the run of XCOPY.

One big point about the options is that options /X and /O result in result

To see this, compare case 8 with the other cases that were the same, did not have /X and /O, but did copy.

These experiments have me better understand XCOPY and contribute an answer to the original question.

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.

Читайте также:  Mac os удаление временных файлов

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,

Copy a file to all folders batch file?

I need copy credits.jpg from C:\Users\meotimdihia\Desktop\credits.jpg to D:\Software\destinationfolder and all subfolders I read many and i write

then i save file saveall.bat but i run it , it dont work at all. help me write 1 bat

3 Answers 3

Give this a try:

Of course, if it’s to go into a batch file, double the ‘%’.

This was the first search I found on google for batch file copy file to all subfolders .

Here’s a way with xcopy. There’s also robocopy but that would be too powerful for a simple task like this. (I’ve used that for entire drive backups because it can use multi-threading)

But, let us focus on xcopy .

This example is for saving to a file with the extension .bat . Just drop the additional % where there is two if running directly on the command line.

  • cd «D:\Software\destinationfolder» change directory to the folder you want to copy the file to. Wrap this in quotes if the path has whitespaces.
  • the for loop — See help here. Or type for /? in a command prompt.
  • /r — Loop through files (recurse subfolders)
  • /d — Loop through several folders
  • %%I — %%parameter : A replaceable parameter
  • xcopy — Type xcopy /? in the command line for lots of help. You may need to press Enter to read the entire help on this.
  • C:\temp\file.ext — The file you want to copy
  • «%%

fsI» — Expands %%I to a full pathname with short names only

  • /H — Copies files with hidden and system file attributes. By default, xcopy does not copy hidden or system files
  • /K — Copies files and retains the read-only attribute on Destination files if present on the Source files. By default, xcopy removes the read-only attribute.
  • The last two parameters are just examples if you’re having trouble with any read-only files and will retain the most important file properties.

    Just for completeness. This example below will copy the same file in each folder of the current directory and not any sub-folders. Just the /r option is removed for it to behave like this.

    Copy Folders and Subfolders using XCopy Command or Easier Way

    You will learn to use XCopy command to copy folders and subfolders for different requirements, not just files. Also, an easier way is prepared. Learn them first and then pick one per your needs.

    By Nicky / Last Updated April 7, 2021

    What is Xcopy command?

    Xcopy is a command that can copy files, directories, and even entire drives from one location to another. It is an advanced version of the copy command that it has additional switches to specify both the source and the destination in detail. And it is useful in particular when copying files from CDROM to a hard drive because it will remove the read-only attribute automatically.

    The Xcopy command is available in most current operating systems such as Windows 10, Windows 8, Windows 7, Windows XP, Windows Vista and Windows 98. The Xcopy command is also DOS command available in MS-DOS.

    Can I copy folders and subfolders using Xcopy command?

    “I have a folder with a lot of subfolders and files in them. Now, I want to copy the folder to another location, but I don’t want to recreate directory structures. Can I use Xcopy command to copy the directory structure of the folder and subfolders to another location?”

    As we know, recreating directory structures can be a tedious and error-prone thing, especially if using the right-click method to create multiple structures and folders. So, you may want to copy folders and subfolders by Xcopy command directly. If you don’t know how to achieve it via Xcopy, keep reading to learn the detailed steps.

    Copy folders and subfolders using Xcopy command in Windows 7/8/10

    To move folders and subfolders in cmd, the most used command syntax would be:

    xcopy [source] [destination] [options]

    And there are plenty of options and switches for different demands. You can copy folders and subfolders including or excluding files from one location to another location by following the steps in different situations.

    Situation 1. copy folders and subfolders and contents

    1. Click Start and type cmd in the search box. Right-click Command Prompt from the list and select Run as administrator.

    2. Now, when you’re in the command prompt, you can type Xcopy command as below to copy folders and subfolders including contents. For example, you want to copy “test” folder and its subfolders on C drive to D drive, type Xcopy command as:

    Xcopy C:\test D:\test /E /H /C /I

    /E – Copy subdirectories, including any empty ones.

    /H — Copy files with hidden and system file attributes

    /C — Continue copying even if an error occurs.

    /I — If in doubt, always assume the destination is a folder. e.g. when the destination does not exist.

    Xcopy will not copy open files.

    Xcopy doesn’t display any progress or completed operations information when executed.

    Remember that for folders with spaces in the name or folders larger than 8 characters in length, it is a good idea to put quotation marks around the path as in the figure above or you may get an error.

    Situation 2. copy folders and subfolders without files

    You can open the Command Prompt according to the steps shown above. Then, type the Xcopy command as below to copy folders and subfolders without files:

    Xcopy Source Destination /T /E

    /T — Copy the subdirectory structure, but not the files.

    /E — Copy subdirectories, including any empty ones.

    Situation 3. copy folders and subfolders with NTFS and Share permission

    Sometimes, you may need to transfer the NTFS and Share permission together with your folders. Once the Command Prompt is open, you can then type the following command to copy all files and folders and retain its NTFS and Share permissions.

    Xcopy Source Destination /O /X /E /H /K

    /E — Copy folders and subfolders, including empty ones.

    /H — Copy hidden and system files also.

    /K — Copy attributes. Typically, Xcopy resets read-only attributes.

    /O — Copy file ownership and ACL information.

    /X — Copy file audit settings (implies /O).

    Actually, there are many syntaxes for the Xcopy command, and you should use different syntaxes for different situations. Under most circumstances, you need to use several syntaxes at the same time. But if one syntax is used incorrectly, the Xcopy command may refuse to work, even worse, you may lose files. Therefore, you must be very careful when using the Xcopy command.

    In fact, most users are not familiar with the use of the command line, and you may be confused with a large number of syntaxes. Therefore, you can copy folders and files in an easier way using a third-party migration tool like AOMEI Backupper Standard.

    Easier way to copy files or folders in Windows 7/8/10

    AOMEI Backupper Standard is a free and reliable backup software for Windows Vista/XP/7/8/10. It allows you to backup folders/files to different places like internal hard drive, external hard drive, USB drive, network and so on. This software has a clear interface and user-friendly design, so, you can figure it out quickly even for the first time.

    In addition, this software has many useful features. For example, it supports scheduled backup, which can help you automatically backup folders/files in Daily, Weekly, Monthly, and Event-triggerВ mode. Furthermore, Full backup and Incremental backup are also supported. You can set incremental backup to save both your storage disk space and backup time.

    Step by Step guide to copy folders files or folders via AOMEI Backupper

    1.В Download AOMEI Backupper Standard, install and launch it.

    2. Click Backup and File Backup subsequently.

    3. Click Add File or Add Folder to choose the files or folders you want to backup and then click OK.

    вњЋ Note: After clicking Add Folder, you can open Filter Settings to set file inclusion/exclusion mask. This function can help you backup files with specific file extension, you can upgrade to paid versions to enjoy it.

    4. Select the destination path you want to copy files to. Internal hard drive, connected external hard drive, Cloud drive, shared folder, and NAS are available.

    5. (Optional) Click Schedule to set a Daily/Weekly/Monthly under General tab, or select Full and Incremental backup under Advanced tab. Then, click Start Backup.

    Verdict

    You can copyВ foldersВ andВ subfolders using Xcopy command by the steps shown above. However, many users say that copying failed due to Xcopy not working sometimes, or Xcopy does not copy all files. And in some cases, Xcopy halts because of “Insufficient Memory” error or invalid number of parameters.В

    Therefore, using AOMEI Backupper to copy folders or files is a more reliable and convenient method. With only a few clicks, you can copy folders or files to your destination, and its useful features like incremental backup, USB plug in, etc. bring you much convenience.

    Moreover, AOMEI Backupper Standard designs for Windows XP/Vista/7/8/10 only, if you are a Windows Server user, you can try another version–AOMEI Backupper Server.

    Читайте также:  Как убрать пароль через биос windows 10 при входе
    Оцените статью