Copying via the Windows command line. XCopy utility or how to copy a folder on the command line

In order to fully work with files and directories in command line, we need to be able to create, move, copy and delete them. For each of these actions there is a corresponding command.

Although, to be precise, the same command is used to rename files and move them, because it performs the same operation of editing the table in which the directory stores the list of files located inside this directory.

Working with files on the command line

Creating a file

To create a file, you can use the touch command. In fact, the purpose of this command is to change the date of last access to the file and the date last change file.

Executing the command without parameters touch file_path/file will update the file's last access time and last modification time. If such a file does not exist, it will be created empty file with the name specified in the command.

To update only the access date, you need to use the ‑a parameter; to update only the modification date, use the ‑m parameter. You can also set not the current, but an arbitrary date, using the -t parameter, and specifying after it the date in the format MMDDhhmm[.SS], for example, 201203201359.11, which corresponds to 2012, 03 month 20 day 13 hours 59 minutes 11 seconds.

Creating a directory

To create directories, there is the mkdir command (from English m a k e dir sector).

Syntax: mkdir directory_name... , where directory name— this is the path and name of the directory to be created. You can specify multiple directories separated by a space.

If the path to the directory being created contains non-existent directories, the directory will not be created, but an error message will be displayed. To create multiple nested directories, use the -p parameter. For example, the command:

Mkdir -p folder1/folder2/folder3

will create all three directories folder1, folder2, folder3 in case they do not exist.

By default, directories are created with permissions set to 755; to create a directory with a different set of rights, use the ‑m access_rights parameter, for example: mkdir ‑m 750 folder .

Moving and changing the name of a file or directory

The names of files or directories are not stored in the files themselves, but are entries in a table that contains a list with the names of files and directories located inside this directory. Thus, when renaming a file or moving it to another directory, the same operation is performed - editing the directory table with a list of files. Therefore, the same mv command (from English m o v e).

Command syntax: mv source_file new_file where original file is the path to the file or directory and the name of the file or directory you want to move, and new file is the path to the destination where the file or directory needs to be moved, and the new name for the moved file or directory.

mv /Users/ixrevo/Downloads/hello.txt /Users/ixrevo/Desktop/hi.txt in this example we move the hello.txt file from the /Users/ixrevo/Downloads/ directory to the desktop /Users/ixrevo/Desktop/ . and the name of the moved file will be hi.txt (the contents of the file will remain unchanged).

Considering that our current directory was /Users/ixrevo/ , we could specify relative path to file:
cp Downloads/hello.txt Desktop/hi.txt

You can read more about the current directory in the article “Navigating the file system on the command line,” and about relative and absolute paths in the article “How the Mac OS X file system is structured.”

You can also rename a file or directory without moving it:

Mv hello.txt hi.txt

Or move without renaming:

Mv /Users/ixrevo/Downloads/hello.txt /Users/ixrevo/Desktop/

Why do you need to specify only the directory where to move the file or directory, without specifying a new name (or specify the old name).

Another way to use the mv command is useful for moving multiple files or directories: mv source_file_1 source_file_2 ... directory where source file_1, source file_2, etc.— paths and names of files or directories that need to be moved, and directory— this is the path to the directory where you need to move the files specified earlier.

Copying a file

To copy files and directories, there is the cp command, which is short for c o p y.

Command syntax: cp source_file new_file where original file is the path and name of the file that needs to be copied, and new file- this is the way to the copy being created file and the name of this copy.

For example, by running the command:

Cp /Users/ixrevo/Downloads/hello.txt /Users/ixrevo/Desktop/hi.txt

We will copy the file hello.txt from the /Users/ixrevo/Downloads/ directory to the desktop /Users/ixrevo/Desktop/, and the name of the copy of the file will be hi.txt.

The cp command also allows you to copy multiple files:

Cp source_file_1 source_file_2 ... directory

Source file_1, source file_2, etc.— paths to files and names of files that need to be copied, and directory- this is the path to the directory where you want to copy the files specified earlier.

Copying a directory

To copy directories, the cp command is used with the ‑R or ‑r option (from English r ecursive). The cp command executed with this option, if a directory was specified as the first argument, will copy the directory and all its contents, that is, all subdirectories and files.

