Batch Processing and vbs scripts required by Java programmers

Source: Internet
Author: User

I feel that I haven't written a blog for a while, and I haven't opened my blog for several days. This is actually a step backwards,
I haven't learned much recently. I have been reading the first blog article for a whole year. I want to stick to it because I haven't learned it recently, so there is really nothing to write, I wrote a few small scripts yesterday and shared them out. As I became lazy, I became more and more fond of simple scripting languages.
Reprinted please indicate the source http://blog.csdn.net/sunyujia/
1. Use vbs to set Environment Variables
Sometimes it is quite troublesome to set environment variables. I hope it can be made right after a click, so that reinstalling the system will not be so troublesome, but considering the versatility, in order not to write the path to death, a Select Directory dialog box is made. The source code is as follows: Save it as javahometool using notepad. vbs.
The following script only sets java_home. In the same principle, with the java_home path, classpath can be set. However, I will not write it here. Leave it to the reader.

  1. Msgbox "welcome to the java_home setting tool create by sunyujia"
  2. 'Reprinted please indicate the author and the source http://blog.csdn.net/sunyujia/
  3. Set shell = Createobject ("Shell. Application ")
  4. Set selfolder = shell. browseforfolder (0, "select java_home directory sunyujia@yahoo.cn:", 0, ssfdrives)
  5. Set selfolderitem = selfolder. Self
  6. Selpath = selfolderitem. Path
  7. Dim wsh
  8. Set wsh = wscript. Createobject ("wscript. Shell ")
  9. Wsh. Environment ("system"). Item ("java_home") = selpath
  10. Msgbox "complete Settings" + selpath

To facilitate frequent system reinstallation

  1. @ Echo off
  2. Reg Add "HKEY_LOCAL_MACHINE/system/CurrentControlSet/control/Session Manager/environment"/V path/T reg_expand_sz/D "D:/jdk5; % PATH %"/F
  3. @ Echo on
  4. Pause

The user environment variable is in HKEY_CURRENT_USER/Environment

The preceding figure shows how to set system environment variables.
The user environment variable is in HKEY_CURRENT_USER/Environment
To put it bluntly, modify the registry. No technical skills
Refresh the registry and group policy. Enter gpupdate/force in the run box or write it to the batch processing box. Click it when necessary, which is convenient. If it does not take effect, you can log out or restart it. 2000, SP, 2003, and different versions are too different in the environment variable refresh, but the restart is omnipotent.
2. Merge SQL scripts in the current directory using batch processing
Merge the SQL script files in the current directory, because it is troublesome to execute a large number of SQL script files one by one.
Save As A. BAT file. The. SQL file in the current directory is merged into a file.

  1. @ Echo off
  2. Set _ taget_path = *. SQL
  3. Set _ filename =./All. SQL
  4. Set subdir =/S & REM Delete this row without traversing sub-Directories
  5. If exist "% _ filename %" (DEL "% _ filename % ")
  6. Setlocal enabledelayedexpansion
  7. For/F "delims =" % I in ('dir "! _ Taget_path! "/A-d/B! Subdir! ') Do (
  8. @ ECHO is merging with file % I
  9. Type "% I"> "! _ Filename! "
  10. > "! _ Filename! "Echo.
  11. )
  12. Endlocal
  13. Echo % _ filename % merged.
  14. Pause> NUL

3. Two Methods for configuring classpath for Batch Processing
Needless to say, writing a Java Applet depends on some jar packages. Sometimes there may be many jar packages, which is too troublesome to write them one by one into the BAT file. It is better to put them under a directory, load them together.
The following are two implementation methods. I recommend the second one, because some problems have been found during the first one,
Because setlocal enabledelayedexpansion is used in the first implementation method, and endlocal is not allowed before Java is executed, this will cause problems if it runs in a complex batch processing environment, therefore, the second method is written using the main program and subroutine. In the past, I used two bat methods to implement the second method. Yesterday I suddenly found that there was a Goto: EOF such a good thing in batch processing. It was really good because Goto: EOF and setlocal enabledelayedexpansion need some dos basics, so I will not introduce them here. If you are interested, you can run these two commands on Baidu or view DoS documents, the simplest way is to enter HH ntcmds.chmin to open cmd.exe's command line reference manual. I will not talk about it anymore. Put the code below:

First, variable latency

  1. @ Echo off
  2. @ REM: You can specify a directory in _ taget_path, which is equivalent to an absolute directory.
  3. Set _ taget_path =./*. Jar
  4. Set subdir =/S & REM Delete this row without traversing sub-Directories
  5. Setlocal enabledelayedexpansion
  6. For/F "delims =" % I in ('dir "! _ Taget_path! "/A-d/B! Subdir! ') Do (
  7. Echo % I
  8. Set classpath = % I ;! Classpath!
  9. )
  10. Echo % classpath %
  11. @ Endlocal
  12. Echo % classpath %
  13. Pause> NUL

Second, use subroutines

  1. @ Echo off
  2. @ REM: You can specify a directory in _ taget_path, which is equivalent to an absolute directory.
  3. Set _ taget_path =./*. Jar
  4. Set subdir =/S & REM Delete this row without traversing sub-Directories
  5. For/F "delims =" % I in ('dir "% _ taget_path %"/a-d/B % subdir % ') Do (
  6. Echo % I
  7. Call: appendcp % I
  8. )
  9. Goto: endcp
  10. : Appendcp
  11. Set classpath = % 1; % classpath %
  12. Goto: EOF
  13. : Endcp
  14. Echo % classpath %
  15. Pause> NUL

The last echo % classpath % shows the configured classpath. The setting here is only for the current cmd. EXE takes effect, not modifying environment variables. It is different from modifying the registry at the top.

 

I hope this article can help myself continue to write blogs.

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.