Vbscript 文件夹副本

Vbscript 文件夹副本,vbscript,Vbscript,下面是VBScript代码。如果文件或文件夹存在,我会收到脚本错误,“文件已存在” 如何解决这个问题 如何仅在文件夹不存在时创建文件夹,并仅复制源路径中新的或不存在的文件 如何在“欢迎”之后插入用户名(第1点)并在(第3点)插入,而不是用户取消 是否可以将按钮更改为复制、更新、取消而不是是、否、取消?(第2点) 守则: Set objFSO = CreateObject("Scripting.FileSystemObject") Set wshShell = WScript.CreateOb

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

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

如何仅在文件夹不存在时创建该文件夹

这是您的代码:

objFSO.FolderExists("E:\Updates")
if TRUE then objFSO.CreateFolder ("E:\Updates")
只需按顺序调用
FolderExists
CreateFolder
方法(
CreateFolder
始终被调用,因为
if TRUE
条件的计算结果为TRUE),并且等于:

objFSO.FolderExists("E:\Updates")
objFSO.CreateFolder ("E:\Updates")
您想调用
CreateFolder
,具体取决于
FolderExists
方法的返回值:

If Not objFSO.FolderExists("E:\Updates") Then
   objFSO.CreateFolder "E:\Updates"
是否仅复制源路径中新的或不存在的文件

VBScript和对象都不具有此功能。但是,可以使用该方法从脚本调用一个可以执行此操作的外部工具,例如
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的内置函数不支持自定义按钮。不过还有一些变通方法:您可以使用(HTML应用程序)创建自定义消息框,或者使用该函数提示用户执行他们希望执行的任务。您可以找到示例。


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

Select Case X
  Case vbYes
     ...
  Case vbNo
     ...
  Case Else ' vbCancel
     ...
End Select
当你说

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

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

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而不是转到“Yes selected Script else”No selected Script来自动复制