Command syntax in in this case as follows:

Cp -r copied_directory destination

If you try to copy a directory without the -r parameter, the directory will not be copied, but an error message will be displayed.

Deleting a file

To delete files, use the rm command (from English r emo v e). Syntax: rm file... , where file is the path and name of the file that needs to be deleted. You can also specify multiple files to delete.

Removing a directory

There are several ways to delete directories. For example, you can use the command

Rmdir directory...

directory— this is the path and name of the directory that needs to be deleted. You can also specify multiple directories for deletion.

If the directory is not empty, that is, it contains nested files and/or folders, then the rmdir command will not be able to delete this directory and will display an error message.

The rm command with the -d parameter works similarly to the rmdir command.

Of course, deleting the contents of a directory first each time, and then the directory itself, is not very interesting, but, fortunately, there is a way to delete a directory along with all its contents. To do this, run the rm command with the ‑R or ‑r parameter, and then all files and directories specified in the rm ‑r directory... command will be deleted along with all subdirectories. But be careful not to delete anything you need with this command, especially since there is no Recycle Bin on the command line and files and directories are permanently deleted.

COPY command

Source [+ source [+ ...]] [result]:

sourceThe names of one or more files to be copied.

/AFile is an ASCII text file.

/BFile is a binary file.

resultThe directory and/or name for the target files.

/VChecks that files are copied correctly.

/N Use short names if possible when copying files whose names do not comply with the 8.3 standard.

/Y Suppresses the confirmation prompt to overwrite an existing target file.

/-Y Mandatory confirmation request to overwrite an existing target file.

The /Y switch can be set via the COPYCMD environment variable.

The /-Y command line switch overrides this setting.

By default, confirmation is required unless the COPY command is executed in a batch file

XCOPY command

To combine files, specify one target and multiple source files using wildcards or the format "file1+file2+file3+...". Copying files and directory structures.

source [result] ] ]

[+file3]...]

source Copied files.

result The location and/or names of the new files.

/A Copies only files with the archive attribute set. The attribute itself does not change.

/M Copies only files with the archive attribute set. After copying, the attribute is removed.

/D: dateCop. files not previously modified specified date. If the date is not specified, only the final files

/EXCLUDE:file1[+file2][+file3]...List of files containing strings. When any of the strings matches any part absolute path to the copied file, such a file is excluded from the copy operation. For example, by specifying the string obj or .obj, you can exclude all files from the obj folder or all files with the obj extension, respectively.

/P Print prompts before creating each new file.

/S Copies directories with subdirectories (except empty ones).

/E Copy directories with subdirectories (including empty ones).

Equivalent to the /S /E switch combination. Compatible with /T switch. /V

Comparison of final files with source ones.

/W Prompts you to press a key before copying.

/C Continue copying, regardless of errors.

/I If the result does not exist and multiple files are copied, the directory name is assumed to be specified.

/Q Prevents displaying the names of copied files.

/F Displays the full names of source and destination files.

/L Displays the names of copied files.

/H Copies hidden and system files (among others).

/R Allow replacement of read-only files.

/T Creates a directory structure (except empty directories) without copying files. If you need to create empty directories and subdirectories, use the /T /E switch combination.

/U Copies only files already present in the destination directory.

/K Copies the Read Only attribute (usually it is reset).

/N Use short names when copying.

/O Copies owner information and ACL information.

/X Copies file audit settings (requires /O).

/Y Suppresses the confirmation prompt to overwrite an existing target file. /-Y Mandatory confirmation request to overwrite an existing target file.

/Z Copy network files with renewal.

The /Y switch can be set via the COPYCMD environment variable. The /-Y command line switch overrides this setting.

Example: 1. Copy text.txt to A:TasksNew

2. When copying, the file name will be changed

Team XCOPY used to copy files and directories while maintaining their structure. Compared to the team COPY has more ample opportunities and is the most flexible copy tool in the Windows command line

Command line format:

XCOPY source [target] ] ] [+file3]...]

Command line options:

source- Copied files.

target_object- Location or names of new files.

/A- Copying only files with the archive attribute set; the attribute itself does not change.

