mIRC Homepage
Posted By: Dr_Brom_sung_ Delete empty folders - 22/02/05 10:12 AM
I want to build a script which delete empty folders, but not just empty folders.... I want it to delete folders which doesn't have any file in it.
I mean that folder A can have filder B inside and folder B can have folder C inside. And when I call the alias to delete filder A, it will delete it because there are no files in either directories, but just empty directories.

sorry for my bad english
Posted By: qwerty Re: Delete empty folders - 22/02/05 10:44 AM
Try this:
Code:
[color:green]; Syntax: $delempty(folder path)
; or /delempty <folder path>
; returns 1 if it successfully deleted an 'empty' folder,
; otherwise $null[/color]
alias delempty {
  if !$isdir($1-) || $findfile($1-,*,1) { return }
  var %a = delempty $+ $ticks
  .comopen %a Scripting.FileSystemObject
  if $comerr { return }
  .comclose %a $com(%a,DeleteFolder,1,bstr*,$gettok($remove($1-,"),1-,92))
  if !$isdir($1-) { return 1 }
}
Posted By: Dr_Brom_sung_ Re: Delete empty folders - 22/02/05 10:58 AM
Nice one shocked shocked... thanks grin

I wish I knew how to work with .comopen etc... I tried it once but it is too complicated.

Thanks again.
Posted By: Dr_Brom_sung_ Re: Delete empty folders - 22/02/05 01:09 PM
I have another question....

If I have a directory A.
This dirrectory has dirrectories B, C and D which all of them might be emty.

I don't want to delete A, but only the empty foders inside A. (If B and D are empty, then it will delete B and D but not C).

Is there an easy way (beside loops) to do that?
If I need to use loops, how exactly I retrieve the names of the directories inside A using mIRC?

Thanks.
Posted By: qwerty Re: Delete empty folders - 22/02/05 10:55 PM
No, there is no way to do it other than with loops. To list all subfolders in a dir, you use $finddir (look it up in the help file). It is important to start deleting folders from the bottom, ie from the 'deepest' folder in the tree, working your way up. A simple way to do this is to sort the list of folders. This way, for example, the folder
C:\program files\mirc\blah
will always come after
C:\program files\mirc
in a folder list sorted alphabetically. So, basically, sorting the folder list makes sure that the deepest folders are going to be at the bottom of the list. The following alias does exactly that: $finddir() loads the list of folders in the sorted (/window -s) window @@. Then we loop backwards, starting from the bottom line of that window and executing /rmdir on each line. As you already know, /rmdir can only delete empty folders; if the folder isn't empty, /rmdir reports an error and the script halts. Using the error handling system in mirc, we can prevent that: when an error is encountered (that is, when a folder cannot be deleted because it's not empty), we silence it and just let the script move on to the next folder.
Code:
alias delempty2 {
  if !$isdir($1-) { return }
  close -@ @@
  window -hls @@
  var %i = $finddir($1-,*,0,@@).shortfn
  while %i {
    rmdir-alias $line(@@,%i)
    dec %i
  }
  close -@ @@
}
alias rmdir-alias {
  .rmdir $1-
  return
  :error
  reseterror
}

The syntax is
/delempty2 <path to folder>
Posted By: Dr_Brom_sung_ Re: Delete empty folders - 23/02/05 08:34 PM
thanks for your help.
Posted By: saragani Re: Delete empty folders - 24/02/05 07:18 AM
Wouldn't it be easier to combine the 2 methodes:



Code:
 
alias delempty1 {
  var %b = $finddir($1-,*,0,1)
  while %b {
    delempty2 $finddir($1-,*,%b,1) 
    dec %b
  }
}

alias delempty2 {
  if !$isdir($1-) || $findfile($1-,*,1) { return }
  var %a = delempty $+ $1-
  .comopen %a Scripting.FileSystemObject
  if $comerr { return }
  .comclose %a $com(%a,DeleteFolder,1,bstr*,$gettok($remove($1-,"),1-,92))
  if !$isdir($1-) { return 1 }
}
 

This should work I think smile
© mIRC Discussion Forums