BAT batch file and folder operation code (with xcopy command detailed) _dos/bat

Source: Internet
Author: User
Tags file copy goto naming convention

Files, folder operations in batches, usage of the xcopy command.

One, build bat file automatically copy, delete command.

Example 1: Copy cd.dll file to Windows\System32 bat file contents:

Copy Code code as follows:

Copy Cd.dll%windir%\system32

Example 2: Uninstall the Cd.dll in the Windows\System32 directory, that is, delete the file copied above:

Copy Code code as follows:

Del%windir%\system32\cd.dll

Example 3: Delete the files in the download folder, as follows:

Copy Code code as follows:

Del C:\download\*.*

Note that the DEL command can only delete files, can not delete folders.
The Delete folder uses the RD command as follows:

Copy Code code as follows:

Rd Filemon

Here the Filemon folder and the bat file are in the same directory, eliminating the specific path. However, RD can only delete empty folders.
To delete a folder and all subdirectories and files inside it, use the RD command with the/s/q parameter.

The/s parameter indicates that the folder and the subdirectories and files below it are deleted
The/q parameter indicates that no acknowledgement is required

Example:

Copy Code code as follows:

RD 123/S/Q

Second, create a folder

Example: Under E:\My documents create 3 new folders at once: Newfolder1,newfolder2,newfolder3

Copy Code code as follows:

@echo off
MD "E:\My Documents\newfolder1"
MD "E:\My Documents\newfolder2"
MD "E:\My Documents\newfolder3"

Third, execute the program, open the file

Example 1: Execute the WinWord.exe program under C disk

Copy Code code as follows:

Start C:\WinWord.exe

Example 2: Open a file with a program, such as opening an HTM file with TheWorld.exe

Copy Code code as follows:
Start D:\TheWorld\TheWorld.EXE C:\ I have a love. htm

[Note]: Whether the program or file path contains spaces, you must enclose them in quotation marks:

Copy Code code as follows:

Start D:\TheWorld\TheWorld.EXE "E:\My documents\ I have a love. htm"

The quotation marks in all programs cannot be full-width and must be in half-width format, so it is best to close the Chinese character input system before entering quotes

XCOPY also has a number of functions that are added to the parameters to complete. The effect of the above/s/e parameters is the root directory and the empty directory are replicated in the past, some use examples and parameters are described as follows:
1, such as simply copy E:\ All files on disk to D:\ On the disk, enter "xcopy E:\*.* D:/s/h" on the line! If in the process of copying, you do not want him to copy the file! Then press the keyboard hotkey "Ctrl" + "Pause/break" two hot Keys to interrupt replication.
2. If you want to put e:\ Disk all the files copied to the D:\ disk, divided into today to copy a little, tomorrow and then copy some, the day after tomorrow and then copy some. Or for some reason the last time you didn't copy it, you want to copy it today. (This is often the case in the copy file) What's the best thing to do?
Then enter "Xcopy E:\*.* D:/s/h/d/y" on the line. He can find out that those files have been copied, and those files have not been copied.
3, of course, we will also encounter this situation, some files we have modified it! There is a new version on the Web. The original copy of the file we need to update the timely. So how do you sync updates? Of course, we can copy the latest good documents to the past, which is also a solution.
If there are many changes to the file, you will not remember that the document is updated. So much of the original copy of the file to be updated, to replicate the past, is not a province of energy, and error prone and some documents are forgotten!
That is the same as the above input "xcopy E:\*.* D:/s/h/d/y" on the line!
4, there is also a situation we often encounter, because copying a file is an error or the file is in use, and the replication work is stopped (for example, copying Windows XP on C disk) we want to skip over an error file and a file that is in use and continue copying other files.
Adding one more parameter "/C" is "Xcopy E:\*.* D:/s/h/d/c/y" On the line!
5, sometimes we want XCOPY to become automatic replication, and after the copy is completed, you can set up a batch file, new text document, renamed to Xcopy.bat of course, the extension is originally TXT format to be renamed BAT in this XCOPY. BAT input

Copy Code code as follows:

xcopy E:\*.* D:/s/h/c/y
Shutdown-s

Then double-click Xcopy.bat to run it, you can go out and play at this time. The computer copies the backup and completes the file itself and shuts down the computer automatically.

The

