自动导入脚本不起作用

时间:2016-11-27 23:25:10

标签: vbscript

我是VB的绝对初学者因此我可能会问一些愚蠢的问题 我有一个VB脚本通过批处理文件触发,导致最后一天导入数据。 下面是VB和批处理文件的代码 如果您发现代码中有任何错误,请通知我。

VB脚本

rem 
rem XLink_Import.vbs
rem 

Set oShell = WScript.CreateObject("WScript.Shell") 

' filename = oShell.ExpandEnvironmentStrings("today_xlink.bat") 
' Set objFileSystem = CreateObject("Scripting.fileSystemObject") 
' Set oFile = objFileSystem.CreateTextFile(filename, TRUE) 

Dim i
Dim ImportStartOffset, ImportedNumberOfDays

If WScript.Arguments.length > 0 Then
    For i=0 to WScript.Arguments.length-1
        Arg = WScript.Arguments(i)
        If Left(Arg,1) = "-" Then
            If ( Arg = "-o" ) Then
                ImportStartOffset = WScript.Arguments(i+1) 
            End if
            If ( Arg = "-n" or Arg = "-l" ) Then
                ImportedNumberOfDays = WScript.Arguments(i+1) 
            End if
        End if
    Next
End If

rem Prepare the import start date

Dim Dy, Mth
Dim ImportDate
ImportDate = Now + ImportStartOffset
Dy = Day(ImportDate) 
Mth = Month(ImportDate) 
If Len(Dy) = 1 Then Dy = "0" & Dy 
If Len(Mth) = 1 Then Mth = "0" & Mth 
ImportStartDate = Dy & "/" & Mth & "/" & Year(ImportDate) 

rem Prepare import script to run (not useed yet)
rem oFile.WriteLine("isps_ul.exe -t -d " & todaydate & " -L 1") 
rem oFile.Close

rem Run XLink import

wscript.echo "isps_ul.exe -t -d " & ImportStartDate & " -L " & ImportedNumberOfDays
oShell.Run "isps_ul.exe -t -d " & ImportStartDate & " -L " & ImportedNumberOfDays, 1, true

批处理文件

@echo off
rem
rem XLink_Import.bat
rem
rem Manually starts an Xlink import starting today + a StartOffset of some days. 
rem Imported number of days can also be set.
rem

set ImportStartOffset=0
set ImportedNumberOfDays=1

cscript XLink_Import.vbs -o %ImportStartOffset% -n %ImportedNumberOfDays%

pause

1 个答案:

答案 0 :(得分:0)

你不需要一个批处理和一个脚本,其中一个就足够了,批量完成整个过程需要一些带有特殊参数的东西,我不会这样做,所以我&# 39; ll稍微调整你的脚本。 由于您将2个配置变量保存在Windows环境中,您也可以从vbscript中读取它们,其他选项是从命令行读取配置文件,或者保留脚本本身。

如果您在配置中正确设置了这些日期(环境变量),那么可以省略您的中间部分,确保日期是正确的。

如果您的导入工作正常,您应该先运行显示为命令的内容进行检查,以便运行"eisps_ul.exe -t -d28/11/2016 -L",否则请先搜索该问题。

我在关于DRY的评论意味着你不应该重复的事情,如果你的命令你可以将连接的命令存储在一个变量中,并使用它来查看和运行。

Dim ImportStartOffset, ImportedNumberOfDays, oShell, command, Dy, Mth, ImportDate, ImportStartDate
Constant WaitOnReturn = true, WindowStyle = 1 '1 = Activate and display

'read configuration environment variables
Set oShell           = CreateObject( "WScript.Shell" )
ImportStartOffset    = wshShell.ExpandEnvironmentStrings( "%ImportStartOffset%" )
ImportedNumberOfDays = wshShell.ExpandEnvironmentStrings( "%ImportedNumberOfDays%" )

'Prepare the import start date (not necessary if environmentvariables would be configured well)
ImportDate = Now + ImportStartOffset
Dy = Day(ImportDate) 
Mth = Month(ImportDate) 
If Len(Dy) = 1 Then Dy = "0" & Dy 
If Len(Mth) = 1 Then Mth = "0" & Mth 
ImportStartDate = Dy & "/" & Mth & "/" & Year(ImportDate) 

'Run XLink import
command = "isps_ul.exe -t -d " & ImportStartDate & " -L " & ImportedNumberOfDays
wscript.echo command
oShell.Run command, WindowStyle, WaitOnReturn
Set oShell = Nothing