Jump to content

Delete Everything But...


Recommended Posts

I have the great job at the end of the school year of clearing out all the stuff from users home drives. Now I have created a folder called 'keep me' on all users home drives which they have been informed to place any data in they want to keep.

Can someone suggest some code as to how I can delete everything else from the share accept the 'keep me' folder ?

Something along the lines of:

If FileExists '%share%\keep me'

do not delete !!

else dirremove everything.

I cannot think how I would go about telling it not to remove everything in the keep me folder. The only way I could think of doing this is to copy the keep me folder over to a temp location first, then running the dirremove, then copying back the keep me folder.

The share names I have in a text file so I could read each one is a variable and get the script to go down each one in turn. Anyway if someone has some suggestions then please let me know !!

Link to comment
Share on other sites

Got access to another part of the same "drive" or share or another server?

Copy the "keep me" folder to a temp place somewhere else

Verify the copy

Delete all from the user's share

Copy the "keep me" back to the user's share

Verify the copy

Delete the temp copy

Repeat for each user

This approach might not be too kind to the network depending on the location of the temp copy and the size of the files in each user's "keep me" folder... but the network load should be down with school out.

Edit: the problem with coding a solution to this is that the user might chage the name of the "keep me" folder ever so slightly. If you are confident that they have not (or cannot) or if you are willing to punish those that do - then serach the forum for "recursive" and see other's code

Edited by herewasplato

[size="1"][font="Arial"].[u].[/u][/font][/size]

Link to comment
Share on other sites

Got access to another part of the same "drive" or share or another server?

Copy the "keep me" folder to a temp place somewhere else

Verify the copy

Delete all from the user's share

Copy the "keep me" back to the user's share

Verify the copy

Delete the temp copy

Repeat for each user

This approach might not be too kind to the network depending on the location of the temp copy and the size of the files in each user's "keep me" folder... but the network load should be down with school out.

Edit: the problem with coding a solution to this is that the user might chage the name of the "keep me" folder ever so slightly. If you are confident that they have not (or cannot) or if you are willing to punish those that do - then serach the forum for "recursive" and see other's code

<{POST_SNAPBACK}>

i had to do something similar at work, where i had to remove alot of files that fit specific criteria. you may want to read in the helpfile for the filefindfirstfile and filefindnextfile functions... and because of the chance for data loss, i'd encourage you to backup your 'keep me' folders prior to testing any solution you work out for the first time.....

Link to comment
Share on other sites

I have full system access to the server as it was me who set it all up to start with !!

The folder keep me is the same as I wrote a batch for that which created one on every single user share.

What you have stated is something I thought was a way round - messy but I think do-able.

I'll start work on it next week.

Just a thought but I wonder if you could do it via a date difference ? The date on the keep me folder will be the same as that was written on every share via a batch file i wrote....

Delete everything under this date / time.....

Is there a date compare function ?? I'll take a look !!

Got access to another part of the same "drive" or share or another server?

Copy the "keep me" folder to a temp place somewhere else

Verify the copy

Delete all from the user's share

Copy the "keep me" back to the user's share

Verify the copy

Delete the temp copy

Repeat for each user

This approach might not be too kind to the network depending on the location of the temp copy and the size of the files in each user's "keep me" folder... but the network load should be down with school out.

Edit: the problem with coding a solution to this is that the user might chage the name of the "keep me" folder ever so slightly. If you are confident that they have not (or cannot) or if you are willing to punish those that do - then serach the forum for "recursive" and see other's code

<{POST_SNAPBACK}>

Link to comment
Share on other sites

Here it is, sound lenghty but actually most of it is only support functions.