XCOPY source [destination] parameter (such as/s/e)
Source Specifies the file to copy. The
destination Specifies the location and/or name of the new file.
/A copies only files that have an archived set of surnames,
but does not change the surname.
/M copies only files that have an archived set of surnames,
and closes the archive name. The
/d:m-d-y copies files that have changed after a specified date or a specified date.
If you do not provide a date, copy only those files that have the source time
that are newer than the target time.
/exclude:file1[+file2][+file3] ... The
specifies a list of files that contain strings. If there is any
string that matches the absolute path of the file to be copied
, that file will not be copied.
For example, specifying a string such as \obj\ or. obj excludes all files under
directory obj or files with the
. obj extension.
/P prompts before creating each target file.
/S replicates directories and subdirectories, except for empty.
/E replicates directories and subdirectories, including empty. The
is the same as/s/e. Can be used to modify/T.
/V verifies each new file.
/W prompts you to press the key before copying.
/C Continue copying, even if there are errors.
/I if the target does not exist and more than one file is being copied,
it is assumed that the destination must be a directory.
/q does not display the file name when copying.
/F Displays the full source and destination file names when replicating.
/L Displays the files to be copied.
/G allows files that are not encrypted to be copied to
a target that does not support encryption.
/H also copies hidden and system files.
/R Overwrite read-only file.
/T Creates a directory structure, but does not copy files. Do not
include an empty directory or subdirectories. /T/E includes
empty directories and subdirectories.
/U copies only files that already exist in the target.
/K replicates the surname. The general Xcopy will reset the read-only surname.
/N is copied with the generated short name.
/o copies file ownership and ACL information.
/X Copy file Approval settings (implied/O).
/Y suppresses prompting to confirm overwriting a
existing target file. The
/-y causes a prompt to confirm overwriting a
existing target file.
/z Replicates network files in restart mode.

Xcopy command
Copy files and directories, including subdirectories.

