mIRC Home    About    Download    Register    News    Help

Print Thread
Delete empty folders #112252 22/02/05 10:12 AM
Joined: Nov 2004
Posts: 148
D
Dr_Brom_sung_ Offline OP
Vogon poet
OP Offline
Vogon poet
D
Joined: Nov 2004
Posts: 148
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

Re: Delete empty folders #112253 22/02/05 10:44 AM
Joined: Jan 2003
Posts: 2,523
Q
qwerty Offline
Hoopy frood
Offline
Hoopy frood
Q
Joined: Jan 2003
Posts: 2,523
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 }
}

Last edited by qwerty; 22/02/05 10:48 AM.

/.timerQ 1 0 echo /.timerQ 1 0 $timer(Q).com
Re: Delete empty folders #112254 22/02/05 10:58 AM
Joined: Nov 2004
Posts: 148
D
Dr_Brom_sung_ Offline OP
Vogon poet
OP Offline
Vogon poet
D
Joined: Nov 2004
Posts: 148
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.

Last edited by Dr_Brom_sung_; 22/02/05 11:04 AM.
Re: Delete empty folders #112255 22/02/05 01:09 PM
Joined: Nov 2004
Posts: 148
D
Dr_Brom_sung_ Offline OP
Vogon poet
OP Offline
Vogon poet
D
Joined: Nov 2004
Posts: 148
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.

Re: Delete empty folders #112256 22/02/05 10:55 PM
Joined: Jan 2003
Posts: 2,523
Q
qwerty Offline
Hoopy frood
Offline
Hoopy frood
Q
Joined: Jan 2003
Posts: 2,523
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>


/.timerQ 1 0 echo /.timerQ 1 0 $timer(Q).com
Re: Delete empty folders #112257 23/02/05 08:34 PM
Joined: Nov 2004
Posts: 148
D
Dr_Brom_sung_ Offline OP
Vogon poet
OP Offline
Vogon poet
D
Joined: Nov 2004
Posts: 148
thanks for your help.

Re: Delete empty folders #112258 24/02/05 07:18 AM
Joined: Feb 2003
Posts: 282
S
saragani Offline
Fjord artisan
Offline
Fjord artisan
S
Joined: Feb 2003
Posts: 282
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