取消后停止功能运行 - 批处理文件

时间:2016-05-25 14:26:36

标签: batch-file vbscript

我试图运行一个提示用户输入位置的脚本,一旦选择了该位置,脚本就会将IE收藏夹复制到该位置。(它是一个更大项目的一部分)将多个目录复制到您选择的位置)

首先使用VBS脚本创建提示,然后根据所选内容创建变量。然后脚本将IE收藏夹复制到该选定位置。问题是,如果我选择"取消"无论如何脚本都会运行!

非常感谢任何帮助!

代码:(批处理脚本)

@echo off

For /F "Tokens=1 Delims=" %%I In ('cscript //nologo BrowseFolder.vbs') Do Set _FolderName=%%I

set dest=%_FolderName%

md "%dest%\UserBackup" &md "%dest%\UserBackup\Favorites" &ROBOCOPY /E /XJ "%userprofile%\Favorites" "%dest%\UserBackup\Favorites"  

代码:(VBS脚本) - 另存为" BrowseFolder.vbs"如果你想以它为例运行它。

Const MY_COMPUTER = &H11&
Const WINDOW_HANDLE = 0
Const OPTIONS = 0

Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(MY_COMPUTER)
Set objFolderItem = objFolder.Self
strPath = objFolderItem.Path

Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.BrowseForFolder _
(WINDOW_HANDLE, "Please select the destination for your data:", OPTIONS, strPath)

If objFolder Is Nothing Then
Wscript.Quit
End If

Set objFolderItem = objFolder.Self
objPath = objFolderItem.Path

Wscript.Echo objPath

提前谢谢!

1 个答案:

答案 0 :(得分:1)

Windows批处理仍在运行,因为您没有检查传回的变量是否为empty or not。 VBs脚本上的取消按钮只会取消VBS脚本。

在Windows批处理文件中尝试此操作:

For /F "Tokens=1 Delims=" %%I In ('cscript //nologo BrowseFolder.vbs') Do Set _FolderName=%%I

IF [%_FolderName%] == [] GOTO EndBatch

set dest=%_FolderName%

md "%dest%\UserBackup" &md "%dest%\UserBackup\Favorites" &ROBOCOPY /E /XJ "%userprofile%\Favorites" "%dest%\UserBackup\Favorites"

:EndBatch