如何为现有VBScript添加分支移动及目标文件夹打开功能?
Updated VBScript with Folder Destination Choice & Auto-Open Feature
Got it, let's tweak your existing script to add the YES/NO destination picker and auto-open functionality. Here's the full working code, followed by breakdowns of the new additions:
Option Explicit Const strDLFolder = "C:\Downloads" Const strDocsA = "C:\DOCUMENTS\A" Const strDocsB = "C:\DOCUMENTS\B" Dim objFSO, objWShell, objDLFolder, strNewFolder, objFile, intChoice, strTargetPath Set objFSO = CreateObject("Scripting.FileSystemObject") Set objWShell = CreateObject("WScript.Shell") ' Ensure source and destination folders exist first If Not objFSO.FolderExists(strDLFolder) Then objFSO.CreateFolder(strDLFolder) End If If Not objFSO.FolderExists(strDocsA) Then objFSO.CreateFolder(strDocsA) End If If Not objFSO.FolderExists(strDocsB) Then objFSO.CreateFolder(strDocsB) End If objWShell.CurrentDirectory = strDLFolder Set objDLFolder = objFSO.GetFolder(strDLFolder) ' Exit if no files to move If objDLFolder.Files.Count = 0 Then WScript.Quit End If ' Get valid folder name from user Do Err.Clear strNewFolder = InputBox("Folder Name", vbLf & "Enter the name of the folder to be created:") If strNewFolder = False Then ' User clicked Cancel WScript.Quit End If On Error Resume Next objFSO.CreateFolder strNewFolder Loop While Err.Number <> 0 Or Not objFSO.FolderExists(strNewFolder) On Error Goto 0 ' Move files to new folder For Each objFile In objDLFolder.Files objFSO.MoveFile objFile.Name, strNewFolder & "\" Next ' Ask user for destination folder intChoice = MsgBox("Move this folder to C:\DOCUMENTS\A?", vbYesNo + vbQuestion + vbSystemModal, "Choose Destination") ' Set target path based on user choice If intChoice = vbYes Then strTargetPath = strDocsA & "\" & objFSO.GetFileName(strNewFolder) Else strTargetPath = strDocsB & "\" & objFSO.GetFileName(strNewFolder) End If ' Move the new folder to chosen destination objFSO.MoveFolder strNewFolder, strTargetPath ' Auto-open the target folder objWShell.Run "explorer.exe """ & strTargetPath & """", 1, False ' Confirmation popup objWShell.Popup "Files moved and folder opened.", 0, "Complete", vbInformation Or vbSystemModal Or &h00040000&
Key New Features Explained:
- Destination Folder Prep: We added checks to create
C:\DOCUMENTS\AandC:\DOCUMENTS\Bif they don't exist—this prevents errors when trying to move the folder later. - YES/NO Dialog: The
MsgBoxwithvbYesNo + vbQuestiongives users a clear choice. We capture the return value (vbYes= 6,vbNo=7) to set the correct target path. - Folder Movement: Using
objFSO.MoveFolderto relocate the newly created folder to the user-selected destination. - Auto-Open Folder:
objWShell.Runlaunches File Explorer directly to the final folder path—we wrap the path in quotes to handle any spaces in folder names. - Robust Error Handling: We kept the original loop to ensure a valid folder name is entered, and added safeguards for missing destination directories.
内容的提问来源于stack exchange,提问作者glenn masters
相关产品推荐
相关产品推荐

