How to change file date attributes (at least modified date) using "dart:io"? How to change file date attributes (at least modified date) using "dart:io"? dart dart

How to change file date attributes (at least modified date) using "dart:io"?


The first method that comes to mind would be to just call touch using Process.

eg.

import 'dart:io';Future touchFile(File f) {  return Process.run("touch", [f.path]);}void main() {   var f = new File('example');   print(f.statSync().changed);   touchFile(f).then((_) {     print(f.statSync().changed);   });}

The equivalent code for people who are chained to windows would be

Future touchFile(File f) {  return Process.run("copy", ["\b", f.path, "+,,"]);}

See this question


Calling out to system processes seems like a serious hack. Here are two functions that uses only Dart APIs, and is not platform dependent. One is synchronous, and the other asynchronous. Use whichever version suits your needs.

void touchFileSync(File file) {  final touchfile = file.openSync(mode: FileMode.append);  touchfile.flushSync();  touchfile.closeSync();}Future<void> touchFile(File file) async {  final touchfile = await file.open(mode: FileMode.append);  await touchfile.flush();  await touchfile.close();}

This will update the last modified time.