Script request: Clone comments of folders and file to a new .txt file

Discuss and share scripts and script files...
Post Reply
karimmaster101
Posts: 158
Joined: 20 May 2011 14:34

Script request: Clone comments of folders and file to a new .txt file

Post by karimmaster101 »

Hello,
I want a script that clone comments of files/folders/sub-folders in the current directory to a new created text files.

The scenario goes like this:

Current directory is C:\exampledir

- This dir has folders and files which has comments, sub-folders too
- The script will search for all items that has comments -as deep as it could go-
- Then clone the comment of every item, then create [file/flodername_Xycomment].txt file which contains the cloned comment.
- In case of folder, the txt file should be placed inside the commented folder
So if >> C:\exampledir\folder1 has a comment, the result file path will be C:\exampledir\folder1\folder1_Xycomment.txt
- In case of the file, the txt file should be placed at the same path as the file
So if >> C:\exampledir\folder30\sub-folder1\myvideo20.mp4 has a comment, the result file path will be C:\exampledir\folder30\sub-folder1\myvideo20_Xycomment.txt

Take your time and many thanks in advance :)

highend
Posts: 13277
Joined: 06 Feb 2011 00:33

Re: Script request: Clone comments of folders and file to a new .txt file

Post by highend »

Where exactly is the difficulty to write this (on your own)?

- You can get all the files and folders with a comment in a specific path via quicksearch()
and their belonging comment with tagitems()
- Loop over these results with foreach()
- Create the necessary names files (together with their path) with getpathcomponent()
- Write (writefile()) a corresponding .txt file and put the belonging comment in

E.g.: viewtopic.php?f=7&t=17657&p=153240

Already has some of these building blocks and the whole script fits easily
into 8 lines of code...

If you have trouble to implement a specific part I'm happy to help but my motto is:

Catch somebody a fish and feed him for a day or teach him how to fish and he can feed
himself for the rest of his life...
One of my scripts helped you out? Please donate via Paypal

jupe
Posts: 2758
Joined: 20 Oct 2017 21:14
Location: Win10 22H2 120dpi

Re: Script request: Clone comments of folders and file to a new .txt file

Post by jupe »

I am new to scripting XY so I'm still a noob at it but I thought I would give your request a go, it will probably need some work from you but it will give you the basic idea I think, there may be problems with it so you should check it over yourself but I think it's close to what you want. Those more experienced will probably have a better way of doing it, but here is my attempt. Hopefully no one here minds my contribution.

Code: Select all

	
	$searchresults	= quicksearch("cmt:""*"" /f", <curpath>);
	foreach($file, $searchresults, <crlf>) {
		writefile(gpc($file, "path") . "\" . gpc($file, "base") . "_XYcomment.txt", tagitems("cmt", , $file, 1));
	}

	$searchresults	= quicksearch("cmt:""*"" /d", <curpath>);
	foreach($folder, $searchresults, <crlf>) {
		writefile(gpc($folder, "path") . "\" . gpc($folder, "component", -1) . "\" . gpc($folder, "component", -1) . "_XYcomment.txt", tagitems("cmt", , $folder, 1));
	}

highend
Posts: 13277
Joined: 06 Feb 2011 00:33

Re: Script request: Clone comments of folders and file to a new .txt file

Post by highend »

Just a hint to speed it up: tagitems() can get the tags from all items in one go
and a while loop instead of the foreach one can then read $i from both variables.
Spares all individual tagitems() calls...
One of my scripts helped you out? Please donate via Paypal

jupe
Posts: 2758
Joined: 20 Oct 2017 21:14
Location: Win10 22H2 120dpi

Re: Script request: Clone comments of folders and file to a new .txt file

Post by jupe »

This is just a rough draft to see it I am on the right track of how you think it should be done, so obviously its no good for the OP as is but is the basic structure close to what you envisioned?

Code: Select all

	

	$searchresults = quicksearch("cmt:* /f", <curpath>);
	$tags = tagitems("cmt", , $searchresults, 1);
	$tags = replace($tags, <crlf>, "|");
	
	$pos = 1;
	$count = gettoken($tags, "count", "|");
	
	while ($pos <= $count) {
		$file = gettoken($searchresults, $pos, <crlf>);
		$tag = gettoken($tags, $pos, "|");
		writefile(gpc($file, "path") . "\" . gpc($file, "base") . "_XYcomment.txt", $tag);
		$pos++;
	}


highend
Posts: 13277
Joined: 06 Feb 2011 00:33

Re: Script request: Clone comments of folders and file to a new .txt file

Post by highend »

Correct. Although I tend to write things in one loop and do not split them up into two (because
you're treating files and folders differently right from the start). You can write it in a more condensed
way though...

Code: Select all

    $items    = quicksearch("/fld=cmt");
    $comments = tagitems("cmt", , $items, 1);

    while ($i++ < gettoken($items, "count", <crlf>)) {
        $item    = gettoken($items, $i, <crlf>);
        $comment = replace(gettoken($comments, $i, <crlf>), "¶", <crlf>);
        // writefile() stuff; with an if (exists($item) == 2) clause to separate files/folders...
    }
One of my scripts helped you out? Please donate via Paypal

jupe
Posts: 2758
Joined: 20 Oct 2017 21:14
Location: Win10 22H2 120dpi

Re: Script request: Clone comments of folders and file to a new .txt file

Post by jupe »

Ah I see, cool, I didn't realize that you could start a variable like that "while($i++" without it being initialized already. I purposefully treated files and folders different from the start in my initial example because I thought it was just as easy as running a check to see if it was a file or folder later on anyway, but it's probably quicker only doing the one search like you say. Anyway I'll leave the OP to make the rest of the script for themselves since there is plenty here for them to piece together now, but thanks for your guidance it's greatly appreciated and I am sure it will help me with my scripting abilities. :)

Sorry for semi hi-jacking your thread.

highend
Posts: 13277
Joined: 06 Feb 2011 00:33

Re: Script request: Clone comments of folders and file to a new .txt file

Post by highend »

Sorry for semi hi-jacking your thread
No problem
One of my scripts helped you out? Please donate via Paypal

Post Reply