Page 1 of 1
Scripting Newbie
Posted: 12 May 2014 04:25
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!
Re: Scripting Newbie
Posted: 12 May 2014 14:08
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)
Delete with recursion
Posted: 07 Nov 2014 21:03
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.
Re: Scripting Newbie
Posted: 07 Nov 2014 21:17
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

Re: Scripting Newbie
Posted: 10 Nov 2014 12:40
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.
Re: Scripting Newbie
Posted: 10 Nov 2014 13:05
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...