Grammar
xcopy Source [destination] [/w] [/P] [/C] [///////////////////////////[/L] [/g] [/d[:mm-dd-yyyy]] [/s [/e]] [/t] [/k] }] [/n] [o] [/x] [/exclude:file1[+[file2]][+[file3]] [{/y|/-y}] [/z]

Parameters
Source
Required. Specifies the location and name of the file to be copied. The parameter must contain a drive or path.
Destination
Specifies the target of the file to be copied. This parameter can contain a drive letter and a colon, a directory name, a file name, or a combination of them.
/w
The following message is displayed and waits for your response before you begin copying files:
Press any key to begin copying file (s)

/P
Prompts you to confirm whether you want to create each destination file.
/C
Ignore the error.
/V
Validate each file when writing to the destination file to ensure that the destination file is exactly the same as the source file.
/q
Suppresses the display of XCOPY messages.
/F
Displays the source and destination file names when copying.
/L
Displays a list of files to copy.
/g
Creates a decrypted destination file.
/D[:MM-DD-YYYY]
Copies only those source files that have been changed after a specified date or a specified date. If you do not include the MM-DD-YYYY value, xcopy copies all new Source files that are newer than the existing destination file. This command-line option allows you to update files that have changed.
/u
Copy only the files that are already in the destination from source.
/I
If Source is a directory or contains wildcard characters, and destination does not exist, Xcopy assumes that destination specifies the directory name and creates a new directory. Xcopy then copies all the specified files to the new directory. By default, Xcopy prompts you to specify whether destination is a file or a directory.
/s
Copies non-empty directories and subdirectories. If omitted, the/s,xcopy will work in a directory.
/e
Copy all subdirectories, including empty directories. Use the/e,/s, and/T command-line options at the same time.
/t
Copy only the subdirectory structure (that is, the directory tree) and do not copy the files. To copy an empty directory, you must include the/e command-line option.
/k
Copies the file, and retains it in the destination file if the source file has a read-only family name. By default, Xcopy deletes a read-only surname.
/R
Copy a read-only file.
/h
Copies files that have a hidden and system file name. By default, Xcopy does not replicate hidden or system files.
/A
Copies only those source files that have the archive file's surname settings. /A does not modify the source file's archived file name. For information about how to set the last name of an archive file by using attrib, see.
/m
Copy the source file with the last name setting of the archive file. Unlike/A,/m closes the archive file of the file specified in the source as the last name. For information about how to set the last name of an archive file by using attrib, see.
/n
Create a copy using an NTFS short file or directory name. You need/n when you copy a file or directory from an NTFS volume to a FAT volume or when the target file system requires a FAT file system naming convention (that is, 8.3 characters). The destination file system can be FAT or NTFS.
/O
Copy file ownership and discretionary access control List (DACL) information.
/x
Copy file auditing settings and system access control List (SACL) information (including/O).
/exclude:filename1[+[filename2]][+[filename3]]
Specifies a list of files that contain strings.
/y
Suppresses prompting you to confirm that you want to overwrite an existing destination file.
/-y
Prompts you to confirm that you want to overwrite an existing destination file.
/z
Replicated over the network in restartable mode.
/?
Display Help at the command prompt.
Comments
Use/V
XOX

Using/exclude
Lists each string in a separate row for each file. If any of the strings listed match any part of the absolute path of the file to be copied, the file is excluded from the replication process. For example, if the string "\obj\" is specified, all files under the OBJ directory are excluded. If the string ". obj" is specified, all files with the. obj extension are excluded.

Using/z
If a connection is lost during replication (for example, if the server used for the connection is offline), the replication process is restored after the connection is reconnected. /z also displays the percentage of replication operations completed per file.

using/y in Copycmd environment variables
You can use/y in copycmd environment variables. Use/-y on the command line to override the command. By default, you are prompted to overwrite unless you run copy from within a batch script.

Copy an encrypted file
Copying an encrypted file to a volume that does not support EFS can result in an error. You should first decrypt the file or copy the file to a volume that supports EFS.

Ancillary files
To attach a file, specify a single destination file, multiple source files (using wildcard characters or file 1 + file 2 + file 3 format).

This section is the second part of the bat batch file and folder (with xcopy command details), please continue reading.

Default value for Destination
If you omit the destination,xcopy command, copy the file to the current directory.

Specify whether destination is a file or a directory
If destination does not contain an existing directory and does not end with a backslash (\), the following message appears:

Does destination specify a file name
or directory name on the target
(F = file, D = directory)?

If you want to copy one or more files to a file, press F. If you want to copy one or more files to a directory, press D.

Using the/I command-line option suppresses the display of this message, which causes Xcopy to determine that the destination is a directory when the source is multiple files or a directory.

To set the archive of destination files by using the xcopy command last name
The xcopy command creates a file with the archive's last name setting, regardless of whether the name is set in the source file. For more information about file names and attrib, see.

Compare Xcopy and diskcopy
If the disk's subdirectories contain files, and you want to copy them to a disk with a different format, use the xcopy command instead of diskcopy. Because the diskcopy command is a track to copy a disk, you need the same format for both the source and destination disks. The xcopy command does not have this requirement. You typically use xcopy unless you need a full copy of the disk image.

Xcopy's exit code
To handle the exit code returned by Xcopy, use the errorlevel parameter on the IF command line of the batch program. For an example of a batch program that uses "if" to handle exit codes, see. The following table lists each exit code and description.

Exit code Description
0 file copy no errors.
1 No files were found to copy.
2 users terminated xcopy by pressing CTRL + C.
4 An initialization error occurred. There is not enough memory or disk space, or an invalid drive name or syntax was entered on the command line.
5 A disk write error has occurred.

Example
To copy all files and subdirectories (including all empty subdirectories) to drive B from drive A, type:
xcopy a:b:/s/e

To include any system or hidden files in the previous example, add the/h command-line option as follows:
xcopy a:b:/s/e/h

To update the files in the \reports directory using the files changed after December 29, 1993 in the \rawdata directory, type:
xcopy \rawdata \reports/d:12-29-1993

To update all files that exist in \reports in the previous example, regardless of the date, type:
xcopy \rawdata \reports/u

If you want to obtain only the list of files that you want to copy from the previous command, and do not copy the files, type:
xcopy \rawdata \reports/d:12-29-1993/l > Xcopy.out

File Xcopy.out lists all the files that you want to copy.

To copy the \customer directory and all subdirectories to the directory on network drive H: \\Public\Address, keep the read only name and prompt you when creating a new file on H:, type:
xcopy \customer h:\public\address/s/e/k/P

To issue the previous command, make sure that Xcopy creates the directory when the \address directory does not exist, and suppresses the display of messages to display when the new directory is created, adding the/I command-line option as follows:
xcopy \customer h:\public\address/s/e/k/p/i

You can create a batch program to perform an XCOPY operation and use the IF batch command to handle an exit code when an error occurs.

Examples, using the replaceable xcopy source and target parameters:
@echo off
REM Copyit. BAT transfers all files in all subdirectories of
REM the source drive or directory (% 1) to the destination

REM drive or directory (% 2)

xcopy%1%2/s/e

if errorlevel 4 goto lowmemory
if errorlevel 2 goto abort
if errorlevel 0 goto exit

: lowmemory
echo Insufficient memory to copy files or
echo Invalid drive or command-line syntax.
Goto exit

: Abort
echo You pressed CTRL + C to end the copy operation.
Goto exit

: Exit

To use this batch program to copy all files in the C:\Prgmcode directory and its subdirectories to drive B, type:
Copyit C:\prgmcode B:

The command interpreter replaces%1 with B with C:\Prgmcode:. For%2, then use the Xcopy and/e and/s command-line options.
If Xcopy encounters an error, the batch program reads the exit code and moves to the label indicated in the appropriate IF ERRORLEVEL statement, and then displays the corresponding message and exits the batch program.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.