VB Script: vbs to move selected files and folders in the current folder to a new custom folder by input box

Jakov

New member
Local time
Today, 16:27
Joined
Nov 25, 2024
Messages
5
Hi,
I made this code with the assistance of Ai, to move selected files and folders in the current folder to a new custom folder by input box
Code:
Dim objFSO, objShell, destinationFolder, currentFolder, newFolderName
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")

' Get the current folder (where the script is executed)
currentFolder = objShell.CurrentDirectory

' Prompt user for new folder name
newFolderName = InputBox("Enter the name of the new folder:", "Create New Folder")

' Exit if no input is provided
If Trim(newFolderName) = "" Then
    WScript.Quit
End If

' Build the path for the new folder
destinationFolder = objFSO.BuildPath(currentFolder, newFolderName)

' Create the folder if it doesn't already exist
If Not objFSO.FolderExists(destinationFolder) Then
    objFSO.CreateFolder(destinationFolder)
End If

' Loop through the selected items passed as arguments
For Each item In WScript.Arguments
    If objFSO.FolderExists(item) Then
        ' If the item is a folder, move it
        objFSO.MoveFolder item, destinationFolder & "\"
    ElseIf objFSO.FileExists(item) Then
        ' If the item is a file, move it
        objFSO.MoveFile item, destinationFolder & "\"
    End If
Next

' Notify the user
MsgBox "Files and folders moved successfully to " & destinationFolder, vbInformation, "Operation Completed"
it works fine for moving single file or folder, but when selecting multiple file and folder, an input boxes appear according to the number of the selected files and folder, if I selected 2 files or folders ----> two input boxes appear, 50 files or folders ----> 50 input boxes and so on. So how to make input box appear only once to type the desired folder name and completing the moving process. Furthermore, if anyone have another code to do the same function, please provided it, as it will be so helpful for me with special thanks.
 
Hi. Welcome to AWF!

I can't test your code right now, but I am just curious as to how exactly are you selecting files. An input box requires you to enter a text. Are you entering multiple filenames?

Sent from phone...
 
Hi. Welcome to AWF!

I can't test your code right now, but I am just curious as to how exactly are you selecting files. An input box requires you to enter a text. Are you entering multiple filenames?

Sent from phone...
Thanks for reply,
No no I didn't type the file names in the input box, to clarify the condition, I made vbs script and added it to context menu, so I select files by mouse, Ctrl, Shift, then right click -----> Move to custom folder, then input box appear to type the desired folder name, type and click Ok.
 
Does item have a name property?, if so try using that for the move file operation.
 
I expect the script will be called for each folder/file separately.

You could check that by taking a look into 'WScript.Arguments.Count' (maybe with a message box).
 
You already knew that? You didn't tell us. ;)
 
But that's a gamechanger. Your script is fine, but one can't use it.
 
Your current code doesn't raise an inputbox multiple times, just once.

So your problem, as someone sort of pointed out already, is in how this code is being called.
 
You'll need to move your Inputbox to the calling code and pass it as a wscript argument to the child code
 

Users who are viewing this thread

Back
Top Bottom