Page 1 of 1
Tag from file/foldername bracket contents
Posted: 21 Aug 2012 00:16
by kiwichick
Hi there, Can I tag a file or a folder (and its contents) based on the bracket contents of the file/foldername?
For example, if the name is "Greatest Hits (album) photo sessions, Stockholm [1975]" - tag with 1975.
And, just for interest's sake, can the same be done for parentheses contents, but only the last instance?
For example, if the name is "Greatest Hits (album) photo sessions, Stockholm (1975)" - tag with 1975.
Could a script then move the tagged files and folders to a folder (in a predetermined location) that matches the tagname.
For example, tag with 1975 and move to "C:\ABBA\Timeline\1975"
Cheers for any help.
Re: Tag from file/foldername bracket contents
Posted: 21 Aug 2012 01:20
by highend
Sure...
Code: Select all
$a="Greatest Hits (album) photo sessions, Stockholm [1975]"; msg regexreplace($a, "(.*\[)(\d{2,})(.*)", "$2");
Returns 1975
Or for parenthesis:
Code: Select all
$a="Greatest Hits (at 1970) photo sessions, Stockholm (1975)"; msg regexreplace($a, "(.*\()(\d{2,})(.*)", "$2");
So look at the commands regexreplace + tag + moveto in the help file

Re: Tag from file/foldername bracket contents
Posted: 22 Aug 2012 09:54
by kiwichick
Thanks highend, Does the code assume the file/folder will be called "Greatest Hits (album) photo sessions, Stockholm [1975]"? Because then it would only work on that one folder - is that correct?
And thanks for the hint about the commands - I'll check it out.
Re: Tag from file/foldername bracket contents
Posted: 22 Aug 2012 10:06
by highend
No, it was just an example.
Both regexes search for the very last "[" character in a string (in this case, a file name) and return the following 2 up to "endless" digits after it.
If you always! use a year format of yyyy you could change {2,} to {4} (which would always return the next found 4 digits after the last "["), etc.
Re: Tag from file/foldername bracket contents
Posted: 22 Aug 2012 11:03
by kiwichick
Thanks again highend, I'm probably doing something wrong but it's not working for me. I've copied your code and run the script - a window pops up that says 1975 but when I click OK nothing is tagged. And it says 1975 regardless of what year is in the brackets.
Re: Tag from file/foldername bracket contents
Posted: 22 Aug 2012 11:23
by highend
Öhh,
it was an
example...
You have to take e.g. the other tag script (
http://www.xyplorer.com/xyfc/viewtopic. ... 9&start=15) and incorporate the regexreplace + moveto commands into it...
Re: Tag from file/foldername bracket contents
Posted: 23 Aug 2012 02:37
by kiwichick
Oh dear, my bad. Sorry I really don't understand any of what you've said

I know next-to-nothing about scripting and I must admit I'm finding it very confusing (even though I'm usually very good at picking stuff like this apart and figuring out what it means). All good, I'll just leave it for now. Thanks for your help though. The script may be of use to someone who understands what to do with it

