14 October 2009

BosoNewFolder.vbs

Codice per creare una nuova cartella random e aprirla:
Option Explicit

Function GeneratePassword(strCharacters, intLength)
	
	Randomize
	Dim strS, intI
	
	For intI = 1 To intLength
		strS = strS + Mid(strCharacters, Int(Rnd() * Len(strCharacters))+1, 1)
	Next
	
	GeneratePassword=strS
	
End Function

Sub CreateNewFolder(baseFolder)
	
	const allowedChars = "abcdefgijklmnopqrstuvwxyz"
	const nameLength = 4
	
	
	const tempFolder = 2
	const runWaitOnReturn = True	' lo script deve aspettare che l'istruzione termini 
									' prima di continuare?
								
	dim fullPath
	dim fso
	dim wsh
	
	set wsh = CreateObject("WScript.Shell")

	
	set fso = CreateObject("Scripting.FileSystemObject")
	
	' genera un nome casuale che non esista
	fullPath = fso.GetSpecialFolder(tempFolder)
	
	while fso.FolderExists(fullPath)
		fullPath = GeneratePassword(allowedChars, nameLength)
	wend
	
	' normalizza la stringa
	fullPath = UCase(Left(fullPath, 1)) & Right(fullPath, Len(fullPath) - 1)
	
	' compone il path completo
	if right(baseFolder, 1) <> "\" then baseFolder = baseFolder & "\"
	fullPath = baseFolder & fullPath
	
	' crea directory
	fso.CreateFolder(fullPath)
	
	' open folder
	wsh.Run "explorer " & Chr(34) & fullPath & Chr(34), 5, runWaitOnReturn

	set wsh = nothing
	set fso = nothing
	
End Sub

CreateNewFolder(wscript.arguments.item(0))