Board logo

标题: [其他] bat,vbs,js,powershell混合编程 [打印本页]

作者: yiwuyun    时间: 2015-4-1 10:54     标题: bat,vbs,js,powershell混合编程

一直想写个VBS,JS,BAT混合编程的范例,但始终找不到重写源文件名的方法,虽然sed能实现,但总是第三方软件,所以未能动笔,最近学了下PS,顺便就把这几个混合起来了。
理论上,任何脚本语言,只要有exe的引擎,就可以通过powershell和bat混合,从而实现所有脚本的混合编程。因此,SED,GAWK所写的脚本都能和VBS,JS,POWERSHELL混
编。不多说,上范例。
  1. if ($true){}# == ($true){}# goto ___yiwuyun
  2. <#BeginBatOperation#
  3. :___yiwuyun
  4. @echo off&setlocal&cls
  5. (echo $yiwuyun_fileName="%~f0"&echo $strPath="%~dp0"&type "%~f0")|powershell -command -
  6. pause
  7. exit/b 0
  8. #EndBatOperation#>
  9. <#StartPowerShell#>
  10. "正在运行JScript";
  11. $js_content=gc $yiwuyun_fileName;
  12. for($i=0;$i -lt $js_content.count;$i++){
  13.   if($js_content[$i] -match "^<#StartJS#"){$StartJS_Line=$i+1;}
  14.   if($js_content[$i] -match "^#EndJS#>"){$EndJS_Line=$i-1;break;}
  15. }
  16. for($i=$StartJS_Line;$i -le $EndJS_Line;$i++){
  17.   $js+=$js_content[$i]+"`r`n";
  18. }
  19. out-file -FilePath $yiwuyun_fileName -Encoding "OEM" -InputObject $js;
  20. cscript //nologo //e:jscript $yiwuyun_fileName;
  21. out-file -FilePath $yiwuyun_fileName -Encoding "OEM" -InputObject $js_content;
  22. "正在运行VBScript";
  23. $vbs_content=gc $yiwuyun_fileName;
  24. for($i=0;$i -lt $vbs_content.count;$i++){
  25.   if($vbs_content[$i] -match "^<#StartVBS#"){$StartVBS_Line=$i+1;}
  26.   if($vbs_content[$i] -match "^#EndVBS#>"){$EndVBS_Line=$i-1;break;}
  27. }
  28. for($i=$StartVBS_Line;$i -le $EndVBS_Line;$i++){
  29.   $vbs+=$vbs_content[$i]+"`r`n";
  30. }
  31. out-file -FilePath $yiwuyun_fileName -Encoding "OEM" -InputObject $vbs;
  32. cscript //nologo //e:vbscript $yiwuyun_fileName;
  33. out-file -FilePath $yiwuyun_fileName -Encoding "OEM" -InputObject $vbs_content;
  34. <#EndPowerShell#>
  35. <#StartJS#
  36. var str="abcdefgBC12bC34";
  37. var patt=new RegExp(/(B)(c)/ig);/* js中的索引从0开始,与C语言一致  var patt=new ReExp("BC","g");*/
  38. var result;
  39. var strMatch="";
  40. while((result=patt.exec(str))!=null){
  41.   for(var i=0;i<result.length;i++){
  42.     if(i==0){
  43.       strMatch=strMatch+"match: "+result[i]+"  "+"index:"+result.index;
  44.     }else{
  45.       strMatch=strMatch+"  "+i+":"+result[i];
  46.     }
  47.   }
  48.   strMatch=strMatch+"   all:"+result.input+"   patt.lastIndex:"+patt.lastIndex;
  49.   WScript.Echo(strMatch);
  50.   strMatch="";
  51. }
  52. #EndJS#>
  53. <#StartVBS#
  54. Dim objExcel,strArray(6)
  55. strPath=Left(WScript.ScriptFullName,InStrRev(WScript.ScriptFullName,"\"))
  56. Set objExcel=CreateObject("Excel.Application")
  57. 'objExcel.WorkBooks.Add.SaveAs(strPath&"test.xlsx")
  58. DeleteFileByFilename(strPath&"test.xlsx")
  59. objExcel.WorkBooks.Add().SaveAs(strPath&"test.xlsx")
  60. objExcel.DisplayAlerts=false
  61. objExcel.visible=true
  62. objExcel.SheetsInNewWorkBook=2
  63. strArray(0)="姓名"
  64. strArray(1)="数学"
  65. strArray(2)="外语"
  66. strArray(3)="历史"
  67. strArray(4)="化学"
  68. strArray(5)="生物"
  69. For col=1 to 6
  70.    objExcel.WorkBooks("test.xlsx").ActiveSheet.Cells(1,col).value=strArray(col-1)
  71.    'objExcel.Cells(1,col).value=strArray(col-1)
  72. Next
  73. For row=2 to 10
  74.   objExcel.Cells(row,1).value=String(1,65)+CStr(row)
  75. Next
  76. objExcel.Range("B2:F10").Formula="=40+Int(Rand()*61)"
  77. objExcel.Range("A1:F10").Style.HorizontalAlignment=-4108
  78. Set objShape=objExcel.ActiveSheet.Shapes.AddChart(51)
  79. objShape.Chart.SetSourceData objExcel.Range("A1:F10"),2
  80. Set objShape=Nothing
  81. 'objExcel.ActiveSheet.SaveAs(strPath&"test.xlsx")
  82. objExcel.ActiveWorkBook.Save
  83. objExcel.Quit()
  84. Sub DeleteFileByFilename(FullFilename)
  85. Dim objFSO
  86. Set objFSO=WScript.CreateObject("Scripting.FileSystemObject")
  87. If objFSO.FileExists(FullFilename) Then
  88.   'call objFSO.DeleteFile(FullFilename,True)
  89.   objFSO.DeleteFile FullFilename,True
  90. End If
  91. Set objFSO=Nothing
  92. End Sub
  93. #EndVBS#>
复制代码





欢迎光临 批处理之家 (http://bbs.bathome.net/) Powered by Discuz! 7.2