+ 1
copy and delete vs move
I need to code a copy action in to my code. Files need to be moved from a file-share to a other location. After the files have been copied. I need to delete / clean / clear the file share to keep this place as empty a possible. What do you prefer to use copy and delete or move I am afraid, that I am gone lose files, when using move. But this is not base on experience because I have never used move.
4 Answers
+ 8
I prefer the rename, copy, rename, delete.
1. Identify source file:
- file1.txt
2. Rename source file with prefix:
_copy_lock_file1.txt
3. Copy renamed source file to new destination using original name:
- //target/file1.txt.
4a. Rename source file using a prefix:
_copy_done_file1.txt
- OR -
4b. Move to a temporary sub directory:
_temp_delete_copy/file1.txt
5. Delete all renamed files and the temp folder - if used. This should only be done once all source files have been copied to the target destination.
This option gives you some flexibility to safely remove the source files only after you've confirmed all files have been copied.
It also allows you to keep track of which files were copied and which are in progress. You can also use this to asynchronously move multiple files via multiple threads without accidentally picking up a file copy in progress.
+ 5
Move doesnât exactly move the file. It actually copies first then deletes the original. It just does it immediately, so there may be times when you want to copy, do something, then delete, otherwise move just does both.
+ 5
When processing multiple files from a file share I'll usually copy and delete, because if the move operation has to abort (fileshare goes offline, script is buggy...), I have half of the data here and half of the data there otherwise.
That's not always an issue but by copying first you can just rerun the script without thinking about it much.
As always it depends on the application I guess. Moving is probably fine if you are processing one file at a time.
+ 1
@David Carroll Thank you. That is a really good way to do it. It also helps me to identify new files when they are comming in to the directory.