Take the 2-minute tour ×

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?

share|improve this question
add comment

4 Answers

up vote -1 down vote accepted

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

share|improve this answer
Thank you, this is the answer. I actually thought the post was real (still learning lots!) –  Dave Rook 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. blogs.msdn.com/b/ericlippert/archive/2012/05/07/… –  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
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
show 3 more comments

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


share|improve this answer
Because I didn't know of it :) Thank you (+1) this is great... So much framework, so little time... –  Dave Rook Oct 14 '12 at 8:42
add comment

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.

share|improve this answer
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 Rook Oct 13 '12 at 15:51
add comment

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.


if your friend.

share|improve this answer
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 Rook Oct 13 '12 at 15:42
add comment

Your Answer