Take the list of files, order it from longest filename to shortest (a shorter filename can't be in a subfolder of a longer... :) ), pass the list and delete files or folders if they do not match the criteria.

$sPath = 'D:\test';<- \\Folder\\ where delete
$sException = 'Ezz';it is meant as a string checked in the whole filename, in your case '\keep me\'

If StringRight($sPath, 1) <> '\' Then $sPath = $sPath & '\'
$aFiles = _FileSearch($sPath)
$m = ''
If $aFiles[0] > 0 Then
   _Quicksort($aFiles, 1, $aFiles[0])
   For $c = 1 to $aFiles[0]
      If Not StringInStr($aFiles[$c], $sException) Then
         If StringInStr(FileGetAttrib($aFiles[$c]), 'd') Then
            DirRemove($aFiles[$c]);It wont delete folder if any file that matched the criteria is still inside.
         Else
            FileDelete($aFiles[$c])
         EndIf            
      EndIf
   Next
EndIf

Exit
;The program ends here, below only support functions.



Func _FileSearch($sIstr)
  ; An array is returned with the full path of all files found. The pos [0] keeps the number of elements.
   Local $sCriteria, $sBuffer, $iH, $iH2, $sCS, $sCF, $sCF2, $sCP, $sFP, $sOutPut = '', $aNull[1]
   $sCP = StringLeft($sIstr, StringInStr($sIstr, '\', 0, -1))
   
   If $sCP = '' Then $sCP = @WorkingDir & '\'
   $sCriteria = StringTrimLeft($sIstr, StringInStr($sIstr, '\', 0, -1))
   If $sCriteria = '' Then $sCriteria = '*.*'
   
  ;To begin we seek in the starting path.
   $sCS = FileFindFirstFile($sCP & $sCriteria)
   If $sCS <> - 1 Then
      While 1
         $sCF = FileFindNextFile($sCS)
         If @error Then
            FileClose($sCS)
            ExitLoop
         EndIf
         If $sCF = '.' Or $sCF = '..' Then ContinueLoop
         
         $sOutPut = $sOutPut & $sCP & $sCF & @LF
      WEnd
   EndIf
   
   $sBuffer = @CR & $sCP & '*' & @LF;The buffer is set for keeping the given path plus a *.
   Do
      $sCS = StringTrimLeft(StringLeft($sBuffer, StringInStr($sBuffer, @LF, 0, 1) - 1), 1);current search.
      $sCP = StringLeft($sCS, StringInStr($sCS, '\', 0, -1));Current search path.
      $iH = FileFindFirstFile($sCS)
      If $iH <> - 1 Then
         While 1
            $sCF = FileFindNextFile($iH)
            If @error Then
               FileClose($iH)
               ExitLoop
            EndIf
            If $sCF = '.' Or $sCF = '..' Then ContinueLoop
            
            If StringInStr(FileGetAttrib($sCP & $sCF), 'd') Then
               $sBuffer = @CR & $sCP & $sCF & '\*' & @LF & $sBuffer; Every folder found is added in the begin of buffer
               $sFP = $sCP & $sCF & '\';                                for future search
               $iH2 = FileFindFirstFile($sFP & $sCriteria);          and checked with the criteria.
               If $iH2 <> - 1 Then
                  While 1
                     $sCF2 = FileFindNextFile($iH2)
                     If @error Then
                        FileClose($iH2)
                        ExitLoop
                     EndIf
                     If $sCF2 = '.' Or $sCF2 = '..' Then ContinueLoop
                     
                     $sOutPut = $sOutPut & $sFP & $sCF2 & @LF;Found items are put in the Output.
                  WEnd
               EndIf
            EndIf
         WEnd
      EndIf
      $sBuffer = StringReplace($sBuffer, @CR & $sCS & @LF, '')
   Until $sBuffer = ''
   
   If $sOutPut = '' Then
      $aNull[0] = 0
      Return $aNull
   Else
      Return StringSplit(StringTrimRight($sOutPut, 1), @LF)
   EndIf
EndFunc  ;==>_FileSearch

Func _Quicksort(ByRef $a, $p, $r)
   Local $j, $q
   $j = int(log($r - $p) / log(2)) * 2 + 4
   Local $stk[$j], $ls = 0
   
   While 1
      While $p < $r
         $q = $p - 1
         For $j = $p To $r - 1
            If StringLen($a[$j]) >= StringLen($a[$r]) Then
               $q = $q + 1
               __qs_swap($a[$q], $a[$j])
            EndIf
         Next
         $q = $q + 1
         __qs_swap($a[$q], $a[$r])
         
         If $r - $q > $q - $p Then
            __qs_stk_push($q + 1, $stk, $ls)
            __qs_stk_push($r, $stk, $ls)
            $r = $q - 1
         Else
            __qs_stk_push($p, $stk, $ls)
            __qs_stk_push($q - 1, $stk, $ls)
            $p = $q + 1
         EndIf
      WEnd
      
      $r = __qs_stk_pop($stk, $ls)
      If @error Then ExitLoop
      $p = __qs_stk_pop($stk, $ls)
   WEnd
EndFunc  ;==>_Quicksort

Func __qs_swap(ByRef $st, ByRef $nd)
   Local $t = $nd
   $nd = $st
   $st = $t
EndFunc  ;==>__qs_swap

Func __qs_stk_pop(ByRef $stk, ByRef $ls)
   If $ls = 0 Then
      SetError(1)
      Return 0
   EndIf
   $ls = $ls - 1
   Return $stk[$ls]
EndFunc  ;==>__qs_stk_pop

Func __qs_stk_push($value, ByRef $stk, ByRef $ls)
   $stk[$ls] = $value
   $ls = $ls + 1
EndFunc  ;==>__qs_stk_push
Edited by ezzetabi
Link to comment
Share on other sites

$file = FileFindNextFile

If $file = "keep me" Then

ContinueLoop(?)

Else

FileDelete($file)

EndIf

Check that code on a test folder before anything else...

<{POST_SNAPBACK}>

If $file = . Then Continue Loop

If $file = .. Then Continue Loop

If $file = "keep me" Then Continue Loop

Link to comment
Share on other sites

If $file = . Then Continue Loop

If $file = .. Then Continue Loop

If $file = "keep me" Then Continue Loop

<{POST_SNAPBACK}>

Thanks for all the ideas, I like MSLx Fanboy/MarkMarkMarks one - I'll knock something up over the next few days and test it - on my test ID first of course... :) Edited by MattX
Link to comment
Share on other sites

you could do this with a BAT file:

FOR /F "tokens=*" %%U in (UserHomeFolderList.Txt) do (
CD /D "%%U"
FOR /F "tokens=*" %%D in ('DIR /AD /B ^| FIND "Keep Me" /v /i') do ECHO %%D
)

Just make UserHomeFolderList.txt contain a list (1 per line) of users home folders.

And when you're done "testing" it, change 'ECHO %%D' to 'RD /S /Q "%%D"', or use DELTREE, depending on your system.

Edited by blindwig
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...