This commit is contained in:
Ingy döt Net 2013-04-10 21:29:02 -07:00
parent 764da6cbbb
commit db842d013d
19005 changed files with 197040 additions and 7 deletions

View file

@ -0,0 +1 @@
This task will attempt to get and set the modification time of a file.

View file

@ -0,0 +1,2 @@
---
note: File System Operations

View file

@ -0,0 +1,8 @@
with Ada.Directories; use Ada.Directories;
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Calendar.Formatting; use Ada.Calendar.Formatting;
procedure File_Time_Test is
begin
Put_Line (Image (Modification_Time ("file_time_test.adb")));
end File_Time_Test;

View file

@ -0,0 +1,5 @@
FileGetTime, OutputVar, output.txt
MsgBox % OutputVar
FileSetTime, 20080101, output.txt
FileGetTime, OutputVar, output.txt
MsgBox % OutputVar

View file

@ -0,0 +1,27 @@
DIM ft{dwLowDateTime%, dwHighDateTime%}
DIM st{wYear{l&,h&}, wMonth{l&,h&}, wDayOfWeek{l&,h&}, \
\ wDay{l&,h&}, wHour{l&,h&}, wMinute{l&,h&}, \
\ wSecond{l&,h&}, wMilliseconds{l&,h&} }
REM File is assumed to exist:
file$ = @tmp$ + "rosetta.tmp"
REM Get and display the modification time:
file% = OPENIN(file$)
SYS "GetFileTime", @hfile%(file%), 0, 0, ft{}
CLOSE #file%
SYS "FileTimeToSystemTime", ft{}, st{}
date$ = STRING$(16, CHR$0)
time$ = STRING$(16, CHR$0)
SYS "GetDateFormat", 0, 0, st{}, 0, date$, LEN(date$) TO N%
date$ = LEFT$(date$, N%-1)
SYS "GetTimeFormat", 0, 0, st{}, 0, time$, LEN(time$) TO N%
time$ = LEFT$(time$, N%-1)
PRINT date$ " " time$
REM Set the modification time to the current time:
SYS "GetSystemTime", st{}
SYS "SystemTimeToFileTime", st{}, ft{}
file% = OPENUP(file$)
SYS "SetFileTime", @hfile%(file%), 0, 0, ft{}
CLOSE #file%

View file

@ -0,0 +1 @@
for %%f in (file.txt) do echo.%%~tf

View file

@ -0,0 +1,25 @@
#include <boost/filesystem/operations.hpp>
#include <ctime>
#include <iostream>
int main( int argc , char *argv[ ] ) {
if ( argc != 2 ) {
std::cerr << "Error! Syntax: moditime <filename>!\n" ;
return 1 ;
}
boost::filesystem::path p( argv[ 1 ] ) ;
if ( boost::filesystem::exists( p ) ) {
std::time_t t = boost::filesystem::last_write_time( p ) ;
std::cout << "On " << std::ctime( &t ) << " the file " << argv[ 1 ]
<< " was modified the last time!\n" ;
std::cout << "Setting the modification time to now:\n" ;
std::time_t n = std::time( 0 ) ;
boost::filesystem::last_write_time( p , n ) ;
t = boost::filesystem::last_write_time( p ) ;
std::cout << "Now the modification time is " << std::ctime( &t ) << std::endl ;
return 0 ;
} else {
std::cout << "Could not find file " << argv[ 1 ] << '\n' ;
return 2 ;
}
}

View file

@ -0,0 +1,27 @@
#include <sys/stat.h>
#include <stdio.h>
#include <time.h>
#include <utime.h>
const char *filename = "input.txt";
int main() {
struct stat foo;
time_t mtime;
struct utimbuf new_times;
if (stat(filename, &foo) < 0) {
perror(filename);
return 1;
}
mtime = foo.st_mtime; /* seconds since the epoch */
new_times.actime = foo.st_atime; /* keep atime unchanged */
new_times.modtime = time(NULL); /* set mtime to current time */
if (utime(filename, &new_times) < 0) {
perror(filename);
return 1;
}
return 0;
}

View file

@ -0,0 +1,24 @@
#include <sys/stat.h>
#include <sys/time.h>
#include <err.h>
const char *filename = "input.txt";
int main() {
struct stat foo;
struct timeval new_times[2];
if (stat(filename, &foo) < 0)
err(1, "%s", filename);
/* keep atime unchanged */
TIMESPEC_TO_TIMEVAL(&new_times[0], &foo.st_atim);
/* set mtime to current time */
gettimeofday(&new_times[1], NULL);
if (utimes(filename, new_times) < 0)
err(1, "%s", filename);
return 0;
}

View file

