Windows copy folder and files

Windows copy command syntax and examples

Using copy command, we can copy files from one directory to another directory. This command is similar to the Linux cp command, but it does not match with the full functionality of cp. Windows copy command can be used to copy files only, we can’t copy directories.

The syntax and usecases of copy command are explained below with examples.

Copy the contents of a file to another file

Example: To copy a file from c:\data\file1.doc to D:\backup\file2.doc

If the destination file already exists you will be prompted for confirmation. To suppress this confirmation you can use /Y switch with copy command. This would be useful if you are executing copy command from a batch file.

If the destination file exists, the above command will overwrite the same without asking the user for confirmation.

Copy file to another directory

When we specify a directory path as the destination, the files will be copied with the same name. We can assign a different name by specifying the new name in the destination path. Example is shown below.

To copy the file 1.doc loated at c:\data\documents to the directory c:\data\newdocs

Copy files with white space in name

If the file name has white space within it, we can wrap up the name in double quotes.
Example: To copy file, my resume.doc to another folder

Copy multiple files

We can’t specify multiple file names in copy command. However, we can use wildcards to identify a group of files and then copy all of them in a single command.
For example, to copy all excel files from current folder to another folder F:\backup

To copy all files in current folder to another folder

Use of environment variables

We can use environment variables in the copy command to specify the path of the folders. Like USERPROFILE, SystemRoot, ProgramFiles, TEMP, WINDIR, APPDATA, HOMEPATH.

For example, to copy a file to a user’s documents folder

The above command copies the file to the My Documents folder of the current logged in user.

You may also want to read

Windows «copy» is funny. Type «copy 1 2» and the file «1» will be copied into a new file «2». Now separate them by a plus sign instead of a space (copy 1+2) and you’ll concatenate 1 and 2 and replace the old file «1» with the result of the concatenation!

Yes, we can concatenate two or more files using copy command. You need to separate the list of files using +. You can redirect the resultant data to a new file also.

The above command will not alter the file 1. It creates a new file 3 with the concatenated data of 1 & 2. If no file name is provided it stores the result in the first file.

My Win7 cannot find a copy command, and when i run xcopy, a window flashes and exits.

I have the same problem. If you solved it, could you please explain how?

If you can not find your copy.exe file, you can download it to your windows directory or C:\ Directory depending the setting on your OS you should also be able to copy and run it from system32 or system folder.

how can i combine 2 .exe files and be able to use both after concatenation

I want to copy 2 different files(.exe,.config) from source to destination server of windows.
can you please help me on this command.

Hello i have a problem with my cmd windows 7.when i try to copy a command. Like help > file.pdf. i mean in extension pdf because i have this problem only with .pdf extension but not with .txt.So whe i execute the command. No problem. Then when i go to open the file.pdf ftom user destination the file.pdf doesn’t open say that is corrupted.please do help me .thanks

Читайте также:  Create partition primary windows 10

i have a file contain many lines as sources and another file has the same numbre of lines as destinations. i want to copy first line as source( c:/test/*.txt) to first line in destination ( d:/test2/), secend line ( c:/test/*.pdf) to second line in destination ( E:/test3/)……

Can I use the DOS/Windows “COPY” command in a BAT file to copy a file or a short string of text to computer memory and then paste (Ctrl +V) that string or file into a document?

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.

Читайте также:  Windows defender добавить исключение cmd

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.

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.

Читайте также:  Панель быстрого поиска windows

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.

(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.

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