2

Is there a good tool to find $RECYCLE.BIN folders on network shares and their size?

In our environment we use roaming profiles and folder redirection for storing user profiles/data. Users are often lazy in cleaning up their recycle bin and so our network shares fill up.

I already tried it with a PowerShell script but it often fails or timeouts.

Thanks for any advice.

Best.

mazebuhu
  • 137
  • 1
  • 5
  • 13

2 Answers2

2

Thanks all for your answers. I found a shorter and in my opinion a more elegant way to to find "$RECYCLE.BIN" recursively on a network share or local directory using Windows PowerShell:

Get-ChildItem \\server\share -Directory -Hidden -Filter '$REC*' -Recurse | ft Name,FullName

or you simply cd into the root directory where you want to start your search:

cd C:\Directory
Get-ChildItem -Directory -Hidden -Filter '$REC*' -Recurse | ft Name,FullName

If you additionally want to display the size of the $RECYCLE.BIN folder you can use this, a bit more complicated, command:

Get-ChildItem \\server\share\some\path -Directory -Hidden -Filter '$REC*' -Recurse | Select-Object Name,FullName,@{l="Size in MB";e={"{0:N2}" -f ((Get-ChildItem $_.FullName -Force | Measure-Object -Property length -sum).sum / 1MB)}} 

or shorter:

gci \\server\share\some\path -Directory -Hidden -Filter '$REC*' -Recurse | select Name,FullName,@{l="Size in MB";e={"{0:N2}" -f ((gci $_.FullName -Force | measure -Property length -sum).sum / 1MB)}} 

(Keep in mind that if use use "Select-Object" you truncate the original return object only the specified members.)

Best!

mazebuhu
  • 137
  • 1
  • 5
  • 13
1

A really dirty command-prompt one-liner might be:

FOR /d /r "D:\" %A IN ($RECYCLE.BIN) DO @IF EXIST "%~A" ECHO %~A & ROBOCOPY /l /s /nfl /fp /np /ndl /nfl /njh /r:0 "%~A" "%~A"

(substitute "D:\" for the root path of your search).

The summary of ROBOCOPY will give you a reasonable size indicator in its 'Skipped' column.

Edit

Another really dirty one-liner, this time scanning all possible drive letters on a range of servers:

FOR %S IN (SERVER1 SERVER2 SERVER3) DO @FOR %D IN (A$ B$ C$ D$ E$ F$ G$ H$ I$ J$ K$ L$ M$ N$ O$ P$ Q$ R$ S$ T$ U$ V$ W$ X$ Y$ Z$) DO @IF EXIST "\\%~S\%~D\$RECYCLE.BIN" ECHO. & ECHO. & ECHO \\%~S\%~D\$RECYCLE.BIN & ROBOCOPY /l /s /nfl /fp /np /ndl /nfl /njh /r:0 "\\%~S\%~D\$RECYCLE.BIN" "\\%~S\%~D\$RECYCLE.BIN"

Of course, if gets to a point where such one-liners become unmaintanable so, in a batch file scanbins.cmd:

@ECHO OFF

VERIFY OTHER 2>nul
SETLOCAL ENABLEEXTENSIONS
IF ERRORLEVEL 1 ECHO Unable to enable extensions & GOTO :loop_end

SET ROBO="%WINDIR%\system32\robocopy.exe" /l /s /nfl /fp /np /ndl /nfl /njh /r:0
SET FIND="%WINDIR%\system32\find.exe" /i
SET DISKS=A$ B$ C$ D$ E$ F$ G$ H$ I$ J$ K$ L$ M$ N$ O$ P$ Q$ R$ S$ T$ U$ V$ W$ X$ Y$ Z$

:loop_start
IF "{%1}"=="{}" GOTO :loop_end

FOR %%D IN (%DISKS%) DO (
  SET BIN_PATH=\\%~1\%%~D\$RECYCLE.BIN

  IF EXIST "!BIN_PATH!\.\*" (
    FOR /f "useback tokens=*" %%F IN (`dir /a:d /b "!BIN_PATH!"`) DO (
      SET USER_BIN_PATH=!BIN_PATH!\%%~nxF
      IF EXIST "!USER_BIN_PATH!\.\*" (
        ECHO.
        ECHO !USER_BIN_PATH!
        %ROBO% !USER_BIN_PATH! !USER_BIN_PATH! | %FIND% " : " | %FIND% /v "Ended" | %FIND% /v "Times"
      )
    )
  )
)

SHIFT
GOTO :loop_start

:loop_end
ENDLOCAL
GOTO :EOF

...which you can call as scanbins.cmd SERVER1 SERVER2 SERVER3

jimbobmcgee
  • 2,645
  • 4
  • 24
  • 40