@ -0,0 +1,30 @@
#include <sys/stat.h>
#include <sys/time.h>
#include <time.h>
#include <fcntl.h>
#include <stdio.h>
const char *filename = "input.txt";
int main() {
struct stat foo;
struct timespec new_times[2];
if (stat(filename, &foo) < 0) {
perror(filename);
return 1;
}
/* keep atime unchanged */
new_times[0] = foo.st_atim;
/* set mtime to current time */
clock_gettime(CLOCK_REALTIME, &new_times[1]);
if (utimensat(AT_FDCWD, filename, new_times, 0) < 0) {
perror(filename);
return 1;
}
return 0;
}

View file

@ -0,0 +1,107 @@
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <wchar.h>
/* Print "message: last Win32 error" to stderr. */
void
oops(const wchar_t *message)
{
wchar_t *buf;
DWORD error;
buf = NULL;
error = GetLastError();
FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, error, 0, (wchar_t *)&buf, 0, NULL);
if (buf) {
fwprintf(stderr, L"%ls: %ls", message, buf);
LocalFree(buf);
} else {
/* FormatMessageW failed. */
fwprintf(stderr, L"%ls: unknown error 0x%x\n",
message, error);
}
}
int
setmodtime(const wchar_t *path)
{
FILETIME modtime;
SYSTEMTIME st;
HANDLE fh;
wchar_t date[80], time[80];
fh = CreateFileW(path, GENERIC_READ | FILE_WRITE_ATTRIBUTES,
0, NULL, OPEN_EXISTING, 0, NULL);
if (fh == INVALID_HANDLE_VALUE) {
oops(path);
return 1;
}
/*
* Use GetFileTime() to get the file modification time.
*/
if (GetFileTime(fh, NULL, NULL, &modtime) == 0)
goto fail;
FileTimeToSystemTime(&modtime, &st);
if (GetDateFormatW(LOCALE_USER_DEFAULT, DATE_LONGDATE, &st, NULL,
date, sizeof date / sizeof date[0]) == 0 ||
GetTimeFormatW(LOCALE_USER_DEFAULT, 0, &st, NULL,
time, sizeof time / sizeof time[0]) == 0)
goto fail;
wprintf(L"%ls: Last modified at %s at %s (UTC).\n",
path, date, time);
/*
* Use SetFileTime() to change the file modification time
* to the current time.
*/
GetSystemTime(&st);
if (GetDateFormatW(LOCALE_USER_DEFAULT, DATE_LONGDATE, &st, NULL,
date, sizeof date / sizeof date[0]) == 0 ||
GetTimeFormatW(LOCALE_USER_DEFAULT, 0, &st, NULL,
time, sizeof time / sizeof time[0]) == 0)
goto fail;
SystemTimeToFileTime(&st, &modtime);
if (SetFileTime(fh, NULL, NULL, &modtime) == 0)
goto fail;
wprintf(L"%ls: Changed to %s at %s (UTC).\n", path, date, time);
CloseHandle(fh);
return 0;
fail:
oops(path);
CloseHandle(fh);
return 1;
}
/*
* Show the file modification time, and change it to the current time.
*/
int
main()
{
int argc, i, r;
wchar_t **argv;
/* MinGW never provides wmain(argc, argv). */
argv = CommandLineToArgvW(GetCommandLineW(), &argc);
if (argv == NULL) {
oops(L"CommandLineToArgvW");
exit(1);
}
if (argc < 2) {
fwprintf(stderr, L"usage: %ls file...\n", argv[0]);
exit(1);
}
r = 0;
for (i = 1; argv[i] != NULL; i++)
if (setmodtime(argv[i])) r = 1;
return r;
}

View file

