Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need to update a file on a server every fifteen or so minutes. This involves a file copy from an authoring site on to the server.

Occasionally (And with a lot of bad luck) the .Net application that serves the file reads it at a period in time when the file is being overwritten and therefore reads in an empty file. The problem is compounded because for performance reasons the file is cached for up to 15 minutes.

Is there anyway in Windows to minimise the possibility of the file being blank? i.e. is there a way to copy the contents into the file but not "commit" them to the file system until its fully written?

The file is copied using SSH/FTP as its on a non-domain box within the DMZ and hosted on Windows 2008 R1 machines.

share|improve this question
up vote 3 down vote accepted

The usual solution is to upload using a temporary name and rename when upload complete.

share|improve this answer
... which relies for proper operation upon atomic renaming semantics that Win32 doesn't have. The Win32 solution then becomes to delete/rename out of the way the existing file just before renaming its replacement in place (retrying until the file has no open handles and it can be deleted/renamed), thereby leaving a window where no file by the desired name exists at all. – JdeBP Jun 8 '11 at 13:54
@JdeBP: Perhaps the .Net application can be coded to handle this (if file ! exists sleep 10 ms and try again, quit after n fails) – RedGrittyBrick Jun 8 '11 at 14:00
According to a stackoverflow question "In Windows Vista and Windows Server 2008 an atomic move function has been added - MoveFileTransacted()" - maybe someday some sftpd implementation will use it? – RedGrittyBrick Jun 8 '11 at 14:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .