To call a bat file in a program

Source: Internet
Author: User
Tags clear screen

1, C + + call DOS command

Writing a C + + program in a Microsoft Visual C + + environment can include a header file <stdlib.h> then call the system () function in the main function body to invoke the DOS command.

For example: If you want the program to stop using System ("PAUSE") in a certain place, you can pause it;

System ("CLS"), can clear screen;

System ("DIR C:"), you can query C disk;

System ("Start Regedit.exe"); Open the Registry;

System ("NET user"), view local user groups, and so on ...

2. How to invoke the execution bat file or exe file in the program

In C #

1. Execute Batch document System.Diagnostics.Process.Start (path);

Path for file paths

2, modify the batch process file

FileStream afile = new FileStream (FilePath, FileMode.OpenOrCreate);

temp = ""; Content in a batch file

Chardata = temp. ToCharArray ();

Encoder e = Encoding.UTF8.GetEncoder ();

E.getbytes (chardata, 0, Chardata.length, bydata, 0, true); Converts a character array to a byte-type array

Afile.write (bydata, 0, bydata.length);

Afile.close ();

In C

System ("*.bat");

System ("*.exe");

#include <process.h>

It does not have to be in the same directory, as long as you want to execute the. bat file

1. In the various directories of the system path variable (known as path execution at the command prompt)

2. In the directory where the program is located

3. The first two can be directly by the way upstairs, otherwise, you must specify the absolute path (e.g.: D:\prog\test.exe) or relative path (e.g.: \sub\subsub\test2.exe).

Note that ' \ ' needs to be represented by the escape character ' \ \ ' (Or try "/").

--------------------------------------------------------------------------------------------------------------- ---------------------------------------------------------------

Take C For example, the code is as follows:

#include "stdio.h"
#include "stdlib.h"
#include <process.h>
void Main ()
{	
	system ("A.bat") );
	System ("PAUSE");
}

A.bat's Code

Type *.txt > A

In the file A, modify the suffix named A.txt


Or use B.bat code directly (B.bat to change the suffix name of file a)

@echo off
Ren a a.txt

Combined, you can write the following. bat code:

Type *.txt > A
@echo off
ren a a.txt




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.