如何在批处理文件中设置等于VB6变量的参数

时间:2012-12-20 12:48:07

标签: batch-file vb6 vbscript imagemagick dos

在我的批处理文件中,我正在调用VBScript并传递3个参数。参数是“IPADDRESS”“PicName”和“Storage”,如下所示:

批处理文件:

        set Pathname="C:\User\username\locationOfFile 
        cd /d %Pathname%
        cscript.exe C:\User\username\locationOfFile\myScript.vbs IPADDRESS PicName Storage

在我的VB6程序中,定义了参数值。

让我们说例如IPADDRESS =“170.190.xxx.xxx”

有没有办法可以读取批处理文件,并根据VB6表单中的声明使用IPADDRESS的值?变量IPADDRESS,PicName和Storage将根据VB6程序所讨论的外部应用程序不断变化,因此我无法在批处理中静态设置它(.... \ myScript.vbs 170.190.xxx.xxx pic1 C:\存储)

我的VBScript如下:

   Option explicit

   if WScript.Arguments.Count <> 3 then
      WScript.Echo "Missing parameters"
   else

   Dim imageMagick
   Set imageMagick = CreateObject("ImageMagickObject.MagickImage.1")

   Dim cam_add 
   Dim annotate 
   Dim filename 
   Dim cmd
   Dim WshShell
   Dim return

   cam_add = """http://" & WScript.Arguments(0) &"/image"""
   annotate = """" & WScript.Arguments(1) & " - """ '& Date
   filename = """" & WScript.Arguments(2) & WScript.Arguments(1) & ".jpg"""
   cmd = "convert " & cam_add & " -fill gold -pointsize 45 -gravity southwest -annotate         0x0+25+25 " & annotate & " -trim +repage -verbose " & filename

   WScript.Echo cmd

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

   WshShell.CurrentDirectory = "C:\Program Files\ImageMagick-6.8.0-Q16\"

   return = WshShell.Run(cmd) 


   end if

总之,我需要设置批次:

cscript.exe C:\ User \ username \ locationOfFile \ myScript.vbs IPADDRESS PicName Storage

所以可以根据我们在VB6表单中设置的内容来使用参数值。

2 个答案:

答案 0 :(得分:2)

如果你要:

shell "the.bat " & IPADDRESS & " " & PicName & " " & Storage

然后在the.bat内,每个空格分隔的参数都可以通过%N获得,其中N是序号;因此%1将包含IPADDRESS的值,%2将包含PicName的值,依此类推。

然后转发到VBScript:

cscript.exe C:\User\username\locationOfFile\myScript.vbs %1 %2 %3

(如果任何变量包含空格,则需要在传递到bat文件之前“引用”它们)

(在VB6中你也可能只是chdir(..)然后直接运行CScript)

答案 1 :(得分:1)

对于这么简单的事情,无论如何都不需要批处理文件。毕竟,您可以使用整个脚本

您甚至可以从那里设置CD,或者在VB6程序中将其设置为:

Option Explicit

Private Sub Main()
    Dim CD As String
    Dim IPADDRESS  As String
    Dim PicName As String
    Dim Storage As String
    Dim OrigCD As String

    CD = "D:\Photo Archives"
    IPADDRESS = "127.0.0.1"
    PicName = "Fudd"
    Storage = "C:\Storage"

    'Cache CD so we can restore it.
    'Change CD and drive so it is inherited.
    OrigCD = CurDir$()
    ChDir CD
    ChDrive CD

    Shell "cscript """ _
        & App.Path & "\test.vbs "" """ _
        & IPADDRESS & """ """ _
        & PicName & """ """ _
        & Storage & """", _
          vbNormalFocus

    'Restore CD and drive.
    ChDir OrigCD
    ChDrive OrigCD

    'Rest of program.
End Sub

脚本示例:

Option Explicit

Private CD

Private Sub Msg(ByVal Text)
    With WScript
        .StdOut.WriteLine Text
        .StdOut.Write "Press ENTER to continue..."
        .StdIn.ReadLine
    End With
End Sub

If WScript.Arguments.Count <> 3 Then
    Msg "Missing parameters"
    WScript.Quit
End If

'Show the CD and the arguments.
With CreateObject("WScript.Shell")
    CD = .CurrentDirectory
End With
With WScript.Arguments
    Msg CD & vbNewLine _
      & """" & .Item(0) _
      & """ """ & .Item(1) _
      & """ """ & .Item(2) & """"
End With

但它看起来甚至不需要脚本。您可以从VB6程序内部构建命令,设置CD /驱动器和Shell()所有构建的命令字符串。