/M- Copying only files with the archive attribute set; after copying the attribute is removed.

/D:m-d-y- Copying files modified no earlier than the specified date. If no date is specified, only the target files that are older than the source files are replaced.

/EXCLUDE:file1[+file2][+file3]...- A list of files containing lines with criteria for excluding files and folders from the copying process. Each line must be on a separate line in the file. If any of the strings matches any part of the absolute path to the file being copied, that file is excluded from the copy operation. For example, by specifying the string \obj\ or .obj, you can exclude all files in the obj folder or all files with the OBJ extension, respectively.

/P- Output prompts before creating each new file.

/S- Copy only non-empty directories with subdirectories.

/E- Copying directories with subdirectories, including empty ones. Equivalent to the /S /E switch combination. Compatible with /T switch.

/V- Checking the size of each new file.

/W- Prompts you to press a key before copying.

/C- Continue copying regardless of errors.

/I- If target object does not exist and multiple files are copied, the target is assumed to specify a directory.

/Q- Prohibition of displaying names of copied files.

/F- Output full names of source and target files.

/L- Displays the names of copied files.

/G- Copying encrypted files to a target directory that does not support encryption.

/H- Copying, among others, hidden and system files.

/R- Overwriting read-only files.

/T- Create directory structure without copying files. Empty directories and subdirectories are not included in the copy process. To create empty directories and subdirectories, use the /T /E switch combination.

/U- Copy only files already present in the target directory.

/K- Copying attributes. Using the XCOPY command typically resets the Read-Only attributes.

/N- Using short names when copying.

/O- Copy owner information and ACL data.

/X- Copying file audit parameters (implies the /O key).

/Y- Suppress confirmation prompt to overwrite existing target file.

/-Y- Prompt for confirmation to overwrite an existing target file.

/Z- Copy network files with resume.

/J- Copy using unbuffered I/O. Recommended for very large files.

Key /Y can be set via the COPYCMD environment variable.

Key /-Y command line overrides this setting.

Examples of using XCOPY

XCOPY /?- provide brief information on using the command.

xcopy C:\users D:\copy1- copy files from the C:\users directory to the D:\copy1 directory. Copying will be performed without subdirectories and only files without the “Hidden” and “System” attributes. The copied files will have the attribute set to Archival. If the directory being copied to does not exist, the user will be prompted with the following message:

What does D:\copy1 mean:
file or directory name
(F = file, D = directory)? D

After answer D the target directory will be created and the copy will be performed to D:\COPY1\. To suppress the request to create the target directory, use the /I option:

xcopy C:\users D:\copy1 /I

xcopy C:\users D:\copy1 /H /Y /C- copying files, including hidden and system ones, suppressing the request to overwrite existing ones and resuming in case of an error. If a file existing in the target directory has the Read-Only attribute, then the copy is not performed. To overwrite such files, use the key /R

xcopy C:\users D:\copy1 /H /Y /C /R /S- copy all files and subdirectories (/S) overwriting existing ones without prompting (/Y), including hidden and system ones. (/H) with overwriting files with the Read-Only attribute (/R) and ignoring errors (/C)

xcopy C:\users D:\copy1 /H /Y /C /R /S /EXCLUDE:C:\users\listnotcopy.txt- the same as in the previous case, but text file C:\users\listnotcopy.txt specifies exceptions from the copying procedure. Example file contents:

\User1\- exclude the directory C:\users\user1 from copying
All Users exclude the directory C:\users\All Users from copying
de*.*- exclude from copying all files and directories starting with the letter combination "de"

xcopy C:\users\*.exe D:\copy1 /H /Y /C /R /S /EXCLUDE:C:\users\listnotcopy.txt- the same as in the previous example, but only copying is performed executable files with extension .exe.

xcopy %TEMP%\*.ini D:\copy1\ini /H /Y /C /R /S /I- copying all files with the extension .ini from the catalog temporary files to the catalog D:\copy1\ini\. If the target subdirectory \ini\ does not exist, then it will be created without prompting the user (/I) .

xcopy %TEMP%\*.ini D:\copy1\ini /H /Y /C /R /S /I /D:09-16-2013- the same as in the previous example, but only those files that have a modified date of September 16, 2013 or older are copied.

