0

I am creating a back up solution. I doubt there is anything new in what I'm trying to achieve.

Before copying the file I want to take a backup of the destination file in case anything becomes corrupt. This means renaming the file.

I have to be careful when renaming in case the file already exists and adding a 01 to the end is not safe.

My question is, based upon not finding the answer else where, would adding a GUID to the file name work. So, if my file was called file01.txt, renaming the file to file01.txtGUID (where GUID is the generated GUID), I could then perform my back up of that file (at this instance having 2 back ups) and then, after ensuring the file has copied (by comparing length of file to the source), delete the file with the GUID in the name.

I know the GUID is not 100% guaranteed to be unique but would this suffice?

Community
  • 1
  • 1
Dave
  • 8,163
  • 11
  • 67
  • 103

4 Answers4

3

Just get a GUID, then ask the destination OS if name+GUID exists. If it does, then pick a new GUID and try again. You are going to delete the name+GUID file anyway, so who cares if you can't pick a unique filename on the first try.

I think you might be focusing on the wrong problems given the risk and impact.

What if you don't have disk space to make two backups on the destination system?

What if the filename + path is too long for the destination OS to handle?

What is someone else modifies the file in the period of time between when you get the name and try to perform an operation of time on the file?

Writing defensive code is about thinking about risks, but don't drive yourself crazy that you focus on less likely or nearly impossible scenarios.

Jason
  • 1,385
  • 9
  • 11
  • I agree, - disk space could be an issue and I have thought about that and since it's only on W7 I was hoping the GUID would not be a problem but I am grateful for your comments. At the moment, it's an educational project I've set myself but if I wanted to expand on it, I would have to consider these points. Thank you +1 – Dave Oct 13 '12 at 15:51
2

Why don't you just use GetTempFileName()? That's what it's for;

http://msdn.microsoft.com/en-us/library/system.io.path.gettempfilename.aspx

cirrus
  • 5,624
  • 8
  • 44
  • 62
  • Because I didn't know of it :) Thank you (+1) this is great... So much framework, so little time... – Dave Oct 14 '12 at 08:42
1

Yes this would suffice. Nothing is impossible via quantum mechanics, and in theory in a million year you might be able to reproduce a GUID via chance, but as you're also adding the name of the file, so it's even more impossible. You could of course also add the filesize in byte, or a hash of the file, but remember that on Windows the length of a path is not infinite.

Guid.NewGuid()

if your friend.

Akku
  • 4,373
  • 4
  • 48
  • 67
  • Thank you - Yes, this was my thought as well but I've had many thoughts only to be wrong :) thank you for taking the time – Dave Oct 13 '12 at 15:42
-1

It is globally unique, unique in the universe. The post you are citing is a joke.

Mark Sowul
  • 10,244
  • 1
  • 45
  • 51
  • Thank you, this is the answer. I actually thought the post was real (still learning lots!) – Dave Oct 13 '12 at 15:37
  • There are no such guarantees about it being unique. They're unique enough to consider unique, and would be fine for this purpose, but they most certainly can't be unique in the universe since they're based on random numbers. MS use V4 of the Guid spec. The docs make no such guarantees. – cirrus Oct 13 '12 at 17:20
  • They are probabilistically unique. If you want to quibble about the .00000000000000000000000000000000001% probability of a collision, go for it, but I'm not going to waste my time worrying about that. http://blogs.msdn.com/b/ericlippert/archive/2012/05/07/guid-guide-part-three.aspx – Mark Sowul Oct 13 '12 at 17:54
  • True. But your response was unnecessarily rude I felt, so I felt like being pedandtic. IMHO it's also the least helpful answer here of all and undeserving of the accept. – cirrus Oct 13 '12 at 18:13
  • Rude how? The question is based on a false premise, that GUIDs aren't unique, because the cited post is a joke that was taken as serious. – Mark Sowul Oct 13 '12 at 18:16
  • 2
    I don't think anyone though it was a joke, including yourself, and there was other context to the question which has sparked some more useful suggestions. The guy was just asking for some clarification. Your terse response was unhelpful and largely valueless. I think you just humiliated the poor guy into accepting, but not because it was a useful response compared to any other here. – cirrus Oct 13 '12 at 22:45
  • I clarified that his GUIDs will be unique. The post that OP cited, http://stackoverflow.com/questions/1705008, is a joke! I don't know what else to tell you. Do you see all the things about heat death of the universe, waiting several trillion years, etc? Do you understand the math behind GUIDs? – Mark Sowul Oct 14 '12 at 01:50
  • If not rude, certainly abrupt, but well done trying not being rude in your comments here. – cirrus Oct 15 '12 at 11:11