bat文件与Vbs文件之间的常用操作(获取用户输入,执行VBS文件)(vbs调用批处理)一看就会

随心笔谈2年前发布 admin
197 0 0

文章摘要

这篇文章介绍了一个使用Windows PowerShell的自动化脚本,主要用于文件操作、WebAPI调用以及时间格式转换。脚本包含以下主要部分: 1. **参数处理**:通过`WScript.Arguments`获取输入参数,并根据参数数量决定是否执行后续操作。 2. **文件操作**:定义了`OutputData`函数,用于将数据写入文件,并使用换行符和制表符分隔内容。 3. **WebAPI操作**:定义了`RequestAPI`函数,用于发送POST请求到WebAPI,并处理返回的响应数据。 4. **时间格式转换**:提供了两个函数,`FormatDate`和`DateToTimeStamp`,用于在时间戳和日期之间进行转换。 总结而言,这篇文章展示了如何利用PowerShell实现文件操作、WebAPI调用以及时间数据的处理。

Dim WshShell
Dim CurDir
Dim oParam

‘取参数
Set oParam=WScript.Arguments

if oParam.Count>0 Then

Else
WScript.Quit
End if

‘获取当前路径
Set WshShell=WScript.CreateObject(“WScript.Shell”)
CurDir=WshShell.CurrentDirectory

‘写文件操作
Function OutputData(filename)
Dim objFSOW
Dim objFileW

Set objFSOW=WScript.CreateObject(“Scripting.FileSystemObject”)
Set objFileW=objFSOW.OpenTextFile(filename,2,True)

objFileW.Write(filename)
objFileW.Write(vbCrLf)
objFileW.Write(vbTab)

Set objFileW=Nothing
Set objFSOW=Nothing

End Function

‘WebAPI操作
‘params=””
Function RequestAPI(url,params)
Dim oHttp

Set oHttp=CreateObject(“MSXML2.ServerXMLHTTP”)
on error resume next

oHttp.Open “POST”,url,False

If Err Then
RequestAPI=Err.Description
End If
On Error Goto 0

oHttp.SetRequestHeader “Content-Type”,”application/json”
oHttp.Send params

If oHttp.readyState<>4 Then
oHttp.waitForResponse(10)
End If

RequestAPI=oHttp.ResponseText

Set oHttp=Nothing

End Function

‘TimeStamp -> Date
Function FormatDate(timestamp)
FormatDate=DateAdd(“s”,CLng(timestamp),”01/01/1970 00:00:00″)
End Function

‘Date ->TimeStamp
Function DateToTimeStamp(dateValue)
DateToTimeStamp=DateDiff(“s”,”01/01/1970 00:00:00″,dateValue)
End Function

© 版权声明

相关文章