xcopy C:\ D:\copy1\LISTDIR /H /Y /C /R /S /I /E /T- create a folder structure of drive C: in the directory D:\copy1\LISTDIR. Files are not copied. Only folders are copied, including empty, hidden and system folders.

xcopy C:\ D:\copy1\LISTDIR /H /Y /C /R /S /I /E /T /D:09-16-2013 recreate the folder structure of drive C: in the D:\copy1\LISTDIR directory, with the modification date of September 16, 2013 and later.

To add new files to directories and update existing ones to more later versions, you can use the REPLACE command.

A complete list of commands can be displayed by typing HELP at the command line.

CD Team

The current directory can be changed using the command

CD [drive:][path\]

The path to the required directory is specified taking into account the above comments. For example, the CD\ command moves to the root directory of the current drive. If you run the CD command without parameters, the names of the current drive and directory will be displayed.

COPY command

One of the most repetitive tasks when working on a computer is copying and moving files from one location to another. To copy one or more files, use the COPY command.

The syntax for this command is:

COPY source [+ source [+ ...]] [result]

Short description The parameters and switches of the COPY command are given in the table.

Table 1.1. COPY Command Options and Options

Parameter

Description

source

Name of the file or files being copied

The file is an ASCII text file, meaning the end of the file is indicated by an ASCII character 26 ( +)

The file is binary. This switch specifies that the command interpreter should, when copying, read from the source the number of bytes specified by the size in the directory of the copied file

result

Directory to place the copy result and/or name of the file being created

Verifying the correctness of the copy by comparing the files after copying

Disabling confirmation request to replace files

Enabling confirmation request to replace files

Here are examples of using the COPY command.

Copying the abc.txt file from the current directory to the D:\PROGRAM directory under the same name:

COPY abc.txt D:\PROGRAM

Copying the abc.txt file from the current directory to the D:\PROGRAM directory under the new name def.txt:

COPY abc.txt D:\PROGRAM\def.txt

Copy all files from txt extension from drive A: to the "My Documents" directory on drive C:

COPY A:\*.txt "C:\My Documents"

If you do not specify a target file in the command, the COPY command will create a copy of the source file with the same name, creation date and time as the source file, and place new copy to the current directory on current disk. For example, in order to copy all the files from the root directory of drive A: to the current directory, just run the following short command:

COPY A:\*.*

When copying, you can specify the names of not only files, but also computer devices as a source or result. For example, in order to print the file abc.txt on a printer, you can use the command to copy this file to the PRN device: COPY abc.txt PRN

Another interesting example: let's create a new text file and write information into it, without using text editor. To do this, just enter the command COPY CON my.txt, which will copy what you type on the keyboard into the file my.txt (if this file existed, it will be overwritten, otherwise it will be created). To complete the input, you must enter the end-of-file symbol, that is, press the keys +.

The COPY command can also merge (glue) multiple files into one. To do this, you need to specify a single resulting file and several source ones. This is achieved by using wildcards (? and *) or the format file1 + file2 + file3. For example, to combine files 1.txt and 2.txt into file 3.txt, you can specify next command:

COPY 1.txt+2.txt 3.txt

Combining all files with the dat extension from the current directory into one file all.dat can be done like this:

COPY /B *.dat all.dat

The /B switch is used here to prevent truncating the files being joined, since when combining files, the COPY command considers text files by default.

If the name of the target file is the same as the name of one of the copied files (except the first one), then the original contents of the target file are lost. If the target file name is omitted, the first file in the list is used as its name. For example, the COPY 1.txt+2.txt command will append the contents of file 1.txt to the contents of file 2.txt. The COPY command can also be used to assign to a file current date and time without modification of its contents. To do this you need to enter a command like

COPY /B 1.txt +,

Here, commas indicate the omission of the receiver parameter, which leads to the desired result.

The COPY command also has its drawbacks. For example, it cannot be used to copy hidden and system files, files zero length, files from subdirectories. Also, if when copying a group COPY files will encounter a file that is in this moment cannot be copied (for example, it is busy with another application), then the copying process will be completely interrupted and the remaining files will not be copied.

