【问题标题】:File Folder copy文件夹副本
【发布时间】:2010-03-23 04:48:56
【问题描述】:

下面是 VBScript 代码。如果文件或文件夹存在,我会收到脚本错误,“文件已存在”。

  • 如何解决这个问题?
  • 如何仅在文件夹不存在时创建文件夹,并仅复制源路径中新的或不存在的文件?
  • 如何在“欢迎”之后和(第 3 点)处插入用户名(第 1 点)而不是用户取消?
  • 可以将按钮更改为复制、更新、取消而不是是、否、取消吗? (第 2 点)

代码:

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set wshShell = WScript.CreateObject( "WScript.Shell" )
strUserName = wshShell.ExpandEnvironmentStrings( "%USERNAME%" )
Message = "       Welcome to the AVG Update Module" & vbCR '1*
Message = Message & "       *****************************" & vbCR & vbCR
Message = Message & "        Click Yes to  Copy   Definition Files" & vbCR & vbCR
Message = Message & "                            OR  " & vbCR & vbCR
Message = Message & "        Click  No to Update  Definition Files." & vbCR & vbCR
Message = Message & "        Click  Cancel (ESC) to Exit." & vbCR & vbCR
X = MsgBox(Message, vbYesNoCancel, "AVG Update Module") '2*
'Yes Selected Script
If X = 6 then
    objFSO.FolderExists("E:\Updates")
    if TRUE then objFSO.CreateFolder ("E:\Updates")
    objFSO.CopyFile "c:\Docume~1\alluse~1\applic~1\avg8\update\download\*.*",
    "E:\Updates\" ,   OverwriteFiles
    MsgBox "Files Copied Succesfully.", vbInformation, "Copy Success"
End If
'No Selected Script
If X = 7 then
    objFSO.FolderExists("Updates")
    if TRUE then objFSO.CreateFolder("Updates")
    objFSO.CopyFile "E:\Updates\*.*", "Updates", OverwriteFiles
    Message =  "Files Updated Successfully."  & vbCR & vbCR
    Message = Message & "Click  OK to Launch AVG GUI." & vbCR & vbCR
    Message = Message & "Click  Cancel (ESC) to Exit." & vbCR & vbCR
    Y = MsgBox(Message, vbOKCancel, "Update Success")
    If Y = 1 then
        Set WshShell = CreateObject("WScript.Shell")
        WshShell.Run chr(34) & "C:\Progra~1\avg\avg8\avgui.exe" & Chr(34), 0
        Set WshShell = Nothing
    End if
    If Y = 3 then WScript.Quit
End IF
'Cancel Selection Script
If X = 2 then
    MsgBox "No Files have been Copied/Updated.", vbExclamation, "User Cancelled" '3*
End if

【问题讨论】:

    标签: vbscript


    【解决方案1】:

    文件夹不存在时如何创建

    这是你的代码:

    objFSO.FolderExists("E:\Updates")
    if TRUE then objFSO.CreateFolder ("E:\Updates")
    

    只需按顺序调用FolderExistsCreateFolder 方法(总是调用CreateFolder,因为if TRUE 条件的计算结果为True)并且等于:

    objFSO.FolderExists("E:\Updates")
    objFSO.CreateFolder ("E:\Updates")
    

    你想根据FolderExists方法的返回值调用CreateFolder

    If Not objFSO.FolderExists("E:\Updates") Then
       objFSO.CreateFolder "E:\Updates"
    

    只复制新文件或源路径中不存在的文件?

    VBScript 和FileSystemObject 对象都没有此功能。但是,可以使用WshShell.Run 方法从您的脚本中调用可以执行此操作的外部工具,例如xcopy。我猜你需要这样的东西:

    Set WshShell = CreateObject("WScript.Shell")
    WshShell.Run "xcopy c:\Docume~1\alluse~1\applic~1\avg8\update\download\*.* E:\Updates\ /D", , True
    

    如何插入用户名(第 1 点)

    将消息文本与strUserName 变量值连接:

    Message = "       Welcome " & strUserName & " to the AVG Update Module" & vbCR
    ...
    MsgBox "No Files have been Copied/Updated.", vbExclamation, strUserName & " Cancelled"
    

    可以将按钮更改为复制、更新、取消而不是是、否、取消吗?(第 2 点)

    不,VBScript 的内置MsgBox 函数不支持自定义按钮。不过有一些解决方法:您可以使用HTA(HTML 应用程序)创建自定义消息框,或者使用InputBox 函数提示用户他们希望执行的任务。您可以找到示例 here


    我还想指出,您可以通过使用Select Case 语句来检查MsgBox 返回值而不是多个If...Then...End If 语句来改进您的脚本。此外,使用 6 或 7 之类的“幻数”是一种不好的做法 - 请改用适当的常量。例如:

    Select Case X
      Case vbYes
         ...
      Case vbNo
         ...
      Case Else ' vbCancel
         ...
    End Select
    

    【讨论】:

      【解决方案2】:

      当你说

      "只复制新的或做的文件 源路径中不存在?”

      您的意思是,如果目标目录中不存在文件,您只想将文件从源目录复制到目标目录吗?如果是这样,这将实现这一目标

      Const SourceFolder = "C:\Test1\"
      Const DestinationFolder = "C:\Test2\"
      
      Set fso = CreateObject("Scripting.FileSystemObject")
      'Get a collection of al the files in the source directory
      Set fileCol = fso.GetFolder(SourceFolder).Files
      
      'Loop through each file and check to see if it exists in the destination directory
      For Each objFile in fileCol
          If NOT fso.FileExists(DestinationFolder & objFile.Name) Then
              'If the file does not exist in the destination directory copy it there.
              objFile.Copy DestinationFolder
          Else
              If objFile.DateLastModified > fso.GetFile(DestinationFolder & objFile.Name).DateLastModified Then
                  'If the file is newer than the destination file copy it there
                  objFile.Copy DestinationFolder, True
              End If
          End If
      Next 
      Set fileCol = Nothing
      Set fso = Nothing
      

      添加了请求的日期检查。

      【讨论】:

      • 替换旧文件版本(“仅复制新文件”)怎么样?
      • 是否要比较创建日期、上次修改日期或上次访问日期?
      • 感谢 Helen 和 Tester101 的帮助。如何通过添加 if strUserName = DARIO than goto 'Yes selected Script else 'No selected script 来自动复制
      猜你喜欢
      • 2014-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-03
      • 2020-06-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多