Start MongoDB script with bat batch processing

Source: Internet
Author: User
Tags echo command goto



At the beginning of the article, let's review how to open the MongoDB database with the cmd Command window, with the following command:



Open the MongoDB database



 
cd D:\Program Files\MongoDB\bin
mongod --depath "D:\Program Files\MongoDB\data"





See the above window information, we can see the MongoDB database started successfully or in the browser run http://localhost:27017/see such English it looks like you is trying to access MongoDB over HTTP on the native driver port proves that the MongoDB database started successfully.



can refer to my previous article: http://www.cnblogs.com/pingfan1990/p/4531573.html



Let's explain how to write a. bat batch file to start the MongoDB database:


One, what thing? Bat batch File


The bat file is a batch file under DOS. A batch file is a unformatted text file that contains one or more commands. It has a. bat or. cmd file name extension. At the command prompt, type the name of the batch file, or double-click the batch file, and the system calls Cmd.exe to run them one by one in the order in which the commands appear in the file. Use a batch file (also known as a batch program or script) to simplify routine or repetitive tasks.


Second, bat common syntax commands


Echo command
Turn on echo or turn off the request Echo feature, or display a message. If there are no parameters, the echo command displays the current echo setting.
Grammar



echo [{on|off}] [message]
Sample:@echo off / echo hello world


In practical applications we will combine this command with a redirect symbol (also known as a pipe symbol, usually > >> ^) to implement input commands into a file of a particular format.



  @ command



Indicates that the command after the @ is not displayed, and in the intrusion process (for example, using batches to format the enemy's hard disk), it is not natural to let the other party see the command you are using.



Sample: @echo Off@echo now initializing the program,please wait a minite ...


@format X:/q/u/autoset (Format This command is not allowed to use/y This parameter, it is gratifying that Microsoft left a autoset This parameter to us, the effect and/y is the same. )



  Goto command



Specifies to jump to the label, and when the label is found, the program processes the command starting from the next line.



Syntax: Goto label (label is a parameter that specifies the line in the batch program to which you want to turn.) )



 
 
Sample:
If {%1}=={} goto noparms
If {%2}=={} goto noparms (if if, %1, %2 here do not understand, jump first, there will be a detailed explanation.)
@Rem check parameters if null show usage
:noparms
Echo Usage: monitor.bat ServerIP PortNumber
Goto end


The name of the tag can be random, but it is better to have a meaningful letter, the letter is added: to indicate that the letter is a label, the Goto command is based on this: to find the next jump to go there. It is better to have some explanation so that others will understand your intentions.



  REM command (can be used:: instead of REM)



Note Command, in C language equivalent to/*--------* *, it will not be executed, just a comment on the role, easy for others to read and your own later modification.



  Pause command



When you run the Pause command, the following message is displayed:


Press any key to continue ...

 
Sample:
@echo off
:begin
copy a:*.* d:\back
echo Please put a new disk into driver A
pause goto begin


In this example, all the files on the disk in drive A are copied to D:\back. The comment that appears prompts you to put another disk in drive A, and the pause command suspends the program so that you can replace the disk and press any key to continue processing.



  Call command



  Another batch program is called from one batch program, and the parent batch program is not terminated. The call command accepts the label that is used as the calling target. If you use call outside of a script or batch file, it will not work at the command line.



grammar
Call [[Drive:][Path] FileName [BatchParameters]] [:label [arguments]]
parameter
[Drive:][Path] FileName 


Specifies the location and name of the batch program to invoke. The filename parameter must have a. bat or. cmd extension.



  Start command


Calling an external program, all DOS commands and command-line programs can be called by the start command. Intrusion Common parameters: Min Start window minimized separate in a separate space start 16-bit Windows program high in the high priority category start Application REALTIME in the REALTIME priority category start Application WAIT Start the application and wait for it to end parameters These are the 32-bit GUI applications that are CMD when the application executed for the parameters that are routed to the command/program. EXE returns a command prompt without waiting for the application to terminate. If executed within a command script, the new behavior does not occur.


  Choice command  


Choice Use this command to let the user enter a character to run different commands. Use should be Plus/C: parameters, C: After the prompt should be written to enter the characters, no spaces between. It has a return code of 1234 ...

Such as: choice /c:dme defrag,mem,end
Will show
Defrag, mem, end[D,M,E]?
Sample:
The contents of Sample.bat are as follows:
@echo off
Choice /c:dme defrag,mem,end
If errorlevel 3 goto defrag (should judge the highest error code first)
If errorlevel 2 goto mem
If errotlevel 1 goto end
:defrag
c:\dos\defrag
Goto end
:mem
Mem
Goto end
:end
Echo good bye


When this file runs, Defrag,mem,end[d,m,e] is displayed? The user can select D m e, then the IF statement will be judged, D represents the execution of a program segment labeled Defrag, M represents the execution of a program segment labeled Mem, E represents the execution of a program segment labeled End, and each segment ends with a goto end to jump the program to the end label. Then the program will show good Bye, the end of the file.



  If command


The IF indicates that the specified conditions will be judged, thus deciding to execute different commands. There are three formats: ⒈if "parameter" = = "string" the command parameter to execute if equal to the specified string, the condition is set, run the command, otherwise run the next sentence. (note is two equals) such as if "%1" = = "a" format a:if {%1}=={} goto noparms if {%2}=={} goto noparms⒉if exist filename The command to execute if there is a specified file, the condition  Set up, run the command, or run the next sentence.  If Existconfig.sysedit config.sys⒊if errorlevel/if not errorlevel number of commands to execute if the return code equals the specified number, then the condition is established, run the command, otherwise run the next sentence. such as if errorlevel 2 goto x2 DOS program run will return a number to DOS, called the error code ERRORLEVEL or return code, the common return code is 0, 1. For CommandThe for command is a more complex command that is used primarily for parameters that loop through commands within a specified range. When using the for command in a batch file, specify the variable using the%%variable

 for inch (set do command [CommandLineOptions]
%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. When using the for command in a batch file, specify the variable using%%variable instead of%variable. Variable names are case-sensitive, so%i differs from%i. bat Batch processing example


  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.


D. bat batch Implementation start MongoDB script


MongoDB Install bin path D:\Program files\mongodb\bin,mongodb in Data folder D:\Program Files\mongodb\data



We should be like this in the cmd command window, start the MongoDB database



CD D:\Program Files\mongodb\binmongod  " D:\Program Files\mongodb\data "


But every time we have to knock on these, it really makes us very troublesome, so we can use bat's just to do such a bat file:


:: indicates that all commands that are run after this statement do not display the command line itself.
@echo off
Hello, hello
::Enter d disk
D:
Cd \Program Files\MongoDB\bin
Mongod --dbpath "D:\Program Files\MongoDB\data"

//The following is the comment section
::mongod -- dbpath "D:\Program Files\MongoDB\data"

:: d:> echo "this d"
:: d:> echo "this d"
:: d:> echo "this d"
:: d:> echo "this d"
::mongod --dbpath "D:\Program Files\MongoDB\data"
::Write b.txt file

::dir D:\Program Files\MongoDB\bin> b.txt

::pause 


Through this file, every time we have to open the MongoDB service, click on the bat file, you can.






Resources:



BAT file http://baike.baidu.com/view/1024624.htm



BAT file Syntax and tricks (BAT file authoring and use)



BAT file syntax and tips for writing and using bat files



Start MongoDB script with bat batch processing


Related Article

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.