XCOPY command

The problems mentioned in the description of the COPY command can be solved using XCOPY commands, which provides much more options when copying. It should be noted, however, that XCOPY can only work with files and directories, but not with devices.

The syntax for this command is:

XCOPY source [result] [keys]

The XCOPY command has many options, we will only touch on a few of them. The /D[:[date]] key allows you to copy only files modified on or after the specified date. If the date parameter is not specified, then copying will be performed only if the source is newer than the result. For example, the command

XCOPY "C:\My Documents\*.*" "D:\BACKUP\My Documents" /D

will copy to the "D:\BACKUP\My Documents" directory only those files from the "C:\My Documents" directory that have been changed since the last such copying or that were not in "D:\BACKUP\My Documents" at all.

The /S switch allows you to copy all non-empty subdirectories in the source directory. Using the /E switch, you can copy all subdirectories, including empty ones.

If the /C switch is specified, copying will continue even if errors occur. This can be very useful when copying operations are performed on groups of files, for example, when backup data.

The /I switch is important when multiple files are copied and the destination file is missing. When this switch is specified, the XCOPY command assumes that the destination file must be a directory. For example, if you specify the /I switch in the command to copy all files with the txt extension from the current directory to the TEXT subdirectory that does not yet exist,

XCOPY *.txt TEXT /I

then the TEXT subdirectory will be created without additional requests.

The /Q, /F and /L keys are responsible for the display mode when copying. When you specify the /Q key, file names are not displayed when copying, and the /F key - the full paths of the source and result are displayed. The /L switch means that only files that need to be copied are displayed (the copying itself is not performed).

Using the /H switch, you can copy hidden and system files, and using the /R switch, you can replace files with the Read-Only attribute. For example, to copy all files from the root directory of the C: drive (including system and hidden ones) to the SYS directory on the D: drive, you need to enter the following command:

XCOPY C:\*.* D:\SYS /H

The /T switch allows XCOPY to copy only the source directory structure, without duplicating the files in those directories, and empty directories and subdirectories are not included. In order to still include empty directories and subdirectories, you need to use the /T /E key combination.

Using XCOPY, when copying, you can update only the already existing files(new files are not written). To do this, use the /U key. For example, if the C:\2 directory contained the files a.txt and b.txt, and the C:\1 directory contained the files a.txt, b.txt, c.txt and d.txt, then after executing the command

XCOPY C:\1 C:\2 /U

in the C:\2 directory there will still be only two files a.txt and b.txt, the contents of which will be replaced by the contents of the corresponding files from the C:\1 directory. If a file with the Read-Only attribute was copied using XCOPY, then By default, this attribute will be removed from the copy file. In order to copy not only data, but also the entire attributes of a file, you must use the /K key.

The /Y and /-Y switches determine whether confirmation should be requested before replacing files when copying. /Y means that such a request is needed, /-Y means it is not needed.

DIR Team

Another very useful command is DIR [drive:][path][filename] [keys], which is used to display information about the contents of drives and directories. The [drive:][path] parameter specifies the drive and directory whose contents should be displayed. The [filename] parameter specifies the file or group of files to be included in the list. For example, the command

DIR C:\*.bat

will display all files with bat extension V root directory drive C:. If you specify this command without parameters, the disk label and serial number, the names (in short and long versions) of files and subdirectories located in the current directory, as well as their date and time are displayed. latest modification. It then displays the number of files in the directory, the total size (in bytes) occupied by the files, and the amount of free disk space. For example:

The volume on device C is labeled PHYS1_PART2 Serial number volumes: 366D-6107 Contents of the folder C:\aditor .<ПАПКА> 25.01.00 17:15 . .. <ПАПКА>01/25/00 17:15 .. TEMPLT02 DAT 227 08/07/98 1:00 templt02.dat UNINST1 000 1 093 03/02/99 8:36 UNINST1.000 HILITE DAT 1 082 09/18/98 18:55 hilite.dat LT01 DAT 48 07.08 .98 1:00 templt01.dat UNINST0 000 40 960 04/15/98 2:08 UNINST0.000 TTABLE DAT 357 08/07/98 1:00 ttable.dat ADITOR EXE 461 312 12/01/99 23:13 aditor.exe README TXT 3 9 74 01/25/00 17:26 readme.txt ADITOR HLP 24 594 10/08/98 23:12 aditor.hlp TEXT~1 TXT 0 03/11/01 9:02 Text file.txt 11 files 533 647 bytes 2 folders 143 261 696 bytes free

