keys %files returns the list of keys (the filenames) and sorts them by a numeric comparison of the associated value (the modification time). (In the original code, it selected only certain filenames, but you'd changed the pattern to match anything.)Īs for how it sorts filenames: get_sorted_files returns a list of pathnames and modification times, which you store into the %files hash. How to use timestamp to get recent files in Perl. You'd need to pass "./", or better yet, fix it so the function appends a separator if needed.įourth, the grep no longer did anything and should be removed. Perl Rename Files to Modified Time with Perl JDave 0 Comments Perl, programming, scripting We were processing hundreds of recordings for a client, but they didn’t know the dates on the files because moving between systems changed the date time stamp. the time stamp on file name 'test' is 12:30 pm but the script time display as 10:30 am. Third, it expects $path to end with a directory separator, and you were passing ".". localtime was converting the filename to a number (yielding 0), and then converting epoch time 0 ( 0:00:00 UTC) to your timezone. The first filename was a key, the second was its value, the third was the next key, and so on. (stat($_)) was returning an empty list (because you were looking for a file like GLOB(0x3f9b38)status.txt instead of the correct pathname) and so the hash actually wound up containing filenames as both keys and values. Second, all the modification dates read "Wed Dec 31 16:00:00 1969" because you were passing a bad pathname to stat. Your $dir is a directory handle that's where the GLOB(0x.) is coming from. My %hash = map ), "\n" įirst, you renamed $dir in the original code to $path, but didn't change it in the map line. File visiting buffers record the modification time of the file so as to guard against changes by another. Emacs file name handler magic applies to both file-attributes and set-file-times so they can act on 'remote' files too. Time values are in the usual Emacs list form. Opendir my($dir), $path or die "can't opendir $path: $!" set-file-times sets both the access time and modification time of the file. the timestamp, and the file count is printed in a table form. File::Find is also core perl module, included with perl. Use warnings # I bet you weren't using this, because it produced a lot Alternatively, you can use the strftime() function from the POSIX module, which is a core module, and is included with perl. There are at least 2 problems with that code.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |