4

Here's some easy points for someone:

If I issue a command like copy * /destination in a folder containing a file that is being written to, will the partially written file be copied? Will it be ignored? Will copy throw an error?

Basically I have a script that copies files from a folder to a new location on a schedule, and there could be a case in which a new file is being written to the source folder when the script runs. I'm wondering what precautions I might need to take.

Edit: I should mention that the file will be coming in via SMB to a shared folder.

2
  • Why don't you try it and see what happens? With test files, of course.
    – shufler
    Oct 29, 2009 at 17:36
  • I did, and got different results depending on how the file was being written. I thought perhaps there was some definitive guideline.
    – Boden
    Oct 29, 2009 at 17:47

2 Answers 2

3

Either:

  1. The file will be locked and nothing will happen. It may crash your script if you don't have decent error handling.
  2. A copy of the file will be made in the state it was in at the instant the copy was initiated. Data written to the file after this instant will not be in the copied file. Think of it as a snapshot of sorts

As always, test test test, and move from there.

3
  • I tried this two ways. In one case I started a download of a large file in Firefox, and ran the copy command. It copied both the 0 byte destination file and the .part file that are created by Firefox. In another case I started a copy of a large file from a network share and then ran the copy command, and it errored with a "the process cannot access the file because it is being used by another process." That was using explorer, however. The real files will come in via smb from a copy machine, and they get written so quickly it's difficult to test...I'll have to scan a few bazillion pages.
    – Boden
    Oct 29, 2009 at 17:51
  • Ok I tested by initiating a copy via shell from one machine to share on another machine. While the file was being copied, the copy command run on the destination machine failed with the error I mentioned above. That's what I want. Can you elaborate in your answer under what circumstances your two possible outcomes would occur? Why does firefox write its files without locking? When copying a file via SMB/CIFS will it always be locked, or does the initiating process have some say in the matter?
    – Boden
    Oct 29, 2009 at 18:06
  • FF is just written to work that way. It touches a file with the correct name, it then dumps all the incoming data to a .part file, and when done it merges this into the touched file. It all depends on how the handling software has been written.
    – Izzy
    Oct 29, 2009 at 20:09
3

Windows won't let you make a regular copy of the file. You need to use Shadow Copy (aka VSS). Quick Google search produced this example.

1
  • Thanks! I don't actually want the file to be copied if it's not complete though.
    – Boden
    Oct 29, 2009 at 17:47

You must log in to answer this question.

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