Using the DIR command switches, you can set various modes location, filtering and sorting. For example, when using the /W switch, the list of files is displayed in wide format with the maximum possible number of file or directory names on each line. For example:

The volume in device C is labeled PHYS1_PART2 Volume Serial Number: 366D-6107 Contents of folder C:\aditor [.] [..] TEMPLT02.DAT UNINST1.000 HILITE.DAT TEMPLT01.DAT UNINST0.000 TTABLE.DAT ADITOR.EXE README. TXT ADITOR.HLP TEXT~1.TXT 11 files 533,647 bytes 2 folders 143,257,600 bytes free

Using the /A[[:]attributes] key, you can display the names of only those directories and files that have the specified attributes (R - “Read Only”, A - “Archive”, S - “System”, H - “Hidden”, the prefix "–" has the meaning NOT). If the /A switch is used with more than one attribute value, only those file names will be displayed whose attributes all match the specified ones. For example, to display the names of all files in the root directory of the C: drive, which are both hidden and system, you can use the command

DIR C:\ /A:HS

and to display all files except hidden ones - the command

DIR C:\ /A:-H

Note here that the directory attribute corresponds to the letter D, that is, in order, for example, to display a list of all directories on the C: drive, you need to specify the command

DIR C:/A:D

The /O[[:]sorting] switch specifies the order in which the directory contents are sorted when output by the DIR command. If this switch is omitted, DIR prints the names of files and directories in the order in which they are contained in the directory. If the /O switch is specified and the sorting option is not specified, then DIR displays the names in alphabetical order. In the sorting parameter you can use the following values: N - by name (alphabetical), S - by size (starting with the smallest), E - by extension (alphabetical), D - by date (starting with the oldest), A - by download date (starting with the oldest ones), G - start the list with directories. The prefix "–" means reverse order. If you specify more than one sort order value, files are sorted by the first criterion, then by the second, and so on.

The /S switch means displaying a list of files from a given directory and its subdirectories.

The /B switch lists only directory names and file names (in long format) one per line, including extension. In this case, only basic information is displayed, without final information. For example:

Templt02.dat UNINST1.000 hilite.dat templt01.dat UNINST0.000 ttable.dat aditor.exe readme.txt aditor.hlp Text file.txt

MKDIR and RMDIR commands

To create a new directory and delete an existing empty directory, use MKDIR teams[drive:]path and RMDIR [drive:]path [keys] respectively (or their short equivalents MD and RD). For example:

MKDIR "C:\Examples" RMDIR "C:\Examples"

The MKDIR command cannot be executed if the directory or file with given name already exists. The RMDIR command will fail if the directory being deleted is not empty.

DEL command

You can delete one or more files using the command

DEL [drive:][path]filename [keys]

Are wildcards used to delete multiple files at once? And *. The /S key allows you to delete specified files from all subdirectories, the /F key allows you to forcefully delete read-only files, the /A[[:]attributes] key allows you to select files for deletion by attributes (similar to the /A[[:]attributes key ] in the DIR command).

REN Team

You can rename files and directories using the RENAME (REN) command. The syntax of this command is as follows:

REN [drive:][path][directory1|file1] [directory2|file2]

Here directory1|file1 specifies the directory/file name to be changed, and directory2|file2 specifies the new directory/file name. Can wildcards be used in any parameter of the REN command? And *. In this case, the symbols represented by the templates in the file2 parameter will be identical to the corresponding symbols in the file1 parameter. For example, to change the extension to doc for all files with a txt extension in the current directory, you need to enter the following command:

REN *.txt *.doc

If a file named file2 already exists, the REN command will stop executing and display a message indicating that the file already exists or is in use. Additionally, the REN command cannot specify a different drive or directory to create the resulting directory and file. For this purpose, you need to use the MOVE command, designed to rename and move files and directories.

