Page 1 of 1
Finding the newest or the last "accessed" file in a folder
Posted: 27 Jun 2014 13:56
by klownboy
Hi, I'm trying to build a script similar to what Filehero did here
http://www.xyplorer.com/xyfc/viewtopic.php?f=7&t=11898 for quickly loading layouts, column layouts catalogs, etc., but I would also like to include Toolbars. Everything is working but I'm trying to use a method of time stamping the toolbar file as a way to determine and flag in the script the last loaded toolbar. I know I can use a perm variable or possibly tag, but I'd rather stay away from those methods. So I'm using a timestamp method
Code: Select all
timestamp a, , "$DIR_TOOLBARS\$item.txt";
...later in script use $DateDiff = datediff("<datea>"); in a foreach loop to determine the "newest" toolbar txt file in the folder
when loading a Toolbar earlier in the script and later I'd like to determine and flag which is the last loaded toolbar (i.e., toolbar txt file) in that folder for use in the save/save as dialog.
I'm quite sure I could do a SC "listfolder" and obtain the toolbar text files and then use SC "datediff" in a foreach or while loop to determine the file with the shortest amount of time from "now" (i.e., the last accessed toolbar text file). It would use a similar technique as used in this thread
http://www.xyplorer.com/xyfc/viewtopic. ... ge+of+file I want this to be in the background so I wouldn't want to have to use a method such as going to the folder and filtering by access date descending and select the first file. Would you know of any other simpler way to determine the last accessed file in a folder without setting a perm variable or using a tag?
Thanks,
Ken
Re: Finding the newest or the last "accessed" file in a fold
Posted: 27 Jun 2014 14:48
by bdeshi
Code: Select all
echo gettoken(runret("cmd.exe /c dir ""<xypath>"" /B /T:A /O:-D /A:-D"),1,<crlf>);
The command lists files (names only) by descending access date. (
and lists folders last ed: now folders aren't listed at all)
So the first line of the return is the file with the latest access date.
That is extracted and displayed.
In short, this code will show the last accessed file in <xypath>
Re: Finding the newest or the last "accessed" file in a fold
Posted: 27 Jun 2014 15:29
by klownboy
Unbelievable and fast Sammay! I was a DOS user writing Batch files way back in the late 80's and 90's but forgot about using good old "dir" with a million switches. I had to go look them up with a dir /? to see what each one did.

That "runret" is coming in very handy isn't it? I tweaked it slightly for my use to strip out the extension as shown and it works great.
Code: Select all
::echo replace(gettoken(runret("cmd.exe /c dir ""D:\Tools\XYplorer\Toolbars"" /B /T:A /O:-D /A:-D"),1,<crlf>), ".txt", "");
Pity there isn't a native way in XY scripting we could do something similar without resorting to using datediff in a foreach or while loop.
Thank you so much,
Ken
Re: Finding the newest or the last "accessed" file in a fold
Posted: 27 Jun 2014 16:49
by bdeshi
My pleasure.
Native XY functions? No, don't kill the cmdprompt!!

Re: Finding the newest or the last "accessed" file in a fold
Posted: 28 Jun 2014 00:37
by klownboy
No I don't think they'll kill the command line even in Windows 9.
I used this code along with other lines within a heredoc used for a menu and the resultant menu displayed was split right after the <crlf> that was used for separator for the gettoken (i.e., another line is displayed starting with the closing parenthesis after the <crlf>). I still can't figure out why since it is working properly from the AB.
Code: Select all
$savemen = <<<SAV
"Save Layout|:savesett" #673;
"Save Column layout|:treeshow" #376;
"Save Catalog|:cat" #186;
"Save Toolbar|D:\Graphics\Icons\Combined\Customize Toolbar on.ico";
$TB_last = replace(gettoken(runret("cmd.exe /c dir $DIR_TOOLBARS /B /T:A /O:-D /A:-D"), 1, <crlf>), ".txt", "");
$CurrentToolbar = toolbar();
$TB_name = input("Save current toolbar as: Your current toolbar (before modification) is: $TB_last", , $TB_last, "s");
writefile("$DIR_TOOLBARS\$TB_name.txt", $CurrentToolbar);
SAV;
The rest of the menu is built and doesn't use heredoc. It looks like when used in the menu via heredoc it performing the <crlf> instead of using it as part of the gettoken later. I've tried a bunch of different quoting possibilities and even removed the replace to see if it made a difference but obviously not. Per the heredoc help, variables are resolved and linefeeds survive, but I didn't think it would resolve the variable being created. Any ideas? I can go another route in in lieu of heredoc, but it was just bugging me. Thanks.
Ken
Re: Finding the newest or the last "accessed" file in a fold
Posted: 28 Jun 2014 06:52
by bdeshi
you can use chr(13) instead of <crlf>, which seems to survive.
Re: Finding the newest or the last "accessed" file in a fold
Posted: 28 Jun 2014 12:59
by klownboy
Again thanks Sammay, it's working fine. chr(13) survives the heredoc trip. In the meantime I had went back and rewrote that section of the script without using heredoc, but now I'll revert back to using it.

Re: Finding the newest or the last "accessed" file in a fold
Posted: 28 Jun 2014 13:05
by Filehero
klownboy wrote:... I had went back and rewrote that section of the script without using heredoc, but now I'll revert back to using it.

This evolutionary approach to perfection reminds me of someone - just sitting in front of my screen.
It's all about the fun and smile upon scripting success, anyway.
Cheers,
Filehero
Re: Finding the newest or the last "accessed" file in a fold
Posted: 28 Jun 2014 13:12
by bdeshi
You're welcome, glad to help out!
Re: Finding the newest or the last "accessed" file in a fold
Posted: 28 Jun 2014 16:59
by klownboy
Filehero wrote:It's all about the fun and smile upon scripting success, anyway.
It sure is. It can be frustrating at times especially for a non-programmer, but there's much satisfaction as you say in getting the job done even if it's not the most efficient or streamlined way of doing it. Thanks and good luck this week on the field.
Ken
Re: Finding the newest or the last "accessed" file in a fold
Posted: 29 Jun 2014 01:05
by klownboy
Just a little experimenting to obtain the last file "accessed" in a folder when I timestamp it earlier in a script as explained above. It will return the file doing it's work completely in the background.
Code: Select all
run "cmd /c <xy> /script=""::load 'test.xys','_YouDontSeeMe'"" /flg=2", , 0,0; //test.xys is this small example script loading a sub in the background
text $TB_last; unset $TB_last;
"_YouDontSeeMe"
goto "d:\tools\xyplorer\toolbars";
sortby Accessed, d; sel 1; //or use (w/o sel 1; or next line) perm $TB_last = replace(gettoken(listfolder( , "*.txt", 5, <crlf>), 1, <crlf>),".txt", "");
perm $TB_last = getpathcomponent("<curitem>", "base");
This works fine (i.e., it will reveal the last accessed file in a folder).
I noted something odd, at least at first I thought it was odd. If you use SC listfolder after performing a sort, the sort is totally disregarded by listfolder if you specify the folder in the path syntax (whether you have filters given or not). If you leave the path blank it defaults to the current and then it does obey the previous sorting. For example...
Code: Select all
goto "d:\tools\xyplorer\toolbars";
sortby Accessed, d;
$TB_last = replace(gettoken(listfolder("d:\tools\xyplorer\toolbars", , 5, <crlf>), 1, <crlf>),".txt", "");echo $TB_last; //leave path blank for it to obey previous sort
Yes, know I should be playing with paper folders or airplanes and I will tomorrow. Thanks,
Ken
Re: Finding the newest or the last "accessed" file in a fold
Posted: 08 Jul 2014 14:28
by klownboy
Concerning my previous post, unfortunately listfolder does not recognize previous sorting. I know the help file mentions that it uses the system's native sorting. Any chance Don we could have a flag to the listfolder SC that would utilize the sort in the previous script line (e.g., sortby Accessed, d;)? It would make it much easier to utilize this information in a script.
Also, since I was looking to perform all the actions in the background (e.g., find the last accessed file in a folder that is not the "current" folder), my first point above was also not valid, in that, it works, but I usually saw the location change when performed in the script using that code or similar and that's obviously not what I wanted.
Sammay as much as I liked your amazing solution to find the last accessed file in a folder using runret and good old DOS dir, I was still curious to come up with a XY solution. This works below, 7 lines of code verses your one line. It may or may not be hard to believe since your one line has to run a command box, but the XY's 7 lines of code is significantly faster executing - about 10 times faster though even more so on subsequent runs - we're talking msec though so we probably wouldn't notice.
Code: Select all
$TBFileAge = "";
$TBs = listfolder("d:\tools\xyplorer\toolbars", "*.txt", 1, "|");
foreach ($TBfile, $TBs, "|", , "") {
$p = property("#5","$TBfile"); //accessed date
$Age = datediff("$p", , "n"); //difference in time between now and accessed date in minutes
$TBFileAge = $TBFileAge . $Age . "--" . $TBfile . "|"; }
$TB_last = getpathcomponent(gettoken(gettoken(formatlist($TBFileAge, ne, "|"), 1, "|"), 2, "--"), "base"); //weed out last accessed file
verses
Code: Select all
$TB_last = replace(gettoken(runret("cmd.exe /c dir $DIR_TOOLBARS /B /T:A /O:-D /A:-D"), 1, chr(13)),".txt", "");
Re: Finding the newest or the last "accessed" file in a fold
Posted: 08 Jul 2014 15:33
by TheQwerty
More-so out of curiosity than anything else but try comparing it to this (condensing it to fewer lines if you so desire)...
Code: Select all
"MRA"
$path = "d:\tools\xyplorer\toolbars";
$pattern = '*.txt';
//Assert Exists($path) == 2, "Directory not found: $path";
$TBs = ListFolder($path, $pattern, 1, '|');
//End $TBs == '', "No files found.";
$TBs = Report("{Accessed yyyymmddhhnnssfff}|{Basename}<crlf>", $TBs);
$TBs = FormatList($TBs, 'trend', "<crlf>");
$TB_last = GetToken($TBs, 1, "<crlf>");
$TB_last = GetToken($TB_last, 2, '|');
echo $TB_last;
Though instead of finding the item with accessed time closest to now it finds the latest accessed time - even if it is long into the future.

Re: Finding the newest or the last "accessed" file in a fold
Posted: 08 Jul 2014 18:33
by klownboy
Thanks TheQwerty, it's funny you used "report" because that was the other method I was originally thinking of using. I tested the time on both your version and mine and they both executed so fast even after changing the last accessed file that the time difference was "0" msecs. The runret version though a nice idea will always take longer. In this case I'm tracking the last loaded toolbar txt file so it could be flagged in the script. I don't think the accessed time difference you explained would make any difference.
Thanks again,
Ken