【问题标题】:Using xcopy in VBScript在 VBScript 中使用 xcopy
【发布时间】:2009-12-25 11:48:54
【问题描述】:

这是我之前的问题(Copy Update Create VBScriptFile Folder copy)的后续。到目前为止,我有以下脚本可以使用 xcopy 复制文件:

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set wshShell = WScript.CreateObject("WScript.Shell")
strUserName = wshShell.ExpandEnvironmentStrings("%USERNAME%")

' Discover Current Drive Path
curDrv = objFSO.GetParentFolderName(WScript.ScriptFullName) 'Drive Path
' USB Drive  and folder Path
upath = curdrv & "\ACG" 

' Source
avg8 = "c:\Docume~1\alluse~1\applic~1\avg8\update\download\*.*"
avg9 = "c:\Docume~1\alluse~1\applic~1\avg9\update\download\*.*"

If struserName = DARIO Then 
  '(1) GOTO Update
End If

If Not objFSO.FolderExists (upath) Then
  objFSO.CreateFolder (upath)
End If

If objFSO.FolderExists (avg9) Then
  'WshShell.Run "xcopy c:\Docume~1\alluse~1\applic~1\avg9\update\download\*.* usbdrive:\acg /D", , True
  '(2) WshShell.Run "xcopy avg9 upath /D", , True
  WshShell.Run "xcopy " & avg9 & " " & upath & " /D", , True
End If

If objFSO.FolderExists (avg8) Then
  'WshShell.Run "xcopy c:\Docume~1\alluse~1\applic~1\avg8\update\download\*.* &    usbdrive:\acg & /D", , True
  '(3) WshShell.Run "xcopy avg8 upath /D", , True
  WshShell.Run "xcopy " & avg8 & " " & upath & " /D", , True
End if

MsgBox "Definition Files Copied to your USB Drive @ " & upath, vbInformation, "Copy   Success..."
WScript.Quit

' Update 
If Not objFSO.FolderExists("C:\Updates") Then
  objFSO.CreateFolder "C:\Updates"
End If

If objFSO.FolderExists (upath) then
  Wshshell.Run "xcopy " & upath  & " " & "C:\Updates /D", , True '(4)
  MsgBox "Update Files Copied to C:\Updates" , vbInformation, "Copy Destination"
End IF

' Process
Message = "Click OK to Start Updating product." & vbCr & vbCr
Message = Message & "Click Cancel or (Esc) to Exit." &vbCr & vbCr
Message = Message & "Keep Selecting OK until you get" & vbCr
Message = Message & "the Message :-" & vbCr & vbCr
Message = Message & "''No New Update Files Available''" & vbCR & vbCR
X = MsgBox(Message, vbOKCancel, "AVG Update Module")

Select Case X
  Case vbCancel 
    MsgBox (strUserName & " cancelled the process.") , vbCritical, "Operation Terminated."
    Wscript.Quit

  Case vbOK
    If objFSO.FileExists("C:\Program Files\AVG\AVG89\avgupd.exe") Then
      WshShell.Run "C:\Program Files\AVG\AVG9\avgupd.exe" /source=folder /path="C:\Updates"
    End If

    If objFSO.FileExists("C:\Program Files\AVG\AVG8\avgupd.exe") Then
      WshShell.Run "C:\Program Files\AVG\AVG8\avgupd.exe" /source=folder /path="C:\Updates"
      '(5) Loop to Process
    End If
End Select

不幸的是,xcopy 没有发生。有人可以解释一下脚本有什么问题并指出我正确的方向来实现点 (1) - (5) 吗?谢谢。

【问题讨论】:

  • 为什么使用 xcopy 而不是 FileSystemObjects 复制方法?

标签: vbscript xcopy


【解决方案1】:

像这样使用 FileSystemObject.CopyFile 而不是 xcopy。

If objFSO.FolderExists (avg9) Then
  objFSO.CopyFile "c:\Docume~1\alluse~1\applic~1\avg9\update\download\*.*", "usbdrive:\acg", True
End If

也使用 If Else 而不是 GOTO

If struserName = "DARIO" Then
    'Do whatever should be done if the user is DARIO
Else
    'Do whatever should be done if the user is not DARIO
End If

你也可以使用这样的子程序或函数

if struserName = "DARIO" Then
    DoDARIOStuff
else
   DoOtherStuff
end if

然后像这样在代码的其他地方定义子程序

Sub DoDARIOStuff
    'Do whatever should be done if the user is DARIO
End Sub

Sub DoOtherStuff
    'Do whatever should be done if the user is not DARIO
End Sub

【讨论】:

    猜你喜欢
    • 2014-12-01
    • 2015-08-26
    • 2011-01-29
    • 1970-01-01
    • 2021-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多