Page 1 of 1

VBScript example: make folders from names of selected items

Posted: 08 Mar 2010, 19:35
by JohnFredC
Here is a little VBScript I wrote to create new folders in the "opposite" panel based on a selection. It demonstrates iterating through a selection and performing an action on each member.

Code: Select all

'--------------------------------------------------------------------------------------
'---	Make Folder (VBScript).vbs
'---	Creates folder(s) in the opposite panel with the name(s) of the selected item(s)
'--------------------------------------------------------------------------------------

Dim UseFocusedItem
	 UseFocusedItem = False

'--- Identify the item(s) to use for the foldername(s)
If Salamander.SourcePanel.SelectedItems.Count = 0 Then
	If Salamander.SourcePanel.FocusedItem.Name <> ".." then
		UseFocusedItem = True
	Else
		Salamander.MsgBox "Please select or focus an item first.",0,"Nothing to do!"
		Salamander.AbortScript()
	End If
Else
	Dim Items
	Set Items = Salamander.SourcePanel.SelectedItems
End If

On error resume next

Dim FSO
Set FSO = Createobject("scripting.filesystemobject")

'-- Create new folders in opposite panel
Dim NewFolderRootFolderName
	NewFolderRootFolderName = Salamander.TargetPanel.Path

Dim NewFolder
Dim NewFolderName
Dim ItemsCount
Dim ErrorMessage

If UseFocusedItem = True then
	'--- No selection found, so use focused item only
	NewFolderName = NewFolderRootFolderName & "\" & Salamander.SourcePanel.FocusedItem.Name
	Set NewFolder = FSO.CreateFolder(NewFolderName)
Else
	'--- Use selection
	Dim Item
	If VarType(Items) <> 0 Then
		'--- Set up progress dialog
		Salamander.ProgressDialog.Style = 1
		Salamander.ProgressDialog.Maximum = Items.Count
		Salamander.ProgressDialog.Show
		Salamander.ProgressDialog.AddText "Making folders in: " & NewFolderRootFolderName & vbcrlf & vbcrlf

		'--- Iterate through the selected items and make a corresponding folder in the opposite panel
		For Each Item In Items
			If Item.Name <> ".." Then
				Salamander.ProgressDialog.Step(1)

				'--- Construct name
				NewFolderName = NewFolderRootFolderName & "\" & Item.Name
				'--- Make the folder with the name of the item
				Set NewFolder = FSO.CreateFolder(NewFolderName)
				If Err <> 0 then ErrorMessage = Err.Description else ErrorMessage ="OK"

				Salamander.ProgressDialog.AddText Item.Name & " - " & ErrorMessage
				Err = 0
			End If
		Next

		'--- Allow user to review ProgressDialog.  Comment out for faster behavior.
		Salamander.Sleep(3000)
	End If

End If

The error handling could be enhanced a bit, but I have had no trouble with it.

Re: VBScript example: make folders from names of selected it

Posted: 08 Mar 2010, 20:22
by Jan Rysavy
Note: this script requires Altap Salamander 2.53 beta 1 or newer. See http://forum.altap.cz/viewtopic.php?f=4&t=3799