Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of File and folder Operation in BAT batch processing

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail the sample analysis of file and folder operations in BAT batches. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.

The operation of files and folders in batch processing, and the usage of xcopy command.

First, build bat files to automatically execute copy and delete commands.

Example 1: copy the cd.dll file to the bat file of windows\ system32:

The code is as follows:

Copy cd.dll% windir%\ system32

Example 2: uninstall cd.dll in the windows\ system32 directory, that is, delete the files copied above:

The code is as follows:

Del% windir%\ system32\ cd.dll

Example 3: delete the files in the download folder, as shown below:

The code is as follows:

Del C:\ DOWNLOAD\ *. *

Note that the del command can only delete files, not folders.

Delete a folder using the rd command, as follows:

The code is as follows:

Rd Filemon

Here the filemon folder is in the same directory as the bat file, so the specific path is omitted. However, rd can only delete empty folders.

To delete a folder and all its subdirectories and files, use the rd command with the / s / Q parameter

The / s parameter deletes the folder and its subdirectories and files

The / Q parameter indicates that no confirmation is required.

Example:

The code is as follows:

Rd 123 / s / q

Second, create a folder

Example: create 3 new folders at a time under E:\ My documents: Newfolder1,Newfolder2,Newfolder3

The code is as follows:

@ echo off

Md "E:\ My documents\ Newfolder1"

Md "E:\ My documents\ Newfolder2"

Md "E:\ My documents\ Newfolder3"

Third, execute the program and open the file

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

The code is as follows:

Start C:\ WinWord.exe

Example 2: open a file with a program, such as a htm file with TheWorld.exe

The code is as follows:

Start d:\ TheWorld\ TheWorld.EXE C:\ I have a relationship .htm

[note]: whether the program or file path contains spaces, it must be enclosed in quotation marks:

The code is as follows:

Start d:\ TheWorld\ TheWorld.EXE "e:\ My documents\ I have a love .htm"

Quotation marks in all programs cannot be full-width, but must be in half-width format, so it is best to turn off the Chinese character input system before entering quotation marks.

XCOPY also has many functions, which are accomplished by adding parameters. The function of the / s / e parameter above is to copy both the subdirectory and the empty directory. Some examples and parameters are described as follows:

1. If you simply copy all the files on the E:\ disk to the D:\ disk, enter "xcopy e:\ *. * d: / s / h"! If in the process of copying, you don't want him to copy the file! Then press the keyboard hotkeys "Ctrl" + "Pause / break" to interrupt the copy.

2. If you want to copy all the files on the E:\ disk to the D:\ disk, copy a little bit today, copy some tomorrow, and copy some the day after tomorrow. Or for some reason, I didn't finish the copy last time, but I want to copy it again today. (this often happens in copied files.) what should I do?

Then type "xcopy e:\ *. * d: / s / h / d / y". He can find out which documents have been copied and those documents have not been copied yet.

Of course, we will also encounter this situation, some documents have been modified! There is the latest version on the Internet again. We should update the original backup file in time. So how can I synchronize updates? Of course, we can copy the latest good documents, which is also a solution.

If there are a lot of revised documents, I can't remember clearly that the document has been updated. So many original backup files have to be updated and copied in the past, which is not a labor-saving task, and it is easy to make mistakes and some documents are forgotten!

Then just enter "xcopy e:\ *. * d: / s / h / d / y" as above!

4, there is another situation that we often encounter, because there is an error copying a file or the file is in use, and stop copying work (for example, copying windows xp on C disk) We want to skip an error file and a file in use and continue to copy other files at this time.

Just add one more parameter "/ c" is "xcopy e:\ *. * d: / s / h / d / c / y"!

5. Sometimes we want XCOPY to be copied automatically, and when the copy is completed, we can turn off the computer. We can create a batch file, create a new text document, and change the name to XCOPY.BAT. Of course, if the extension is originally in TXT format, we have to change it to BAT and enter it in this XCOPY.BAT.

The code is as follows:

Xcopy e:\ *. * d: / s / h / c / y

Shutdown-s

Then double-click xcopy.bat to run it, and you can go out and play at this time. The computer will copy the backup files by itself and then shut down the computer automatically.

XCOPY source [destination] parameters (e.g. / s / e)

Source specifies the file to copy.

Destination specifies the location and / or name of the new file.

/ An only copies files with archived surname sets

But do not change the family name.

/ M only copies files with archived surname sets

And close the archive belonging to the last name.

/ D:m-d-y copies files that change on or after the specified date.

If no date is provided, only those source times are copied

A file newer than the target time.

/ EXCLUDE:file1 [+ file2] [+ file3]...

Specifies a list of files that contain strings. If there is any

The absolute path between the string and the file to be copied

Match, that file will not be copied.

For example, specifying a string such as\ obj\ or .obj excludes

All files under the directory obj or with

The file with the .obj extension.

/ P prompt before creating each target file.

/ S copies directories and subdirectories except for empty ones.

/ E copy directories and subdirectories, including empty ones.

Same as / S / E. Can be used to modify / T.

/ V validate each new file.

/ W prompts you to press the key before copying.

/ C continues to copy even if there is an error.

/ I if the target does not exist, more than one file is being copied

Assume that the target must be a directory.

The file name is not displayed when / Q is copied.

/ F displays the full source and destination file names when replicating.

/ L displays the files to be copied.

/ G allows unencrypted files to be copied to

Encrypted targets are not supported.

/ H also copies hidden and system files.

/ R rewrites the read-only file.

/ T creates a directory structure, but does not copy files. No.

Include empty directories or subdirectories. / T / E includes

Empty directories and subdirectories.

/ U copies only files that already exist in the target.

/ K copy the last name. A normal Xcopy resets the read-only family name.

/ N is copied with the generated short name.

/ O copy file ownership and ACL information.

/ X copy file audit settings (implied / O).

/ Y disables prompts to confirm rewriting a

Existing target file.

/-Y causes a prompt to confirm that a

Existing target file.

/ Z copies network files in restart mode.

Xcopy command

Copy files and directories, including subdirectories.

Grammar