MOVE command

The command syntax for moving one or more files is:

MOVE [drive:][path]file_name1[,...] resulting_file

The command syntax for renaming a folder is:

MOVE [drive:][path]directory1 directory2

Here, the resulting_file parameter specifies the new location of the file and can include a drive name, a colon, a directory name, or a combination of these. If only one file is being moved, you can specify a new file name. This allows you to immediately move and rename the file. For example,

MOVE "C:\My Documents\list.txt" D:\list.txt

If the /-Y switch is specified, a confirmation request will be issued when creating directories and replacing files. The /Y switch cancels issuing such a request.

COPY command when working on the command line, allows you to copy one or more files. The command syntax is: COPYsource[+ source [+ ...]][result]. The source is the name of the file being copied, and the result is the directory where the copy of that file will be placed and/or the name of the file being created.

For example, let’s create some file on drive “C” (I created the text file “robot.txt”), and on drive “D” some folder (I created folder “folder1”). Then the command to copy the file “robot.txt” to the folder “folder1” looks like this: copy robot.txt d:\folder1

If you need to copy a file but change its name, you need to write the following command: copy robot.txt d:\folder1\file.txt In this case, the file “robot.txt” will be copied to the folder “folder1”, but with the name “file.txt”.

In order to copy all files with the selected extension, you must put an asterisk “*” instead of the file name. For example, let’s create some folder on the “C” drive (I created the “papka” folder) and copy into it all the text files from the “folder1” folder located on the “D” drive. The command will look like: copy d:\folder1\*.txt c:\papka

If you do not specify a directory as [result], the copy command will copy the file(s) and place them in the current directory. In this case, the copied files will have the same name, creation date and time as the original file. For example, let’s create a text file named “robot1.txt” in the folder “folder1” located on drive “D”. After that, in the command line, change the current directory from “C:\>” to “C:\Fapka”. Then the copy d:\folder1\robot1.txt command will copy the file “robot1.txt” from the “folder1” folder and place it in the current directory, i.e. to the “papka” folder located on the “C” drive. Moreover, the name, date and time of creation of the copied file will be identical to the original file.

In order to copy all files from a specific directory, you must put an asterisk “*” instead of the file name and an asterisk “*” instead of the file extension. For example, let’s create several files in the folder “folder1” that have various extensions (Word document, Rar archive etc.). Then the command to copy all files from the folder “folder1” to drive “C” will look like: copy d:\folder1\*.* c:\


The [source] or [result] of copying can be not only files, but also various devices computer. The following device names are supported on Windows:

  • LPT1 – LPT3 (parallel ports).
  • COM1 – COM3 (serial ports).
  • CON (terminal, for output it is the computer screen, for input it is the keyboard).
  • PRN (printer).
  • AUX (device connected to serial port 1).

For example, using the command line and using the “copy” command, you can create a text file and write information to it: copy con f1.txt . The copy con f1.txt command will create a text file “f1.txt” and copy the characters you enter from the keyboard into it.

After entering the command copy con f1.txt, press Enter and enter the words that need to be saved (I entered command com). After entering, you must mark the end of the file (press Ctrl+Z).
Using the copy command you can merge multiple files into one file. For example, let’s create two files f2.txt and f3.txt on drive “C” (we created the f1.txt file earlier) and write some information to these files. Then the team copy f1.txt+f2.txt+f3.txt d:\f4.txt will copy the contents of the files “f1.txt”, “f2.txt”, “f3.txt” to the file “f4.txt”, which will be automatically created on the “D” drive.

If, when merging files, you do not specify the file - [result], then all information will be saved in the first file. For example, the command copy f1.txt+f2.txt+f3.txt will add the contents of file f1.txt to the contents of files f2.txt and f3.txt

Key /Y disables the message about replacing files. By default, when copying files, if such a file exists, a message warning about the replacement is displayed on the screen. The user can agree with the replacement (press Y) or disagree (press N). The /Y switch disables this message and copying occurs immediately. For example, let’s create a file vov.txt on drive “C” and copy it to drive “D”: copy vov.txt d:\
Then copy this file again. In this case, the message “Replace d:\vov.txt) is displayed