@ -0,0 +1,8 @@
(import '(java.io File)
'(java.util Date))
(Date. (.lastModified (File. "output.txt")))
(Date. (.lastModified (File. "docs")))
(.setLastModified (File. "output.txt")
(.lastModified (File. "docs")))

View file

@ -0,0 +1 @@
(file-write-date "input.txt")

View file

@ -0,0 +1,6 @@
(let* ((filename "input.txt")
(stat (sb-posix:stat filename))
(mtime (sb-posix:stat-mtime stat)))
(sb-posix:utime filename
(sb-posix:stat-atime stat)
(sb-posix:time)))

View file

@ -0,0 +1,10 @@
import std.stdio;
import std.file: getTimes, setTimes, SysTime;
void main() {
auto fname = "unixdict.txt";
SysTime fileAccessTime, fileModificationTime;
getTimes(fname, fileAccessTime, fileModificationTime);
writeln(fileAccessTime, "\n", fileModificationTime);
setTimes(fname, fileAccessTime, fileModificationTime);
}

View file

@ -0,0 +1,16 @@
procedure GetModifiedDate(const aFilename: string): TDateTime;
var
hFile: Integer;
iDosTime: Integer;
begin
hFile := FileOpen(aFilename, fmOpenRead);
iDosTime := FileGetDate(hFile);
FileClose(hFile);
if (hFile = -1) or (iDosTime = -1) then raise Exception.Create('Cannot read file: ' + sFilename);
Result := FileDateToDateTime(iDosTime);
end;
procedure ChangeModifiedDate(const aFilename: string; aDateTime: TDateTime);
begin
FileSetDate(aFileName, DateTimeToFileDate(aDateTime));
end;

View file

@ -0,0 +1,16 @@
def strdate(date) {
return E.toString(<unsafe:java.util.makeDate>(date))
}
def test(type, file) {
def t := file.lastModified()
println(`The following $type called ${file.getPath()} ${
if (t == 0) { "does not exist." } else { `was modified at ${strdate(t)}` }}`)
println(`The following $type called ${file.getPath()} ${
escape ne { file.setLastModified(timer.now(), ne); "was modified to current time." } catch _ { "does not exist." }}`)
println(`The following $type called ${file.getPath()} ${
escape ne { file.setLastModified(t, ne); "was modified to previous time." } catch _ { "does not exist." }}`)
}
test("file", <file:output.txt>)
test("directory", <file:docs>)

View file

@ -0,0 +1 @@
"foo.txt" file-info modified>> .

View file

@ -0,0 +1,3 @@
USE: io.files.info.unix
"foo.txt" now 2 hours time+ set-file-modified-time

View file

@ -0,0 +1 @@
Start,My Documents,Rightclick:Icon:Foobar.txt,Properties

View file

@ -0,0 +1,27 @@
package main
import (
"fmt"
"os"
"syscall"
"time"
)
var filename = "input.txt"
func main() {
foo, err := os.Stat(filename)
if err != nil {
fmt.Println(err)
return
}
fmt.Println("mod time was:", foo.ModTime())
mtime := time.Now()
atime := mtime // a default, because os.Chtimes has an atime parameter.
// but see if there's a real atime that we can preserve.
if ss, ok := foo.Sys().(*syscall.Stat_t); ok {
atime = time.Unix(ss.Atim.Sec, ss.Atim.Nsec)
}
os.Chtimes(filename, atime, mtime)
fmt.Println("mod time now:", mtime)
}

View file

@ -0,0 +1,9 @@
import System.Posix.Files
import System.Posix.Time
do status <- getFileStatus filename
let atime = accessTime status
mtime = modificationTime status -- seconds since the epoch
curTime <- epochTime
setFileTimes filename atime curTime -- keep atime unchanged
-- set mtime to current time

View file

@ -0,0 +1,6 @@
import System.Directory
import System.Time
do ct <- getModificationTime filename
cal <- toCalendarTime ct
putStrLn (calendarTimeToString cal)

View file

@ -0,0 +1,7 @@
CHARACTER timestamp*18
timestamp = ' ' ! blank timestamp will read:
SYSTEM(FIle="File_modification_time.hic", FileTime=timestamp) ! 20100320141940.525
timestamp = '19991231235950' ! set timestamp to Millenium - 10 seconds
SYSTEM(FIle="File_modification_time.hic", FileTime=timestamp)

View file

@ -0,0 +1,9 @@
every dir := !["./","/"] do {
if i := stat(f := dir || "input.txt") then {
write("info for ",f ," mtime= ",ctime(i.mtime),", atime=",ctime(i.ctime), ", atime=",ctime(i.atime))
utime(f,i.atime,i.mtime-1024)
i := stat(f)
write("update for ",f ," mtime= ",ctime(i.mtime),", atime=",ctime(i.ctime), ", atime=",ctime(i.atime))
}
else stop("failure to stat ",f)
}

View file

@ -0,0 +1,3 @@
load 'files'
fstamp 'input.txt'
2009 8 24 20 34 30

View file

@ -0,0 +1,20 @@
import java.io.File;
import java.util.Date;
public class FileModificationTimeTest {
public static void test(String type, File file) {
long t = file.lastModified();
System.out.println("The following " + type + " called " + file.getPath() +
(t == 0 ? " does not exist." : " was modified at " + new Date(t).toString() )
);
System.out.println("The following " + type + " called " + file.getPath() +
(!file.setLastModified(System.currentTimeMillis()) ? " does not exist." : " was modified to current time." )
);
System.out.println("The following " + type + " called " + file.getPath() +
(!file.setLastModified(t) ? " does not exist." : " was modified to previous time." )
);
}
public static void main(String args[]) {
test("file", new File("output.txt"));
test("directory", new File("docs"));
}
}

View file

@ -0,0 +1,3 @@
var fso = new ActiveXObject("Scripting.FileSystemObject");
var f = fso.GetFile('input.txt');
var mtime = f.DateLastModified;

View file

@ -0,0 +1,2 @@
var file = document.getElementById("fileInput").files.item(0);
var last_modified = file.lastModifiedDate;

View file

@ -0,0 +1,13 @@
require "lfs"
local attributes = lfs.attributes("input.txt")
if attributes then
print(path .. " was last modified " .. os.date("%c", attributes.modification) .. ".")
-- set access and modification time to now ...
lfs.touch("input.txt")
-- ... or set modification time to now, keep original access time
lfs.touch("input.txt", attributes.access, os.time())
else
print(path .. " does not exist.")
end

View file

@ -0,0 +1,5 @@
f = dir('output.txt'); % struct f contains file information
f.date % is string containing modification time
f.datenum % numerical format (number of days)
datestr(f.datenum) % is the same as f.date
% see also: stat, lstat

View file

@ -0,0 +1 @@
system('touch -t 201002032359.59 output.txt');

View file

@ -0,0 +1,2 @@
-- Returns a string containing the mod date for the file, e.g. "1/29/99 1:52:05 PM"
getFileModDate "C:\myFile.txt"

View file

@ -0,0 +1 @@
FileDate["file","Modification"]

View file

@ -0,0 +1 @@
SetFileDate["file",date,"Modification"]

View file

@ -0,0 +1,27 @@
MODULE ModTime EXPORTS Main;
IMPORT IO, Fmt, File, FS, Date, OSError;
TYPE dateArray = ARRAY [0..5] OF TEXT;
VAR
file: File.Status;
date: Date.T;
PROCEDURE DateArray(date: Date.T): dateArray =
BEGIN
RETURN
dateArray{Fmt.Int(date.year), Fmt.Int(ORD(date.month) + 1), Fmt.Int(date.day),
Fmt.Int(date.hour), Fmt.Int(date.minute), Fmt.Int(date.second)};
END DateArray;
BEGIN
TRY
file := FS.Status("test.txt");
date := Date.FromTime(file.modificationTime);
IO.Put(Fmt.FN("%s-%02s-%02s %02s:%02s:%02s", DateArray(date)));
IO.Put("\n");
EXCEPT
| OSError.E => IO.Put("Error: Failed to get file status.\n");
END;
END ModTime.

View file

@ -0,0 +1,22 @@
MODULE SetModTime EXPORTS Main;
IMPORT Date, FS;
<*FATAL ANY*>
VAR
date: Date.T;
BEGIN
(* Set the modification time to January 1st, 1999 *)
date.year := 1999;
date.month := Date.Month.Jan;
date.day := 1;
date.hour := 0;
date.minute := 0;
date.second := 0;
date.offset := 21601;
date.zone := "CST";
FS.SetModificationTime("test.txt", Date.ToTime(date));
END SetModTime.

View file

@ -0,0 +1,9 @@
<?php
$filename = 'input.txt';
$mtime = filemtime($filename); // seconds since the epoch
touch($filename,
time(), // set mtime to current time
fileatime($filename)); // keep atime unchanged
?>

View file

@ -0,0 +1,9 @@
my $mtime = (stat($file))[9]; # seconds since the epoch
# you should use the more legible version below:
use File::stat qw(stat);
my $mtime = stat($file)->mtime; # seconds since the epoch
utime(stat($file)->atime, time, $file);
# keep atime unchanged
# set mtime to current time

View file

@ -0,0 +1,5 @@
(let File "test.file"
(and
(info File)
(prinl (stamp (cadr @) (cddr @))) ) # Print date and time in UTC
(call 'touch File) ) # Set modification time to "now"

View file

@ -0,0 +1,13 @@
import os
#Get modification time:
modtime = os.path.getmtime('filename')
#Set the access and modification times:
os.utime('path', (actime, mtime))
#Set just the modification time:
os.utime('path', (os.path.getatime('path'), mtime))
#Set the access and modification times to the current time:
os.utime('path', None)

View file

@ -0,0 +1,7 @@
# Get the value
file.info(filename)$mtime
#To set the value, we need to rely on shell commands. The following works under windows.
shell("copy /b /v filename +,,>nul")
# and on Unix (untested)
shell("touch -m filename")

View file

@ -0,0 +1,2 @@
#lang racket
(file-or-directory-modify-seconds "foo.rkt")

View file

@ -0,0 +1,11 @@
#Get modification time:
modtime = File.mtime('filename')
#Set the access and modification times:
File.utime(actime, mtime, 'path')
#Set just the modification time:
File.utime(File.atime('path'), mtime, 'path')
#Set the access and modification times to the current time:
File.utime(nil, nil, 'path')

View file

@ -0,0 +1,3 @@
|a|
a := File name: 'input.txt'.
(a lastModifyTime) printNl.

View file

@ -0,0 +1,5 @@
# Get the modification time:
set timestamp [file mtime $filename]
# Set the modification time to now:
file mtime $filename [clock seconds]