Xcopy Source [Destination] [/ w] [/ c] [/ v] [/ Q] [/ f] [/ l] [/ g] [/ d [: mm-dd-yyyy]] [/ t] [/ k] [/ r] [/ h] [{/ a | / m}] [/ n] [/ o] [/ x] [/ exclude:file1 [+ [ File2]] [+ [file3]] [{/ y | /-y}] [/ z]

Parameters.

Source

Necessary. Specify the location and name of the file to copy. The parameter must contain a drive or path.

Destination

Specify the destination of the file to copy. This parameter can contain drive letters and colons, directory names, file names, or a combination of them.

/ w

The following message appears and waits for your response before starting to copy the file:

Press any key to begin copying file (s)

/ p

Prompts you to confirm that you want to create each target file.

/ c

Ignore the error.

/ v

Verify each file when writing to the target file to ensure that the target file is exactly the same as the source file.

/ q

Suppresses the display of xcopy messages.

/ f

The source and destination file names are displayed when copying.

/ l

Displays a list of files to copy.

/ g

Create a decrypted target file.

/ d [: mm-dd-yyyy]

Copy only those source files that have changed on or after the specified date. If you do not include the mm-dd-yyyy value, xcopy copies all Source files that are newer than existing Destination files. This command line option allows you to update changed files.

/ u

Only files that are already in destination are copied from source.

/ i

If Source is a directory or contains wildcards and Destination does not exist, xcopy assumes that destination specifies a 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

Copy non-empty directories and subdirectories. If / sforce xcopy is omitted, it will work in a directory.

/ e

Copy all subdirectories, including empty directories. Use both the / e, / s, and / t command line options.

/ t

Only the subdirectory structure (that is, the directory tree) is copied, not files. To copy an empty directory, you must include the / e command line option.

/ k

Copy the file, and if the source file has a read-only last name, keep the last name in the destination file. By default, xcopy deletes the read-only last name.

/ r

Copy a read-only file.

/ h

Copy files with hidden and last names of system files. By default, xcopy does not copy hidden or system files.

/ a

Copy only those source files that have the archive file family name setting. / a does not modify the last name of the archive file of the source file. For information about how to set the last name of an archive file by using attrib, see.

/ m

Copy the source file with the archive file family name setting. Unlike / a, / m closes the last name of the archive file specified in the source. For information about how to set the last name of an archive file by using attrib, see.

/ n

Create a copy using a NTFS short file or directory name. / n is required to copy a file or directory from a NTFS volume to a FAT volume or when the destination file system requires an FAT file system naming convention (that is, 8.3 characters). The destination file system can be FAT or NTFS.

/ o

Copy file ownership and freely selected access control list (DACL) information.

/ x

Copy file audit settings and system access control list (SACL) information (including / o).

/ exclude:filename1 [+ [filename2]] [+ [filename3]]

Specifies a list of files that contain strings.

/ y

Do not prompt you to confirm that you want to overwrite the existing target file.

/-y

Prompts you to confirm that you want to overwrite the existing target file.

/ z

Replicate over the network in restartable mode.

/?

Displays help at the command prompt.

Annotation

Use / v

XOX

Use / exclude

Lists each string in a separate line of each file. If any string listed matches any part of the absolute path of the file to be copied, the file is excluded from the replication process. For example, if you specify the string "\ Obj\", all files in the Obj directory are excluded. If you specify the string ".obj", all files with an .obj extension are excluded.

Use / z

If a connection is lost during replication (for example, if the server used for the connection is offline), the replication process resumes after the connection is re-established. / z also shows the percentage of copy operations completed by each file.

Use / y in the COPYCMD environment variable

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

Copy encrypted files

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

Ancillary document

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

This section is the content of the second part of BAT batch files and folders (with detailed explanation of xcopy command). Please continue to read it.

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 the 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 the message, causing xcopy to determine that the destination is a directory if the source is multiple files or a directory.

Use the xcopy command to set the archive last name of the Destination file

The xcopy command creates a file with archive last name settings, regardless of whether the last name is set in the source file or not. For more information about the file family name and attrib, see ".

Compare xcopy and diskcopy

If the subdirectory of the disk contains files, to copy them to a disk with a different format, use the xcopy command instead of diskcopy. Because the diskcopy command copies disks track by track, the format of the source and destination disks must be the same. The xcopy command does not have this requirement. Usually use xcopy unless you need a complete copy of the disk image.

Exit code of xcopy

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 process exit codes, see. The following table lists each exit code and description.

Exit code description

There is no error in copying the 0 file.

1 the file to be copied was not found.

2 the user terminated the 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 occurred.

Example

To copy all files and subdirectories (including all empty subdirectories) from drive A to drive B, type:

Xcopy a:b:/s / e

To include any of the systems 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\ Rawdata directory with files that have changed since December 29, 1993, type:

Xcopy\ rawdata\ reports / dvl 12-29-1993

To update all files that exist in\ Reports in the example above, regardless of the date, type:

Xcopy\ rawdata\ reports / u

If you only want to get a list of files to be copied by the previous command, but not the files, type:

Xcopy\ rawdata\ reports / dvl 12-29-1993 / l > xcopy.out

The file Xcopy.out lists all the files to be copied.

To copy the\ Customer directory and all subdirectories to the directory\\ Public\ Address on network drive H:, keep the read-only family name, and prompt you when you create a new file on H:, type:

Xcopy\ customer h:\ public\ address / s / e / k / p

To issue the previous command to ensure that xcopy creates the\ Address directory when it does not exist, and to suppress the message to be displayed when creating a new directory, add 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 xcopy operations and use the if batch command to process the exit code when an error occurs.

Example, using replaceable xcopy source and destination 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 the 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 will replace% 1 and B: with C:\ Prgmcode. For% 2, then use the xcopy with / e and / s command line options.

If xcopy encounters an error, the batch program reads the exit code and goes to the label indicated in the appropriate IF ERRORLEVEL statement, then displays the appropriate message and exits the batch program.

On the "BAT batch file and folder operation example analysis" this article is shared here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, please share it out for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report