VBScript example: make folders from names of selected items

This is a place for users to discuss Altap Salamander. Please feel free to ask, answer questions, and express your opinion. Please do not post problems, bug reports or feature requests here.
Posts: 162
Joined: 10 Dec 2005, 15:44

VBScript example: make folders from names of selected items

Post by JohnFredC » 08 Mar 2010, 19:35

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
		Salamander.MsgBox "Please select or focus an item first.",0,"Nothing to do!"
	End If
	Dim Items
	Set Items = Salamander.SourcePanel.SelectedItems
End If

On error resume next

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)
	'--- Use selection
	Dim Item
	If VarType(Items) <> 0 Then
		'--- Set up progress dialog
		Salamander.ProgressDialog.Style = 1
		Salamander.ProgressDialog.Maximum = Items.Count
		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

				'--- 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

		'--- Allow user to review ProgressDialog.  Comment out for faster behavior.
	End If

End If

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

Jan Rysavy
Posts: 5196
Joined: 08 Dec 2005, 06:34
Location: Novy Bor, Czech Republic

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

Post by Jan Rysavy » 08 Mar 2010, 20:22

Note: this script requires Altap Salamander 2.53 beta 1 or newer. See http://forum.altap.cz/viewtopic.php?f=4&t=3799

Post Reply