Rahul
Rahul

Reputation: 2234

How to copy a file to the same directory (win32)

Lets say I have a file called f1.txt in a directory D1.
If I cut paste this file in explorer I get a nice file called f1-(Copy).txt. I think in earlier version I would get f1(2).txt.
Anyway the point is can I do this programmatically given just the file path in Win32. Can I say Something like CopyFileWithCleverDupNamingScheme. I don't want to write my own duplicate naming scheme essentially. And any such api needs to return the new name obviously.
TIA

Upvotes: 1

Views: 764

Answers (2)

Alex K.
Alex K.

Reputation: 175766

You can use IFileOperation::CopyItem with SetOperationFlags and FOF_RENAMEONCOLLISION.

Upvotes: 3

user1182183
user1182183

Reputation:

Use the CopyFile function from WinAPI. It's self-exlainatory :)

BOOL WINAPI CopyFile(
  _In_  LPCTSTR lpExistingFileName,
  _In_  LPCTSTR lpNewFileName,
  _In_  BOOL bFailIfExists
);

You can just append .new to the old filename?

You will have to check if any files of such already exists, but it's not that hard, just check if the file you want to write already exists, if yes, add some number and keep increasing until a filename is foud which is not used.

For that you can use FindFirstFile in combination with FindNextFile

Upvotes: 1

Related Questions