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

How to use batch parameters

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

Share

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

This article is about how to use batch parameters. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

You can use batch parameters anywhere within a batch file to extract information about environment settings.

Cmd.exe provides batch parameter extension variables (0 to 9). When using batch parameters in a batch file, 0 will be replaced by the batch file name, and 1 to 9 will be replaced by the corresponding parameters typed on the command line. To access parameters greater than 9, you must use the shift command. For more information about the Shift command, see Shift. % * batch parameters are wildcards that can be referenced by all parameters (excluding% 0), which are passed to the batch file.

For example, to copy the contents of Folder1 to Folder2, where% 1 and% 2 will be replaced by the values Folder1 and Folder2, respectively, type the following in the batch file Mybatch.bat:

Xcopy% 1\ *. * 2

To run the file, type:

Mybatch.bat C:\ folder1 D:\ folder2

This is equivalent to typing the following in a batch file:

Xcopy C:\ folder1\ *. * D:\ folder2

You can also use edit characters in batch parameters. The editor extends the batch parameter to a partial or complete file or directory name using the current drive and directory information. To use the edit character, type the percent sign (%) character, followed by the waveform symbol (~) character, and then type the appropriate edit character (that is,% ~ modifier).

The following table lists the edit characters that can be used in the extension.

Edit character description

% ~ 1 extends% 1 and removes any quotation marks (").

% ~ F1 extends% 1 to a fully qualified pathname.

% ~ D1 extends% 1 to the drive letter.

% ~ p1 extends% 1 to the path.

% ~ N1 extends% 1 to the file name.

% x1 extends% 1 to the file extension.

The path to the% S1 extension contains only short names.

% ~ A1 extends% 1 to file properties.

% ~ T1 extends% 1 to the file date / time.

% ~ Z1 extends% 1 to the file size.

% ~ $PATH:1 searches for directories listed in the PATH environment variable and extends% 1 to the fully qualified name of the first directory found. If the environment variable name is not defined, or if the file is not found, this editor extends to an empty string.

The following table lists the possible combinations of editors and qualifiers that can be used to obtain complex results:

Edit character description

% ~ dp1 extends% 1 to the drive letter and path.

% ~ nx1 extends% 1 to the file name and extension.

% ~ dp$PATH:1 searches for% 1 in the directory listed in the PATH environment variable and extends to the drive letter and path of the first directory found.

% ~ ftza1 extends% 1 to output lines like dir.

Be careful

In the above example, you can replace% 1 and PATH with other batch parameters.

The% * editor is the only editor that represents all parameters passed in the batch file. You cannot combine this edit character with the% ~ editor character. % ~ the syntax must be terminated by a valid parameter value.

Batch parameters cannot be used in the same way as environment variables. You cannot search for or replace values, or check substrings. However, you can assign a parameter to an environment variable and then use that environment variable.

Thank you for reading! This is the end of this article on "how to use batch parameters". 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, you can 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