mIRC Home    About    Download    Register    News    Help

Print Thread
#254296 02/08/15 01:46 AM
Joined: Jul 2015
Posts: 42
J
Joveice Offline OP
Ameglian cow
OP Offline
Ameglian cow
J
Joined: Jul 2015
Posts: 42
How do i delete all files in a folder and the folder?
rmdir stops becouse there is files in the folder.

Joined: Jul 2015
Posts: 42
J
Joveice Offline OP
Ameglian cow
OP Offline
Ameglian cow
J
Joined: Jul 2015
Posts: 42
Bump
Anyone Please?
etc I need to delete C:\Folder\Folder2
Folder2 has random files in it
and with this command it deleted Folder2 and everything in it
rmdir stops becouse there is files in the folder.

Joined: Jan 2005
Posts: 192
Vogon poet
Offline
Vogon poet
Joined: Jan 2005
Posts: 192
First delete the files in the folder and then folder.

To delete the file you have command
/remove [-b] <filename>

To get the filenames in directory you can use $findfile
For more info see
/help $findfile
/help File and Directory Identifiers


echo -a $signature
Joined: Jul 2006
Posts: 4,145
W
Hoopy frood
Offline
Hoopy frood
W
Joined: Jul 2006
Posts: 4,145
@Brax: it can be tricky to delete all files of all the sub folder of a directory, you would need recursion on $findfile to do it.

Here is a nice COM object handling this for you:

Code:
alias delete_fullfolder {
.comopen a Scripting.FileSystemObject
if ($com(a,GetFolder,1,bstr*,$1-,dispatch* b)) && (!$comerr) noop $com(b,Delete,1)
if ($com(a)) .comclose a
if ($com(b)) .comclose b
}
/delete_fullfolder <path> -- do not use quote.


#mircscripting @ irc.swiftirc.net == the best mIRC help channel
Joined: Jan 2005
Posts: 192
Vogon poet
Offline
Vogon poet
Joined: Jan 2005
Posts: 192
Recursion or not, it is still one way to do it. wink

But then again... all roads are supposed to lead to Rome.


echo -a $signature
Joined: Jul 2015
Posts: 42
J
Joveice Offline OP
Ameglian cow
OP Offline
Ameglian cow
J
Joined: Jul 2015
Posts: 42
the folder contains random files each time :P

Joined: Jul 2006
Posts: 4,145
W
Hoopy frood
Offline
Hoopy frood
W
Joined: Jul 2006
Posts: 4,145
Lol, not at all wink if recursion is involved you cannot do it in mIRC, or it would work for 100 files, but if you have sub folder with hundred of files it would never work


#mircscripting @ irc.swiftirc.net == the best mIRC help channel
Joined: Jul 2015
Posts: 42
J
Joveice Offline OP
Ameglian cow
OP Offline
Ameglian cow
J
Joined: Jul 2015
Posts: 42
the folder got 5 text documents with random name, and i need it to delete the whole folder and everything inside of it :P thats what i would like :P

Joined: Jul 2006
Posts: 4,145
W
Hoopy frood
Offline
Hoopy frood
W
Joined: Jul 2006
Posts: 4,145
Yeah, did you try the code I gave?


#mircscripting @ irc.swiftirc.net == the best mIRC help channel
Joined: Jul 2015
Posts: 42
J
Joveice Offline OP
Ameglian cow
OP Offline
Ameglian cow
J
Joined: Jul 2015
Posts: 42
not yet, will when im back at the home server

Joined: Jan 2005
Posts: 192
Vogon poet
Offline
Vogon poet
Joined: Jan 2005
Posts: 192
I havent tried but it got me curious.
Can you explain why it wouldnt work with subfolders with hundreds of files?

Least at first glance and without trying it doesnt look impossible. Why couldnt I just loop over all the folders and subfolders, delete the files in them and remove the folders afterwards?

Leaving possible performance issues aside, wouldnt it still work?


echo -a $signature
Joined: Jul 2006
Posts: 4,145
W
Hoopy frood
Offline
Hoopy frood
W
Joined: Jul 2006
Posts: 4,145
I somewhat explained when I said it was about recursion, but the real reason is probably a memory issue.

