Simple Script Problem  
Author Message
RedThumb





PostPosted: Sat Apr 01 09:48:34 CST 2006 Top

VB Scripts >> Simple Script Problem

This simple script is designed to delete all files in a directory and
subdirectories, but leave the directory structure intact.
It works great, except it never exist the outer loop. Iv'e traced it and it
hangs at the outer NEXT.

I'm sure it's something STUPID I did, but I can't figure out what!

Any ideas?

---------------------------------------------------------------------------------------------------------------

' VDel.vbs -- script to clear out a directory but leave structure intact.
' -- Set Computer / Drive / FolderRoot (lowercase)
' No Confirmation or Output is included

strComputer = "."
strFolderRoot = "test"
strDrive = "f:"


On Error Resume Next
Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")

strQuery = "SELECT NAME FROM Win32_Directory where NAME Like '" & strDrive &
"\\" & strFolderRoot & "%'"
Set colFolders = objWMIService.ExecQuery(strQuery, "WQL",
wbemFlagReturnImmediately + wbemFlagForwardOnly)

For each objFolder in colFolders
strTMP = Replace(Mid(objFolder.Name,4,Len(objFolder.Name)),"\","\\")

strQuery = "SELECT NAME FROM CIM_DataFile WHERE Drive = '" & strDrive &
"' AND Path Like '\\" & strTMP & "\\' "
Set colItems = objWMIService.ExecQuery(strQuery, "WQL",
wbemFlagReturnImmediately + wbemFlagForwardOnly)

For Each objItem In colItems
objItem.delete
Next
Next ' Script hangs here after last subdirectory
wscript.Quit

Visual Studio231  
 
 
Richard





PostPosted: Sat Apr 01 09:48:34 CST 2006 Top

VB Scripts >> Simple Script Problem Hi,

I think WMI is running in the background searching your drive. I have never
seen your "Like" syntax used. I would suggest using the code in listing 11.3
linked here to enumerate subfolders:

http://www.microsoft.com/technet/scriptcenter/guide/sas_fil_kgai.mspx

--
Richard
Microsoft MVP Scripting and ADSI
Hilltop Lab - http://www.rlmueller.net



> This simple script is designed to delete all files in a directory and
> subdirectories, but leave the directory structure intact.
> It works great, except it never exist the outer loop. Iv'e traced it and
> it
> hangs at the outer NEXT.
>
> I'm sure it's something STUPID I did, but I can't figure out what!
>
> Any ideas?
>
> ---------------------------------------------------------------------------------------------------------------
>
> ' VDel.vbs -- script to clear out a directory but leave structure intact.
> ' -- Set Computer / Drive / FolderRoot (lowercase)
> ' No Confirmation or Output is included
>
> strComputer = "."
> strFolderRoot = "test"
> strDrive = "f:"
>
>
> On Error Resume Next
> Const wbemFlagReturnImmediately = &h10
> Const wbemFlagForwardOnly = &h20
>
> Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
>
> strQuery = "SELECT NAME FROM Win32_Directory where NAME Like '" & strDrive
> &
> "\\" & strFolderRoot & "%'"
> Set colFolders = objWMIService.ExecQuery(strQuery, "WQL",
> wbemFlagReturnImmediately + wbemFlagForwardOnly)
>
> For each objFolder in colFolders
> strTMP = Replace(Mid(objFolder.Name,4,Len(objFolder.Name)),"\","\\")
>
> strQuery = "SELECT NAME FROM CIM_DataFile WHERE Drive = '" & strDrive &
> "' AND Path Like '\\" & strTMP & "\\' "
> Set colItems = objWMIService.ExecQuery(strQuery, "WQL",
> wbemFlagReturnImmediately + wbemFlagForwardOnly)
>
> For Each objItem In colItems
> objItem.delete
> Next
> Next ' Script hangs here after last subdirectory
> wscript.Quit
>