Copy each selected file to a subfolder named as the file

Discuss and share scripts and script files...
Post Reply
ale
Posts: 127
Joined: 19 Feb 2009 23:23
Location: Italy

Copy each selected file to a subfolder named as the file

Post by ale »

I wrote a couple one line scripts before, but I took inspiration from this question so I tried to write my first script. I'm not a programmer so it was not very easy :) and I understand the result may not be very elegant, I think it probably looks like some pieces assembled together. I gave a look for example to the scripts jacky writes, they are elegant because concise and use regular expressions a lot but they are simply too advanced for me right now, I can't follow them very well. I wanted to write something on my own and it was for me a learning experience and a way to spend some free time in the last couple of days. Variable names were in italian first but I tried to change them to english, with more time I could find better names but I think at some point you have to take a break and show the code to somebody. So I thought to show what I wrote so far, I hope there are not too many conceptual mistakes or bugs. Anything you feel to say as feedbeck to explain what can be improved or what is wrong, is appreciated. Thanks a lot to jacky for the wiki and to Don for help file, it would be impossible to write without them. Ok ok, now the code...

Code: Select all

/* Script to copy each selected file to a new folder named after the file.
Example: if file name is "hello.ext", a folder called "hello" is created then
the file is copied inside the folder. Name collisions will hopefully be handled
from the "new" scripting command wich is used.
Tested with XYplorer v7.90.0120 and v7.90.0125 beta */

// We collect the informations needed to operate
"start"
	setting "AllowRecursion";
	global $counter;
	global $items_number;
	global $items_data;
	$counter = 0;
	$items_number = getinfo(CountSelected);
	$items_data = report({Name}|{Ext}|{Fullname}|{Dir 1|2|3}<crlf>, 1);
	sub "_emptyselection_check";

// If selection is empty, the script will end
"_emptyselection_check"
	global $items_number;
	$emptyselection_truth = ("$items_number" == "0") ? "_emptyselection_exit" : "_main_check";
	sub "$emptyselection_truth";

// We need to loop until there are items to be processed then end execution
"_main_check"
	global $counter;
	global $items_number;
	incr $counter;
	status "Processing data...", , progress;
	$more_items_check = ("$counter" <= "$items_number") ? "_go_on" : "_done_exit";
	sub "$more_items_check";

// Check to see if the item is a file and need to be processed or a folder and
// thus need to be skipped
"_go_on"
	global $counter;
	global $items_data;
	global $item_being_processed;
	$item_being_processed = gettoken("$items_data", $counter, "<crlf>");
	$file_check = gettoken("$item_being_processed", 4, "|");
	$file_check_truth = ("$file_check" != "2") ? "_main_check" : "_file_action";
	sub "$file_check_truth";

// A file may not have an extension. If present we'll drop it later, if not the
// file can be processed as it is
"_file_action"
	global $item_being_processed;
	global $file_name;
	global $file_ext;
	global $file_base;
	$file_name = gettoken("$item_being_processed", 1, "|");
	$file_ext = gettoken("$item_being_processed", 2, "|");
	$file_base = "$file_name";
	$file_ext_truth = ("$file_ext" != "") ? "_drop_extension" : "_create_dir";
	sub "$file_ext_truth";

// We drop the extension because if file name is "hello.ext", we would like to
// simply call the folder "hello"
"_drop_extension"
	global $file_name;
	global $file_ext;
	global $file_base;
	strlen $file_name_lenght, "$file_name";
	strlen $file_ext_lenght, "$file_ext";
	substr $file_base, "$file_name", 0, ($file_name_lenght - $file_ext_lenght - 1);
	sub "_create_dir";

// Finally a folder can be created
"_create_dir"
	global $file_base;
	global $created_dir;
	new "$file_base", dir;
	$created_dir = "<curname>";
	sub "_copy_file";

// Let's copy the file
"_copy_file"
	global $item_being_processed;
	global $created_dir;
	$file_fullname = gettoken("$item_being_processed", 3, "|");
	copyto "$created_dir", "$file_fullname";
	sub "_main_check";

// End execution because selection was empty
"_emptyselection_exit"
	status "Please select one or more files", , stop;

// End execution
"_done_exit"
	status "Done";

kiwichick
Posts: 277
Joined: 08 Aug 2012 04:14

Re: Copy each selected file to a subfolder named as the file

Post by kiwichick »

I, too, was inspired by that script. I've modified it so that it works on multiple selections as this one is intended to do.

http://www.xyplorer.com/xyfc/viewtopic. ... 578#p77578

Enjoy!

Post Reply