标题: [其他] 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混
编。不多说,上范例。- if ($true){}# == ($true){}# goto ___yiwuyun
- <#BeginBatOperation#
- :___yiwuyun
- @echo off&setlocal&cls
- (echo $yiwuyun_fileName="%~f0"&echo $strPath="%~dp0"&type "%~f0")|powershell -command -
- pause
- exit/b 0
- #EndBatOperation#>
-
- <#StartPowerShell#>
- "正在运行JScript";
- $js_content=gc $yiwuyun_fileName;
- for($i=0;$i -lt $js_content.count;$i++){
- if($js_content[$i] -match "^<#StartJS#"){$StartJS_Line=$i+1;}
- if($js_content[$i] -match "^#EndJS#>"){$EndJS_Line=$i-1;break;}
- }
- for($i=$StartJS_Line;$i -le $EndJS_Line;$i++){
- $js+=$js_content[$i]+"`r`n";
- }
- out-file -FilePath $yiwuyun_fileName -Encoding "OEM" -InputObject $js;
- cscript //nologo //e:jscript $yiwuyun_fileName;
- out-file -FilePath $yiwuyun_fileName -Encoding "OEM" -InputObject $js_content;
-
-
- "正在运行VBScript";
- $vbs_content=gc $yiwuyun_fileName;
- for($i=0;$i -lt $vbs_content.count;$i++){
- if($vbs_content[$i] -match "^<#StartVBS#"){$StartVBS_Line=$i+1;}
- if($vbs_content[$i] -match "^#EndVBS#>"){$EndVBS_Line=$i-1;break;}
- }
- for($i=$StartVBS_Line;$i -le $EndVBS_Line;$i++){
- $vbs+=$vbs_content[$i]+"`r`n";
- }
- out-file -FilePath $yiwuyun_fileName -Encoding "OEM" -InputObject $vbs;
- cscript //nologo //e:vbscript $yiwuyun_fileName;
- out-file -FilePath $yiwuyun_fileName -Encoding "OEM" -InputObject $vbs_content;
-
- <#EndPowerShell#>
-
- <#StartJS#
- var str="abcdefgBC12bC34";
- var patt=new RegExp(/(B)(c)/ig);/* js中的索引从0开始,与C语言一致 var patt=new ReExp("BC","g");*/
- var result;
- var strMatch="";
- while((result=patt.exec(str))!=null){
- for(var i=0;i<result.length;i++){
- if(i==0){
- strMatch=strMatch+"match: "+result[i]+" "+"index:"+result.index;
- }else{
- strMatch=strMatch+" "+i+":"+result[i];
- }
- }
- strMatch=strMatch+" all:"+result.input+" patt.lastIndex:"+patt.lastIndex;
- WScript.Echo(strMatch);
- strMatch="";
- }
-
- #EndJS#>
-
-
- <#StartVBS#
- Dim objExcel,strArray(6)
- strPath=Left(WScript.ScriptFullName,InStrRev(WScript.ScriptFullName,"\"))
- Set objExcel=CreateObject("Excel.Application")
- 'objExcel.WorkBooks.Add.SaveAs(strPath&"test.xlsx")
- DeleteFileByFilename(strPath&"test.xlsx")
- objExcel.WorkBooks.Add().SaveAs(strPath&"test.xlsx")
- objExcel.DisplayAlerts=false
- objExcel.visible=true
- objExcel.SheetsInNewWorkBook=2
- strArray(0)="姓名"
- strArray(1)="数学"
- strArray(2)="外语"
- strArray(3)="历史"
- strArray(4)="化学"
- strArray(5)="生物"
- For col=1 to 6
- objExcel.WorkBooks("test.xlsx").ActiveSheet.Cells(1,col).value=strArray(col-1)
- 'objExcel.Cells(1,col).value=strArray(col-1)
- Next
-
- For row=2 to 10
- objExcel.Cells(row,1).value=String(1,65)+CStr(row)
- Next
-
- objExcel.Range("B2:F10").Formula="=40+Int(Rand()*61)"
- objExcel.Range("A1:F10").Style.HorizontalAlignment=-4108
-
- Set objShape=objExcel.ActiveSheet.Shapes.AddChart(51)
- objShape.Chart.SetSourceData objExcel.Range("A1:F10"),2
- Set objShape=Nothing
-
-
- 'objExcel.ActiveSheet.SaveAs(strPath&"test.xlsx")
- objExcel.ActiveWorkBook.Save
- objExcel.Quit()
-
-
- Sub DeleteFileByFilename(FullFilename)
- Dim objFSO
- Set objFSO=WScript.CreateObject("Scripting.FileSystemObject")
- If objFSO.FileExists(FullFilename) Then
- 'call objFSO.DeleteFile(FullFilename,True)
- objFSO.DeleteFile FullFilename,True
- End If
- Set objFSO=Nothing
- End Sub
- #EndVBS#>
复制代码
欢迎光临 批处理之家 (http://bbs.bathome.net/) |
Powered by Discuz! 7.2 |