Scripting Newbie

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
Post Reply
davidonabus
Posts: 4
Joined: 12 May 2014 04:21

Scripting Newbie

Post by davidonabus »

Hello Folks,

I'm interested in learning some scripting within XYplorer. I thought I'd start small.

I'd like to, recursively, from the folder that is currently selected delete all folders that:
contain __worthless__.xml and no other file
are empty

Any help would be appreciated!

bdeshi
Posts: 4256
Joined: 12 Mar 2014 17:27
Location: Asteroid B-612
Contact:

Re: Scripting Newbie

Post by bdeshi »

Welcome!
This script should do the trick.

Code: Select all

 // begin if a folder is selected, exists() retunrs 2 for folder
 if (exists(<curitem>) == 2) {
  $list = trim(folderreport(dirs, r, <curitem>, r, , '|'), '|'); //create |-separated list of items in selected folder
  $count = gettoken("$list", 'count', '|'); //count number of items
 
  $counter = 1; $hitlist = '';
  //invert itemlist, this makes recursive deletion easier because deeper folders are listed earlier
  while ($counter <= $count) {
   $hitlist = "$hitlist".'|'.gettoken($list, -$counter,'|'); $counter++;
  }
  $hitlist = trim($hitlist,'|'); // trim an extra pipe char (|) at the end
 
  // commence fire, I mean deletion
  foreach($it, $hitlist, '|') {
   // deletes __worthless__.xml
   // according to post, this makes the parent folder empty
   if exists("$it\__worthless__.xml") {
    delete 0, 0, "$it\__worthless__.xml"; // deletes forever, because they're apparently worthless
   }
   // delete empty folders
   // includes folders containing __worthless__.xml because they're empty now
   if (listfolder("$it", ,32) == "") { // here, 32 means return count of items
    delete 1, 0, "$it"; // deletes to recycle bin
   }
  }
  msg "Done!";
 } else { msg "select a folder first."; }
 
Select a target folder and then run this script (you know how, right?)
Look up the functions/commands used here in XY help file for in depth undestanding.

How it works: basically, creates a recursive item list of the selected folder.
Then deletes __worthless__.xml, now these folders become empty.
Then deletes all empty folders (which includes folders made empty in above step)
Icon Names | Onyx | Undocumented Commands | xypcre
[ this user is asleep ]

Papoulka
Posts: 455
Joined: 13 Jul 2013 23:41

Delete with recursion

Post by Papoulka »

In case you weren't thanked for this then... Thank You, Sammay. I needed a way to search through subfolders and recursively delete files named "junk.txt". This fits the bill exactly.

I am surprised that this is so hard (meaning complicated) to achieve. I kept hoping to find something in the "Delete" command that would do it more directly, such as "Delete .\*\junk.txt". That would be nice.

highend
Posts: 14588
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Scripting Newbie

Post by highend »

It isn't so "hard".

Code: Select all

    $delete = input("Enter the file name to delete...", , , , , 400, 250);
    if ($delete) { delete 1, 0, listpane(, $delete, 1+8+16); }
Switch to branch view and execute that command. Enter your file name (without path). Done.

Branch view because listpane() is so much faster than folderreport :)
One of my scripts helped you out? Please donate via Paypal

Papoulka
Posts: 455
Joined: 13 Jul 2013 23:41

Re: Scripting Newbie

Post by Papoulka »

But I need something that will work any time, and can be part of a script that's doing other things as well.

highend
Posts: 14588
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Scripting Newbie

Post by highend »

But I need something that will work any time, and can be part of a script that's doing other things as well.
I which case does it not work when being part of another script?

Switching to branch view? There is a # command as well. The long way? Replace the listpane with a folderreport...
One of my scripts helped you out? Please donate via Paypal

Post Reply