Code that interacts with files on my HDD

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

FatCat0
Posts: 4
Joined: Mon Jun 16, 2014 7:25 pm UTC

Code that interacts with files on my HDD

Postby FatCat0 » Mon Jun 16, 2014 7:36 pm UTC

This question is probably pretty simple: I would like to write a program that can look for changes in files and react accordingly. The end goal I'm going for right now is to make a program which activates when a file is updated, makes a backup of said file, then waits a prescribed time before it starts paying attention to the file being updated. Nothing groundbreaking or terribly complex, but I don't know how to do the "Pay attention to this file and act when it is modified" part of this.

I have a reasonable background in coding, so you don't have to dumb things down too much for me. I am open to suggestions from pretty much any coding language (again, my goal is simple, I don't think new syntax will trip me up), or even just general information about this stuff. Ideally I'd like to be able to accomplish this on both Windows and Linux, but the former is what I'm really interested in.

User avatar
phlip
Restorer of Worlds
Posts: 7572
Joined: Sat Sep 23, 2006 3:56 am UTC
Location: Australia
Contact:

Re: Code that interacts with files on my HDD

Postby phlip » Wed Jun 18, 2014 4:04 pm UTC

I've never actually used it myself, but I know there's an API in Linux for monitoring files for changes... no idea how to use it or if it's at all usable, but it exists.

Code: Select all

enum ಠ_ಠ {°□°╰=1, °Д°╰, ಠ益ಠ╰};
void ┻━┻︵​╰(ಠ_ಠ ⚠) {exit((int)⚠);}
[he/him/his]

speising
Posts: 2353
Joined: Mon Sep 03, 2012 4:54 pm UTC
Location: wien

Re: Code that interacts with files on my HDD

Postby speising » Wed Jun 18, 2014 4:10 pm UTC

and in win .net, there's the filesystem watcher

edit: or some of those in c++
Last edited by speising on Wed Jun 18, 2014 4:14 pm UTC, edited 1 time in total.

User avatar
phlip
Restorer of Worlds
Posts: 7572
Joined: Sat Sep 23, 2006 3:56 am UTC
Location: Australia
Contact:

Re: Code that interacts with files on my HDD

Postby phlip » Wed Jun 18, 2014 4:12 pm UTC

OK, so in my defense, it's 2AM... but I totally didn't see the words "Windows and" before the word "Linux" in the OP... :oops:

Code: Select all

enum ಠ_ಠ {°□°╰=1, °Д°╰, ಠ益ಠ╰};
void ┻━┻︵​╰(ಠ_ಠ ⚠) {exit((int)⚠);}
[he/him/his]

Breakfast
Posts: 117
Joined: Tue Jun 16, 2009 7:34 pm UTC
Location: Coming to a table near you

Re: Code that interacts with files on my HDD

Postby Breakfast » Wed Jun 18, 2014 4:44 pm UTC

FatCat0 wrote:...I don't know how to do the "Pay attention to this file and act when it is modified" part of this.


I'm going to throw around some common buzzwords and terminology for this type of thing. What you're wanting to do is LISTEN for an EVENT and RESPOND to it. Or, your file is OBSERVABLE and you might have an OBSERVER that watches it and responds to changes.

This kind of thing (depending on language) is typically accomplished with:

User avatar
headprogrammingczar
Posts: 3072
Joined: Mon Oct 22, 2007 5:28 pm UTC
Location: Beaming you up

Re: Code that interacts with files on my HDD

Postby headprogrammingczar » Thu Jun 19, 2014 12:18 am UTC

inotify is the perfect solution on Linux systems. I can't remember the name of the API on Windows, but it definitely exists and is used by stuff like Dropbox. Unfortunately, there's no good cross-platform library so you're pretty much stuck with conditionally using one or the other.
<quintopia> You're not crazy. you're the goddamn headprogrammingspock!
<Weeks> You're the goddamn headprogrammingspock!
<Cheese> I love you

FatCat0
Posts: 4
Joined: Mon Jun 16, 2014 7:25 pm UTC

Re: Code that interacts with files on my HDD

Postby FatCat0 » Thu Jun 19, 2014 3:52 pm UTC

Thanks for the info everyone! I'm thinking FindNextChangeNotification is what I'm going to use for Windows (which I'm writing first). I kind of suspected I would have to use different approaches for Win/Linux, and it seems like it will be pretty straightforward to implement both.

FLHerne
Posts: 41
Joined: Fri Jan 13, 2012 9:44 pm UTC

Re: Code that interacts with files on my HDD

Postby FLHerne » Thu Jun 19, 2014 6:30 pm UTC

If you're using Qt for other stuff already (and for a cross-platform project, even non-GUI, it's really handy! :D ), you could use a QFileSystemWatcher. Save all the platform-specific fiddling...


Return to “Coding”

Who is online

Users browsing this forum: No registered users and 6 guests