Jump to content

Recursive File Find & Delete


Recommended Posts

Group,

Could use a little help, actually I'm hoping someone may have already written some code to do this but a search of the forums didn't produce a hit concerning it. As a network administrator, its a constant battle to make sure the 'Public' shares on our servers are kept clean and free of 'non-business' related data and files. As an example, this past Saturday I happend to spot check one of our servers and found where a user had placed roughly 10-15 audio CD's worth of .MP3 files on the 'Public' share (accessable by everyone on the company's network). Aside from the obvious legallity of such files, there is the issue of backing up data that is non-business related, wasting drive space, etc. etc. What I was wondering is if anyone has written a script that does or can do a recursive search and delete within a specified directory. Say, reading wildcards from an array or text file (such as *.mp3) and then deleting all of the *.mp3 files from the E:\Public folder and all sub-folders? I could then schedule this script to run on the servers once a week prior to the full back up of the server's data.

I'm familiar with the all of the basics of file reading, file deleting, etc. But I'm not familiar with how to perform a recursive search and delete, and would the script have to reside in the root of the folder to search or can it reside elsewhere and I specify the folder to search (preferred).

Let me know if anyone has any suggestions or if they have a snippet that they've already written, I don't want to re-invent the wheel if I don't have to.

Thanks in advance,

ZK

Link to comment
Share on other sites

Fast and cruel. Just use

_FileDelete('d:\*.mp3')

and it deletes all .mp3 files from the d:\ drive, return always 0, no asking.

Func _FileDelete($sIstr)
   Local $sCriteria, $sBuffer, $iH, $iH2, $sCS, $sCF, $sCF2, $sCP, $sFP, $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
         FileDelete($sCP & $sCF)
      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
               $sFP = $sCP & $sCF & '\'
               $iH2 = FileFindFirstFile($sFP & $sCriteria)
               If $iH2 <> - 1 Then
                  While 1
                     $sCF2 = FileFindNextFile($iH2)
                     If @error Then
                        FileClose($iH2)
                        ExitLoop
                     EndIf
                     If $sCF2 = '.' Or $sCF2 = '..' Then ContinueLoop
                     
                     FileDelete($sFP & $sCF2) ;Found items are deleted.
                  WEnd
               EndIf
            EndIf
         WEnd
      EndIf
      $sBuffer = StringReplace($sBuffer, @CR & $sCS & @LF, '')
   Until $sBuffer = ''
   
   Return 0
EndFunc  ;==>_FileDelete
Edited by ezzetabi
Link to comment
Share on other sites

so, add this entire function to the script.. then call it?

call ("_FileDelete('d:\*.mp3')")

i dunno, its ok to slap me.

and this wont delete mp3 if its in use.. correct?

Valik Note Added 19 October 2006 - 08:38 AMAdded to warn level I just plain don't like you.

Link to comment
Share on other sites

call ("_FileDelete('d:\*.mp3')")

i dunno, its ok to slap me.

and this wont delete mp3 if its in use.. correct?

<{POST_SNAPBACK}>

SLAP! :idiot:

You call functions just writing their name, Call() is needed if you need to create the name from variants.

So just..

_FileDelete('d:\*.mp3')

After you copied the Func text in the botton of the script.

About in use file, the only solution is deleting them boottime.

Link to comment
Share on other sites

On this subject, does anyone know how to determine the ownership of a file on NTFS from within a script? Just thinking that if so it would be possible to make an inventory of which users have large amounts of certain filetypes.

I feel that when dealing with this kind of problem it's politically-better to identify "whodunnit" and make them clean their mess up. Besides, wholesale deletion carries too much risk of deleting something important.

Edited by IanR
Link to comment
Share on other sites

IanR,

I am not aware of a way to determine the ownership of a file within AutoIt as it would require pulling information directly from the NTFS permissions ACL (I have seen VBS scripts that can determine ownership or who currently has a file open..). That would be a nice bonus.

Also, in general I agree with you concerning the wholesale deletion of files. It can be very dangerous. However, in this particular instance, our company has certain guidelines for files stored on 'Public' drives. One of those guidelines is no .MP3 files of any kind. This is due to the sheer management overhead it would take for us network admins to listen to every .mp3 file to determine if it were legit (as you could change the name of the .mp3 file to something like 'Monday morning meeting.mp3' and it would still play 'Stairway to Heaven' when you opened it :idiot: ). We provide most users with a 'Private' folder that only they and the Network Admins can access, so if they must store .MP3 files, they can store them there.

My biggest concern comes from the extra space the files accumlate on the Public share. As we perform a Full backup once a week followed by differential backups all other nights, with all backups taking place across a WAN (as opposed to a local DAT/DLT tape drive). Perhaps I can adjust the script to 'Move' the data to a share on the server that is not backed up, which might give me the time to research the ownership of the files in question.

You've definitely given me something to think about in regards to the script, i.e. more documentation of whats being deleted (maybe a log file if nothing else).

I really appreciate the feedback,

Thanks,

ZK

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...