mIRC Homepage
Posted By: Joveice Delete folder with files - 02/08/15 01:46 AM
How do i delete all files in a folder and the folder?
rmdir stops becouse there is files in the folder.
Posted By: Joveice Re: Delete folder with files - 09/08/15 11:31 PM
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.
Posted By: Brax Re: Delete folder with files - 10/08/15 02:54 AM
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
Posted By: Wims Re: Delete folder with files - 10/08/15 03:01 AM
@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.
Posted By: Brax Re: Delete folder with files - 10/08/15 07:34 AM
Recursion or not, it is still one way to do it. wink

But then again... all roads are supposed to lead to Rome.
Posted By: Joveice Re: Delete folder with files - 10/08/15 11:42 AM
the folder contains random files each time :P
Posted By: Wims Re: Delete folder with files - 10/08/15 02:21 PM
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
Posted By: Joveice Re: Delete folder with files - 10/08/15 03:25 PM
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
Posted By: Wims Re: Delete folder with files - 10/08/15 04:18 PM
Yeah, did you try the code I gave?
Posted By: Joveice Re: Delete folder with files - 10/08/15 08:03 PM
not yet, will when im back at the home server
Posted By: Brax Re: Delete folder with files - 11/08/15 12:35 AM
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?
Posted By: Wims Re: Delete folder with files - 11/08/15 01:17 AM
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.
Posted By: Brax Re: Delete folder with files - 11/08/15 03:00 AM
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.
Posted By: Wims Re: Delete folder with files - 11/08/15 01:50 PM
Yeah, it always seems easier when you don't actually try it laugh
Posted By: Plornt Re: Delete folder with files - 14/08/15 11:01 PM
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.).
Posted By: Wims Re: Delete folder with files - 15/08/15 12:35 AM
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.
Posted By: Plornt Re: Delete folder with files - 15/08/15 10:11 AM
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.
Posted By: Wims Re: Delete folder with files - 15/08/15 11:05 AM
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.
Posted By: Khaled Re: Delete folder with files - 24/08/15 09:29 AM
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.
Posted By: Wims Re: Delete folder with files - 24/08/15 12:52 PM
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?
© mIRC Discussion Forums