Deltree


  1. Posts : 120
    Win 7 Ultimate x64 & x86
       #1

    Deltree


    Folks, I have a script that I wrote to go through my HDD's, find any directory that has temp or temporary in it, and delete the entire contents and sub contents. With a 32 bit system, I used to just copy deltree to my system32 directory and use that command within my script. But with x64, that is not compatible.

    Does anyone have a program that works similarly? I know I can use RD /S /Q which achieves almost the same thing, but if the directory is not empty, it will fail. Does anyone know of a way to accomplish what I am looking for?

    BTW, Here is the script if anyone is interested...

    Code:
    @echo off
    REM ########################################################
    REM #                                                      #
    REM # Author: John V. Nelson                               #
    REM # Date:  09 - 04 - 2009                                #
    rem # Notes: Ensure you have DELTREE installed in          #
    rem #        System32.  This batch file with search out    #
    rem #        all directories with TEMP or Temporary and    #
    rem #        remove the contents but leave the directory.  #
    REM #                                                      #
    REM # Use:  To run, type DelTempDirs.bat TempDirs2.txt     #
    rem ########################################################
    cd c:
    REM Write directories out that need to be cleaned
    rem
    if exist c:\scripts\TempDirs2.txt del /q c:\scripts\TempDirs2.txt
    dir c:\*temp /a:D /s /b > c:\scripts\TempDirs.txt
    dir c:\*Temporary* /a:D /s /b >> c:\scripts\TempDirs.txt
    
    REM Add the backslash "\" to the end of each line so the directory does not get deleted itself
    set addtext=\
    for /f "delims=" %%l in (c:\scripts\TempDirs.txt) Do (
          echo %%l%addtext% >> c:\scripts\TempDirs2.txt
    )
    @echo off
    if "%1" NEQ "" goto :checkfile
    echo Usage: c:\scripts\TempDirs2.txt
    echo.
    goto :eof
    
    :checkfile
    if exist %1 goto :DoDelTree
    echo Input file does not exist
    echo.
    goto :eof
    
    :DoDelTree
    for /f "tokens=*" %%I in (%1) do deltree /Y %%I
    goto :eof
    
    :EOF
    Echo Done!

      My Computer


  2. Posts : 120
    Win 7 Ultimate x64 & x86
    Thread Starter
       #2

    Okay, I figured something out with a VBS script. The use is similar to regular DELTREE. The use is as such.

    If you wanted to delete the contents of the directory c:\Temp, but not remove the TEMP directory, usage would be:

    DELTREE.vbs c:\Temp, TRUE

    If you wish to delete the contents of c:\Temp, AND the directory c:\temp...

    DELTREE.vbs c:\Temp, FALSE

    Here is the code...

    Code:
    Option Explicit
    
    Dim objFSO, objTempFolder, strTempFolder
    
    Const TEMP_FOLDER = 2
    
    Set objFSO        = CreateObject( "Scripting.FileSystemObject" )
    Set objTempFolder = objFSO.GetSpecialFolder( TEMP_FOLDER )
    strTempFolder     = objTempFolder.Path
    
    DelTree strTempFolder, True
    
    
    Sub DelTree( myFolder, blnKeepRoot )
    ' With this subroutine you can delete folders and their content,
    ' including subfolders.
    '
    ' You can specify if you only want to empty the folder, and thus
    ' keep the folder itself, or to delete the folder itself as well.
    ' Root directories and some (not all) vital system folders are
    ' protected: if you try to delete them you'll get a message that
    ' deleting these folders is not allowed.
    '
    ' Use: Deltree.vbs <Path to delete>, True (false)
    '
    ' Arguments:
    ' myFolder     [string]   the folder to be emptied or deleted
    ' blnKeepRoot  [boolean]  if True, the folder is emptied only,
    '                         otherwise it will be deleted itself too
    '
    '
        Dim arrSpecialFolders(3)
        Dim objMyFSO, objMyFile, objMyFolder, objMyShell
        Dim objPrgFolder, objPrgFolderItem, objSubFolder, wshMyShell
        Dim strPath, strSpecialFolder
    
        Const WINDOWS_FOLDER =  0
        Const SYSTEM_FOLDER  =  1
        Const PROGRAM_FILES  = 38
    
        ' Use custom error handling
        On Error Resume Next
    
        ' List the paths of system folders that should NOT be deleted
        Set wshMyShell       = CreateObject( "WScript.Shell" )
        Set objMyFSO         = CreateObject( "Scripting.FileSystemObject" )
        Set objMyShell       = CreateObject( "Shell.Application" )
        Set objPrgFolder     = objMyShell.Namespace( PROGRAM_FILES )
        Set objPrgFolderItem = objPrgFolder.Self
    
        arrSpecialFolders(0) = wshMyShell.SpecialFolders( "MyDocuments" )
        arrSpecialFolders(1) = objPrgFolderItem.Path
        arrSpecialFolders(2) = objMyFSO.GetSpecialFolder( SYSTEM_FOLDER  ).Path
        arrSpecialFolders(3) = objMyFSO.GetSpecialFolder( WINDOWS_FOLDER ).Path
    
        Set objPrgFolderItem = Nothing
        Set objPrgFolder     = Nothing
        Set objMyShell       = Nothing
        Set wshMyShell       = Nothing
    
        ' Check if a valid folder was specified
        If Not objMyFSO.FolderExists( myFolder ) Then
            WScript.Echo "Error: path not found (" & myFolder & ")"
            WScript.Quit 1
        End If
        Set objMyFolder = objMyFSO.GetFolder( myFolder )
    
        ' Protect vital system folders and root directories from being deleted
        For Each strSpecialFolder In arrSpecialFolders
            If UCase( strSpecialFolder ) = UCase( objMyFolder.Path ) Then
                WScript.Echo "Error: deleting """ _
                           & objMyFolder.Path & """ is not allowed"
                WScript.Quit 1
            End If
        Next
    
        ' Protect root directories from being deleted
        If Len( objMyFolder.Path ) < 4 Then
            WScript.Echo "Error: deleting root directories is not allowed"
            WScript.Quit 1
        End If
    
        ' First delete the files in the directory specified
        For Each objMyFile In objMyFolder.Files
            strPath = objMyFile.Path
            objMyFSO.DeleteFile strPath, True
    
        Next
    
        ' Next recurse through the subfolders
        For Each objSubFolder In objMyFolder.SubFolders
            DelTree objSubFolder, False
        Next
    
        ' Finally, remove the "root" directory unless it should be preserved
        If Not blnKeepRoot Then
            strPath = objMyFolder.Path
            objMyFSO.DeleteFolder strPath, True
    
        End If
    
        ' Clean-up after thyself
        On Error Goto 0
        Set objMyFolder = Nothing
        Set objMyFSO    = Nothing
    End Sub
    Note: Just copy the code above, paste it into notepad, and save it as DELTREE.vbs (or whatever you wish to name it). Run it from a command prompt. You may have to use a fully qualified path depending on where you save it.
    Last edited by Perrybucsdad; 16 Oct 2009 at 13:52.
      My Computer


  3. Posts : 7
    Windows 7 Ultimate 64-bit
       #3

    DELTREE.vbs


    in a batch file, do we do
    DELTREE.vbs c:\windows\temp TRUE
    or
    DELTREE.vbs c:\windows\temp, TRUE
      My Computer


 

Our Sites
Site Links
About Us
Windows 7 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 7" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 02:14.
Find Us