A system I created needs to regularly back up database data using batch processing.
Requirement: The DMP file is generated at on the 23rd of each month. If there is a new. dmp file in the directory, change it to old. DMP, and then generate a new. dmp file.
Implementation: 1 backup. BAT file
If exist c: \ new. dmp goto new
Goto Export
: Export
Echo database person exporting...
Exp system/system @ 24w.person full = y file = c: \ new. dmp
Goto end
: New
Echo new
If exist c: \ old. dmp goto Delete
Goto rename
: Delete
Echo delete old. dmp...
Del c: \ old. dmp
Goto rename
: Rename
Echo rename...
Ren c: \ new. dmp old. dmp
Goto Export
: End
echo export data OK.
exit
2. BAT file: scheduled backup on Windows server. bat
Net start schedule
at/every: D: \ datafile \ person \ backup. bat
3 the server is automatically added to the scheduled service in 2 when it is started
Add C: \ at in the Registry HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ Run
. bat completed
---------------------------------- common batch processing commands ----------------------------
echo, @, call, pause, and REM are the most commonly used commands for batch processing files. Start from learning. Echo indicates the character after this command is displayed.
echo off indicates that after this statement, all running commands do not display the command line itself.
@ is similar to echo off, but it is added at the beginning of other command lines, indicating that the command line itself is not displayed during running.
call calls another batch file (if another batch file is called directly, subsequent commands of the current file cannot be executed after that file is executed)
running pause will pause and press any key to continue... wait for the user to press any key.
REM indicates that the character after this command is interpreted as a line. If it is not executed, it will only be used for future search.
For example, use Edit to edit. BAT file, enter the following content and save the disk as c: \. bat, after executing the batch processing file, you can: write all the files in the root directory into a.txt, start UCDOS, enter WPS and other functions.
The content of the batch file is: file representation:
Echo off command line not displayed
Dir c: \ *. *> a.txt: Write the C-drive file into a.txt.
Call c: \ UCDOS. Bat call UCDOS
Echo Hello show "hello"
Pause pause and wait for the button to continue
Rem uses WPS annotation to use WPS
Cd ucdos enters the UCDOS directory
WPS use WPS
You can also use parameters in a batch file like the C language, which requires only one parameter identifier %.
% Indicates a parameter. A parameter is a string added after the file name when a batch file is run. Variables can be represented from % 0 to % 9, % 0 indicates the file name itself, and strings are represented in the order of % 1 to % 9.
For example, C: the name of the processing file in the root directory is F. bat, and the content is format % 1.
If c: \> f a: is executed, format:
Another example is C: The name T. bat of the batch of files processed in the root directory, and the content is type % 1 type % 2.
Run c: \> T a.txt B .txt to display the contents of a.txt and B .txt in sequence.
Special commands
If goto choice for is a relatively advanced command in the batch processing file. If you are familiar with these commands, you are an expert in batch processing files. If indicates whether the specified conditions are met, and then different commands are executed. There are three formats:
1. If "parameter" = "string" command to be executed
If the parameter is equal to the specified string, the condition is true. Run the command. Otherwise, run the next sentence. (Note that there are two equal signs)
For example, if "% 1" = "A" format:
2. If exist file name command to be executed
If a specified file exists, the condition is true. Run the command. Otherwise, run the next sentence. For example, if exist config. sys edit config. sys
3. If errorlevel: command to be executed
If the return code is equal to the specified number, the condition is true. Run the command. Otherwise, run the next sentence. For example, if errorlevel 2 goto X2 DoSProgramAt runtime, a number is returned to DOS, which is called the error code errorlevel or return code.
When the Goto batch file runs here, it will jump to the label specified by Goto, which is generally used with if. For example:
Goto end
: End
Echo this is the end
The label is represented by a string. The row where the label is located is not executed.
Choice uses this command to allow users to enter a single character to run different commands. The/C: parameter should be added for use, and C: should be followed by a prompt to enter characters without spaces. Its return code is 1234 ......
For example: choice/C: dimethyl defrag, mem, end
Will display
Defrag, mem, end [d, M, E]?
For example, the content of test. bat is as follows:
@ Echo off
Choice/C: dimethyl defrag, mem, end
If errorlevel 3 goto defrag, first judge the highest error code.
If errorlevel 2 goto mem
If errotlevel 1 goto end
: Defrag
C: \ dos \ defrag
Goto end
: Mem
Mem
Goto end
: End
Echo good bye
After this file is run, defrag, mem, end [d, M, E]? You can select d m e, and then the if statement will make a judgment. D indicates the program segment with the execution label as defrag, and M indicates the program segment with the execution label as mem, E indicates the program segment whose execution label is end. Each program segment finally jumps the program to the end label using goto end. Then, the program displays Good bye, and the file ends.
For Loop Command, as long as the conditions match, it will execute the same command multiple times.
Format: For [% F] In (SET) do [command]
If the parameter F is in the specified set, the condition is true and the command is executed.
If one batch file contains one row:
For % C in (*. bat *. txt) do type % C
It indicates that if a file ends with bat or TXT, the file content is displayed.
At commands of NT Server
The Service Program Manager of Windows NT server can only set the startup mode (automatic, manual, and invalid) of a service. In daily management, A service is often required to be enabled or disabled on a regular basis. The at Command provided by Windows NT server can achieve this. You can use it to implement the scheduled switch of the RAS Service. The specific steps are as follows:
1. Start the Schedule service in "Settings", "Control Panel", and "services", and set its "Startup Mode" attribute to "automatic. Or use the net start schedule command to start
2. Use the AT command in the Command window to set the timed startup and shutdown of the service program.
The at command format is as follows:
At [\ computername] time [/interactive] [/every: Date [,] | next: Date [,] "command ″
Parameter description:
No parameter: displays all scheduled commands that have been set.
\ Computername: indicates the name of the computer on which the command is executed. If it is omitted, it is executed on the local machine.
Time: command execution time
/Interactive: whether to interact with the logged-on user when the program is executed.
/Every: Date [,]: Specifies the date on which the program is executed (for example, Tuesday, Wednesday,/every: T, W), and no. 1, 2,/every: 1, 2 every month ).
Next: Date [,]: Specifies the date of the next execution of the program.
"Command": scheduled Windows NT commands, programs, and batch processing.
At [\ computername] [[ID] [/delete] [/Yes]
Parameter description:
ID: The identification number assigned to the plan command. It can be found by the AT command without parameters.
/Delete: cancels the specified scheduler command. If the ID is omitted, all scheduler commands are canceled.
/Yes: Yes is mandatory for all cancel requests.
For example, if you start the service at every day, the command to disable the RAS Service at the next day is as follows:
At 19:00/every: M, T, W, Th, F, S, Su Net start "remote access server ″
At 7: 30/every: M, T, W, Th, F, S, Su net stop "remote access server ″