Re: Tag from file/foldername bracket contents
Posted: 28 Aug 2012 00:01
by kiwichick
Hi there, Is there anyone who can help me with simplified (newbie) instructions on how to do this? Cheers.
Re: Tag from file/foldername bracket contents
Posted: 28 Aug 2012 05:14
by kiwichick
So, with help from highend's previous code and finding what I could in the Help File, I've managed to create a basic script as a start. It will tag a selected item with the bracket contents:
$a=<curbase>; tag regexreplace($a, "(.*\[)(\d{2,})(.*)", "$2"),,1;
As I said, it's very basic and in the Help file I've found some of the individual commands, etc that (I think) are what I need to improve it but I can't make them all work together. If someone can point me in the right direction that would be GREAT!! Things I would love help with:
1. When I select (not open) a folder it tags only the folder.
Q. How to: get the script to tag both the folder and its contents with the foldername brackets (any content items with brackets are only tagged as per the folder, they are not given their own name bracket tags).
2. If I select an item without brackets, the item is tagged with its base-name. So, John [1983].txt is tagged with 1983 but John.txt is tagged with John.
Q. How to: get the script to look for brackets and, if not found, a) skip non-bracketed items (if multiple items are selected) or b) popup msg (if no selected items contain brackets).
3. If I select more than one item, the tag of the last selected item is used on all items. So items selected in the following order are all tagged with 1999:
Folder A [1979]
Folder B [2012]
File C [1983].ext
File D [1999].ext
Q. How to: tag each selected item according to its own name only. So the result would be:
Folder A and its contents are tagged with 1979
Folder B and its contents are tagged with 2012
File C is tagged with 1983
File D is tagged with 1999
Thanks for any help anyone can offer.
Re: Tag from file/foldername bracket contents
Posted: 28 Aug 2012 06:11
by highend
Q. How to: get the script to tag both the folder and its contents with the foldername brackets (any content items with brackets are only tagged as per the folder, they are not given their own name bracket tags).
1. Store the digits in the bracket from the current selected folder in a variable
2. Do a folderreport("items", ...) on this folder to get all files and folders and store it in another variable
3. Use a foreach() loop over the folderreport list to tag everything
Q. How to: get the script to look for brackets and, if not found, a) skip non-bracketed items (if multiple items are selected) or b) popup msg (if no selected items contain brackets).
strpos() is a script command that let's you search for (a) character(s) inside strings
If strpos() returns -1, the char wasn't found
E.g.:
Code: Select all
$a = "has a square bracket ]"; if(strpos($a, "]") != -1) { echo "closing square bracket found"; } else { echo "no closing square bracket found"; }
You have to do this if check in the foreach() loop.
How to: tag each selected item according to its own name only. So the result would be:
This code creates a list of (only) selected items:
Code: Select all
$selectedItems = get("SelectedItemsPathNames", "|");
Run a foreach() loop over the variable and inside it, store the brackets content (via the regexreplace command) in a variable and tag the file with the (tag()) command.
Everything you need is in the script, that I already posted (
http://www.xyplorer.com/xyfc/viewtopic. ... 9&start=15).
You just have to modify it a bit to fit YOUR needs.
Scripting isn't that difficult. Ok, sometimes it is, but you need only very basic things and if you read the script carefully and look at each command in the help file it's really easy to change it.
I've done exactly the same, I learned from scripts that I found in the script exchange section and when it got more complicated, I asked the script experts (Stefan, The Qwerty, etc.) for help :O
Re: Tag from file/foldername bracket contents
Posted: 28 Aug 2012 06:29
by kiwichick
Thanks highend, Again, I barely understand anything you've said - sorry there's too much lingo and not enough explain-it-to-a-5yearold

Explaining 'what' to do is not the same as explaining 'how' to do it. To every answer you've given I just want to reply "But, how?"

And I wanted a separate script from the one you provided earlier. I'll see what I can figure out, though.
Re: Tag from file/foldername bracket contents
Posted: 11 Sep 2012 05:52
by kiwichick
For anyone else who may be interested, this script works on selected Files, Folders and ALL selected Folder contents (files and subfolders no matter how deep they go). It can Add (not Set) Tags with [bracket] contents. And it can also Tag with [bracket] contents then Move to a folder (location in the code - default is <curpath>) named after those [bracket] contents. Enjoy!
Code: Select all
"Tag with bracket contents|:tagsadd"
end(getinfo("CountSelected") < 1), "At least one File or Folder must be selected!";
$lstF = get(selecteditemspathnames);
foreach ($tkF, $lstF, "<crlf>") {
$tst = exists($tkF);
IF ($tst == "0") { end 1, "Script will close - No items were selected!"; }
ELSEIF ($tst == "1") { $b = regexreplace("$tkF", "(.+\\).+", "$1"); $bfldr = regexreplace($tkF, "(.*\[)(\d{2,})(.*)", "$2"); tag "$bfldr", "$tkF", 1; continue 2; }
ELSEIF ($tst == "2") { }
$lst = folderreport("items", "r", $tkF, "r");
foreach ($tk, $lst, "<crlf>") {
$b = regexreplace("$tk", "(.+\\).+", "$1");
IF ($b == "") { continue; }
ELSEIF ($b != "") { }
$bfldr = regexreplace($tk, "(.*\[)(\d{2,})(.*)", "$2");
tag "$bfldr", "$tk", 1;
}
$b = regexreplace("$tkF", "(.+\\).+", "$1");
$bfldr = regexreplace($tkF, "(.*\[)(\d{2,})(.*)", "$2");
tag "$bfldr", "$tkF", 1;
}
status "Tagging done!", "339933";
"Tag with bracket contents and Move|:tagsadd"
end(getinfo("CountSelected") < 1), "At least one File or Folder must be selected!";
$src = get(selecteditemspathnames);
$cp = "<curpath>"; // change for a different 'Move' location
foreach ($tkF, $src, "<crlf>") {
$tk = regexreplace($tkF, ".+\\(.+)\.*", "$1");
$tag = regexreplace($tk, "(.*\()(\d{2,})(.*)", "$2");
IF ($tag == "$tk") { $tag = regexreplace($tk, "(.*\[)(\d{2,})(.*)", "$2"); }
ELSEIF ($tag != "$tk") { }
IF ($tag == "$tk") { status "will not be tagged!: $tk", "FF0000", alert; wait 2000; status "moving on..."; continue 2; }
ELSEIF ($tag != "$tk") { }
$trgt = "$cp" . "\" . "$tag" . "\" . "$tk";
moveto "$cp" . "\" . "$tag", "$tkF", , 2;
$tst = exists($trgt);
IF ($tst == "0") { end 1, "Script will close - No items were selected!"; }
ELSEIF ($tst == "1") { tag "$tag", "$trgt", 1; continue 2; }
ELSEIF ($tst == "2") { }
tag "$tag", "$trgt", 1;
$lst = folderreport("items", "r", $trgt, "r");
IF ($lst == "") { continue 2; }
ELSEIF ($lst != "") { }
foreach ($tk, $lst, "<crlf>") {
IF ($tk == "") { continue 2; }
ELSEIF ($tk != "") { }
tag "$tag", "$tk", 1; }
}
status "Tagged and Moved!", "339933";