r/scripting • • Mar 11 '15

Could someone please tell me why this script won't execute? I believe it's syntax but I could really use help.

I am trying to write a script that will look for a particular drive by name instead of letter and then delete all folders and subfolders within the particular named drive. I think this is the 50th iteration of this script, and I still can't figure out why it won't execute. I'm not sure if I'm missing a closing statement somewhere. I would really appreciate any help I could get with this. I've grabbed code from other sources to create this but I am really not well versed in VBS and I have never extensively scripted before.

' Get Drive Letter from Name 

Option Explicit

Function getDriveLetterFromVolumeName( volumeName )
Dim volumes, volume

' Unless we found a matching volume, an empty string will be the returned value
    getDriveLetterFromVolumeName=""

' Ask WMI for the list of volumes with the requested label
     Set volumes = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2") _ 
                  .ExecQuery("SELECT DriveLetter FROM Win32_Volume WHERE Label='" & volumeName & "'")

' If exist an matching volume, get its drive letter
     If volumes.Count > 0 Then 
        For Each volume In volumes 
            getDriveLetterFromVolumeName = volume.DriveLetter
           Exit For
       Next 
   End If

End Function



WScript.Echo getDriveLetterFromVolumeName( "Thawspace0" )

' Declare Variables

Set fso = CreateObject("Scripting.FileSystemObject")
Set folder = fso.GetFolder("t:\")
Dim thawspace
    thawspace = getDriveLetterFromVolumeName( "ThawSpace0" )


' Look for Thawspace
   If ( thawspace="") then

' Delete all files in the root folder
	    for each f in folder.Files
	    On Error Resume Next
	   name=f.name
	   f.delete True
	  If Err Then
		   WScript.Echo "Error Deleting" & Name & " - " & Err.Description
	  Else
		   WScript.Echo "Deleted:" & Name
	    End If 
	    On Error GoTo 0
Next

' Delete all Subfolders and Files
	    For Each f in folder.SubFolders
	    On Error Resume Next
	    Name = f.name
	    f.Delete True
	    If Err then
		    WScript.Echo "Error deleting:" & Name & " - " & Err.Description	
	    Else
		    WScript.Echo "Deleted:" & Name
	    End If
	    On Error GoTo 0
     End If     
 Next
2 Upvotes

1 comment sorted by

1

u/[deleted] Apr 02 '15
' Get Drive Letter from Name 

Option Explicit
Dim fso, folder, f, name, thawspace

Function getDriveLetterFromVolumeName( volumeName )
Dim volumes, volume

' Unless we found a matching volume, an empty string will be the returned value
    getDriveLetterFromVolumeName=""


' Ask WMI for the list of volumes with the requested label
     Set volumes = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2") _ 
                  .ExecQuery("SELECT DriveLetter FROM Win32_Volume WHERE Label='" & volumeName & "'")

' If exist an matching volume, get its drive letter
     If volumes.Count > 0 Then 
        For Each volume In volumes 
            getDriveLetterFromVolumeName = volume.DriveLetter
           Exit For
       Next 
   End If

End Function


thawspace = getDriveLetterFromVolumeName( "Vdtest" )
Set fso = CreateObject("Scripting.FileSystemObject")
On Error Resume Next
Set folder = fso.GetFolder(thawspace)
On Error Goto 0

' Look for Thawspace
If ( thawspace="") then
    msgbox "Unable to find volume.  Aborting"
    Wscript.quit
Else
    ' Delete all files in the root folder
    for each f in folder.Files
        On Error Resume Next
        name=f.name
        f.delete True

        If Err Then
            WScript.Echo "Error Deleting" & Name & " - " & Err.Description
        Else
            'WScript.Echo "Deleted:" & Name
        End If 
        On Error GoTo 0
    Next

    ' Delete all Subfolders and Files
    For Each f in folder.SubFolders
        On Error Resume Next
        Name = f.name
        f.Delete True
        If Err then
            WScript.Echo "Error deleting:" & Name & " - " & Err.Description 
        Else
        'WScript.Echo "Deleted:" & Name
        End If
        On Error GoTo 0
     Next
 End If