about auto-search in a folder

Asked by pipboy

The file folder content as below:
-----------------------------------
Name Date

aaa | 5/21/2011
bbb | 5/20/2011
ccc | 5/19/2011
----------------------------------

this folder would add a new files every day

how can i get the newest one(aaa,5/21) automatically by sikuli?

now i use keyboard command(click > sort the date -> click and copy),
but i think there may be a more efficient way?

finder() can do this ?

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
RaiMan
Solved:
Last query:
Last reply:
Revision history for this message
RaiMan (raimund-hocke) said :
#1

Since Sikuli talks Python, such things can be done much easier:

import os
dir = "absolute-path-to-your-folder"
files = os.listdir(dir)
latest = 0
file = ""
for f in files:
    full = os.path.join(dir, f)
    info = os.stat(full)
    t = time.strftime("%Y-%m-%d", time.localtime(info.st_ctime))
    if t > latest:
        latest = t
        file = full
print file, latest # here you have your file

Revision history for this message
pipboy (peace03) said :
#2

Many thanks !! this is very useful to me.
i need to copy and move the latest file (remote) to my computer
i tried shutil module , but it seems not work correctly?

code:

import os
import shutil

....
....

print file, latest
shutil.copy2( 'file' , 'd:/newname' ) # (remote_latest_file , save_to_local_side )

Revision history for this message
Best RaiMan (raimund-hocke) said :
#3

on Windows, you have to use backslashes.
They have to be doubled (escaped) in normal strings or use raw strings r"".
file already contains a filename and is a string, so no additional '

shutil.copy2( file , r"d:\newname" ) # (remote_latest_file , save_to_local_side )

so if file contains e.g. r"x:\some-dir\somefile.txt" you would have a copy at r"d:\newname\somefile.txt" if new name is an existing directory or simply r"d:\newname", where newname now is a text file without extension and contains the contents of r"x:\some-dir\somefile.txt".

Revision history for this message
pipboy (peace03) said :
#4

Thanks RaiMan, that solved my question.