文件夹复制 [英] File Folder copy
问题描述
下面是 VBScript 代码.如果文件或文件夹存在,我会收到脚本错误,文件已经存在".
- 如何解决这个问题?
- 如何仅在文件夹不存在时创建文件夹并仅复制源路径中的新文件或不存在的文件?
- 如何在欢迎"之后和(第 3 点)插入用户名(第 1 点)而不是用户取消?
- 可以将按钮更改为复制、更新、取消而不是是、否、取消吗?(第 2 点)
代码:
Set objFSO = CreateObject("Scripting.FileSystemObject")设置 wshShell = WScript.CreateObject("WScript.Shell")strUserName = wshShell.ExpandEnvironmentStrings( "%USERNAME%" )消息 =欢迎使用 AVG 更新模块"&VBCR '1*消息 = 消息 &********************************" &vbCR &vbCR消息 = 消息 &单击是复制定义文件"&vbCR &vbCR消息 = 消息 &或" &vbCR &vbCR消息 = 消息 &" 单击否更新定义文件."&vbCR &vbCR消息 = 消息 &" 单击取消 (ESC) 退出."&vbCR &vbCRX = MsgBox(Message, vbYesNoCancel, "AVG 更新模块") '2*'是选定的脚本如果 X = 6 那么objFSO.FolderExists("E:\Updates")如果为 TRUE,则 objFSO.CreateFolder ("E:\Updates")objFSO.CopyFile "c:\Docume~1\alluse~1\applic~1\avg8\update\download\*.*","E:\Updates\" , OverwriteFilesMsgBox "文件复制成功.", vbInformation, "复制成功"万一'没有选定的脚本如果 X = 7 那么objFSO.FolderExists("更新")如果为 TRUE,则 objFSO.CreateFolder("Updates")objFSO.CopyFile "E:\Updates\*.*", "Updates", OverwriteFilesMessage = "文件更新成功."&vbCR &vbCR消息 = 消息 &单击确定启动 AVG GUI."&vbCR &vbCR消息 = 消息 &单击取消(ESC)退出."&vbCR &vbCRY = MsgBox(Message, vbOKCancel, "更新成功")如果 Y = 1 那么Set WshShell = CreateObject("WScript.Shell")WshShell.Run chr(34) &"C:\Progra~1\avg\avg8\avgui.exe" &铬(34), 0设置 WshShell = 无万一如果 Y = 3,则 WScript.Quit万一'取消选择脚本如果 X = 2 那么MsgBox "没有文件被复制/更新.", vbExclamation, "用户已取消" '3*万一
<小时>
文件夹不存在时如何创建
这是您的代码:
objFSO.FolderExists("E:\Updates")如果为 TRUE,则 objFSO.CreateFolder ("E:\Updates")
简单地依次调用 FolderExists
和 CreateFolder
方法(CreateFolder
总是被调用,因为 if TRUE
条件计算结果为 True) 并等于:
objFSO.FolderExists("E:\Updates")objFSO.CreateFolder ("E:\Updates")
您想根据 FolderExists
方法的返回值调用 CreateFolder
:
如果不是 objFSO.FolderExists("E:\Updates") 然后objFSO.CreateFolder "E:\Updates"
<块引用>
只复制新文件或源路径中不存在的文件?
VBScript 和 FileSystemObject
对象具有此功能.但是,可以使用 WshShell.Run
方法.我想你需要这样的东西:
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 = " 欢迎 " &strUserName &到 AVG 更新模块"&vbCR...MsgBox "没有文件被复制/更新.", vbExclamation, strUserName &"取消"
<块引用>
是否可以将按钮更改为复制、更新、取消而不是是、否、取消?(第 2 点)
不,VBScript 的内置 MsgBox
函数不支持自定义按钮.不过有解决方法:您可以使用 HTA(HTML 应用程序)或使用 InputBox
功能来提示用户他们希望执行的任务.您可以在此处找到示例.
我还想指出,您可以使用 Select Case
语句来检查 MsgBox
返回值而不是多个 If...Then...End If
语句.此外,使用幻数"(如 6 或 7)是一种不好的做法 - 而是使用适当的常量.例如:
选择案例 X案例 vb 是...案例vbNo...Case Else ' vb 取消...结束选择
Below is the VBScript code. If the file/s or folder exist I get scripting error, "File already exists".
- How to fix that?
- How to create folder only if it does not exist and copy files only that are new or do not exist in source path?
- How to insert the username (Point 1) after "Welcome" and at (Poin 3) instead of user cancelled?
- Can the buttons be changed to Copy,Update,Cancel instead of Yes,No,Cancel? (Point 2)
The code:
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
How to create folder only if it does not exist
This your code:
objFSO.FolderExists("E:\Updates")
if TRUE then objFSO.CreateFolder ("E:\Updates")
simply calls the FolderExists
and CreateFolder
methods in sequence (CreateFolder
is always called because the if TRUE
condition evaluates to True) and is equal to:
objFSO.FolderExists("E:\Updates")
objFSO.CreateFolder ("E:\Updates")
You want to call CreateFolder
depending on the return value of the FolderExists
method:
If Not objFSO.FolderExists("E:\Updates") Then
objFSO.CreateFolder "E:\Updates"
and copy files only that are new or do not exist in source path?
Neither VBScript nor the FileSystemObject
object have this functionality. However, it is possible to call an external tool that can do that, such as xcopy
, from your script using the WshShell.Run
method. I guess you need something like this:
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run "xcopy c:\Docume~1\alluse~1\applic~1\avg8\update\download\*.* E:\Updates\ /D", , True
How to insert the username (Point 1)
Concatenate the message text with the strUserName
variable value:
Message = " Welcome " & strUserName & " to the AVG Update Module" & vbCR
...
MsgBox "No Files have been Copied/Updated.", vbExclamation, strUserName & " Cancelled"
Can the buttons be changed to Copy,Update,Cancel Instead of Yes,No,Cancel?(Point 2)
No, VBScript's built-in MsgBox
function does not support custom buttons. There're workarounds though: you could create your custom message box using an HTA (HTML application) or use the InputBox
function to prompt the user for the task they wish to perform. You can find examples here.
I'd also like to note that you can improve your script by using the Select Case
statement to check the MsgBox
return value instead of multiple If...Then...End If
statements. Also, it's a bad practice to use "magic numbers" like 6 or 7 - use the appropriate constants instead. For example:
Select Case X
Case vbYes
...
Case vbNo
...
Case Else ' vbCancel
...
End Select
这篇关于文件夹复制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!