Well our function must remove all files in the folder, look for all subfolders, and remove all files in that sub folder, then look for all subsubfolder in that subfolder, etc etc, so the main function is actually just a function removing all files in the directory, then calling itself for all the subfolder, and then removing that directory (you can't do it before looking for all subfolders), this is recursion:

/remfolder main_folder :
Code:
alias remfolder {
;get all directory in folder $1-
noop $finddir($1-,*,0,remfolder $1-)
rmdir $1-
}
It may be possible to write a procedural script (no recursion) but good luck with that.
And mIRC don't like recursion much, testing using something like this to create a lot of subfolders (/testf):
Code:
alias testf {
  var %a
  mkdir main_folder
  var %b 20
  while (%b) {
    %a = 8
     %d = main_folder
    while (%a) {
      %d = %d $+ \folder $+ %a $+ %b 
      mkdir $qt(%d)
      dec %a
    }
    dec %b
  }
}
executing /remfolder main_folder after that, I get an error (I'm on the latest 7.42, not 7.43 but it doesn't matter much) on /rmdir on a folder, if you try right away to delete that folder, with the exact same /rmdir command, it will work, suggesting no memory is available to do the operation, or something, I don't really know.


#mircscripting @ irc.swiftirc.net == the best mIRC help channel
Joined: Jan 2005
Posts: 192
Vogon poet
Offline
Vogon poet
Joined: Jan 2005
Posts: 192
Okay. I will play around with it someday to see what I can come up with, if I have too much free time laugh

Just that from initial look it seemed like it shouldnt be too complicated to do with some long looping.


echo -a $signature
Joined: Jul 2006
Posts: 4,145
W
Hoopy frood
Offline
Hoopy frood
W
Joined: Jul 2006
Posts: 4,145
Yeah, it always seems easier when you don't actually try it laugh


#mircscripting @ irc.swiftirc.net == the best mIRC help channel
Joined: May 2010
Posts: 29
P
Ameglian cow
Offline
Ameglian cow
P
Joined: May 2010
Posts: 29
Not tried it, but how about just invoking the systems delete with "/run"?


For example:

Code:
//run cmd.exe /c del /S /F /Q $mircdirtest


Just tested, it works great... So great I just accidentally deleted my mIRC directory haha (Oh god. Why wont recuva run.).

Last edited by Plornt; 14/08/15 11:19 PM.
Joined: Jul 2006
Posts: 4,145
W
Hoopy frood
Offline
Hoopy frood
W
Joined: Jul 2006
Posts: 4,145
That works but /run is not going to wait for it to be done, mIRC will keep processing your code, just like with timer, being able to know when it's done might be just super useful.


#mircscripting @ irc.swiftirc.net == the best mIRC help channel
Joined: May 2010
Posts: 29
P
Ameglian cow
Offline
Ameglian cow
P
Joined: May 2010
Posts: 29
Originally Posted By: Wims
That works but /run is not going to wait for it to be done, mIRC will keep processing your code, just like with timer, being able to know when it's done might be just super useful.


Ok true... So providing you are deleting the folder just to gain back the ability to write/read from it without clashing with the previous data you just deleted and are not wanting to know when its done to display some sort of progress. Couldnt you rename the directory before hand, and then use that command to delete? That way you can create a new directory with the old name as soon as the rename is done.

Probably a bit hacky but depending on what they are trying to do, it should work.

Joined: Jul 2006
Posts: 4,145
W
Hoopy frood
Offline
Hoopy frood
W
Joined: Jul 2006
Posts: 4,145
It does work, but practically speaking nobody does/need that, I think you just made up a very weird case scenario where it could be used INSTEAD OF the com object, which could still be used and I'm certain would be prefered because it's more predictable, if you are making a script for it, you want it to be predictable, otherwise, you use explorer to delete a folder.


#mircscripting @ irc.swiftirc.net == the best mIRC help channel
Joined: Dec 2002
Posts: 5,411
Hoopy frood
Offline
Hoopy frood
Joined: Dec 2002
Posts: 5,411
The script below is designed to mimic the way /rmdir behaves but will delete the folder and its contents:

Code:
rmdirex {
  if ($len($1-) == 0) {
    echo 2 -e * /rmdirex: insufficient parameters
    return
  }

  var %dir = $mircdir $+ $1-
  if (!$isdir(%dir)) return

  window -sh @rmdirex
  aline @rmdirex %dir
  noop $findfile(%dir, *.*, 0, aline @rmdirex $1-)
  noop $finddir(%dir, *.*, 0, aline @rmdirex $1-)

  var %n = $line(@rmdirex, 0)
  while (%n > 0) {
    var %fn = $line(@rmdirex, %n)
    if ($isdir(%fn)) .rmdir %fn
    else .remove -b %fn
    dec %n
  }

  window -c @rmdirex
  return

  :error
  reseterror
  window -c @rmdirex
  echo 2 -e * /rmdirex: unable to remove ' $+ %dir $+ '
}

By using /window -s to sort the list, files and folders are listed after their containing folders, so all the script needs to do is go backwards through the list. Note that it uses /remove -b to move deleted files to the recycle bin.

Joined: Jul 2006
Posts: 4,145
W
Hoopy frood
Offline
Hoopy frood
W
Joined: Jul 2006
Posts: 4,145
Nice script!
Though, like I said, it's doable with a procedural script, but now you're using a while loop to remove everything, on huge folder, this would be much slower than the COM object script.
What do you think about adding a new 'f' switch to /rmdir to include this feature, removing all the files and subfolders from the folder?


#mircscripting @ irc.swiftirc.net == the best mIRC help channel

Link Copied to Clipboard