File Management in Windows using C++

The Windows API contains several file management functions. These functions let you perform many tasks like copying/moving a file, renaming a file and deleting a file.

These functions are listed below :

a) CopyFile () – Used for copying a file
b) MoveFile () – Used for moving/renaming a file
c) MoveFileEx () – Used for moving/renaming a file
d) DeleteFile () – Used for deleting a file

Note :

1. Whenever the term file name or filename is used in this article, it refers to the complete location of a file, including the drive, directory, subdirectories and the file’s actual name. For example, D:\\WCB\\Windows\\HelloWindows.cpp

2. Though in everyday computing, we use the following format to describe a file’s location : D:\abc\xyz\cpp.txt, we shall be using D:\\abc\\xyz\\cpp.txt in our programs, because a single ‘\’ will not work properly. As you already know, we need to write ‘\\’ so that the compiler takes it as ‘\’.

a) CopyFile () – This function is used to create a copy of an existing file. It also copies the metadata of the existing file to the new file.

BOOL CopyFile
(LPCTSTR lpExistingFileName, LPCTSTR lpNewFileName, BOOL fFailIfExists)

Here, the various parameters have the following meanings :

i) lpExistingFileName : This is the name of the existing file.

ii) lpNewFileName : This is the name which would be assigned to the new file. If the location of the existing file and the new file are different, then both files may have the same name, otherwise they would have different names.

For example, if the existing file is D:\\HelloWindows\\OriginalFile.txt and you want to copy it to D:\\HelloCpp\\OriginalFile.txt, then the copy operation would succeed, otherwise if you want to have both files in the same directory i.e. HelloWindows, then you have to use a different name for the new file e.g. D:\\HelloWindows\\NewFile.txt

iii) fFailIfExists : This variable can have either TRUE or FALSE as its value.

If it is TRUE – In this case, if the name assigned to the new file i.e. lpNewFileName is exactly same as an existing file, the Copy operation will fail and the new file will not be created.

If it is FALSE – In this case, if the name assigned to the new file i.e. lpNewFileName is exactly same as an existing file, the Copy operation will replace the existing file.

b) MoveFile () – This function is used for moving/renaming an existing file. Whether this function renames a file or moves it, depends on the values of its parameters.

BOOL MoveFile (LPCTSTR lpExistingFileName, LPCTSTR lpNewFileName)

Here, the various parameters have the following meanings :

i) lpExistingFileName : This is the name of the existing file.

ii) lpNewFileName : This is the name of the new file. If the directory for the file is same, the file is renamed, otherwise it is moved to the directory specified.

For example, if the value of lpExistingFileName is D:\\HelloWindows\\OriginalFile.txt and the value of lpNewFileName is D:\\HelloWindows\\SameFile.txt, then the existing file i.e. OriginalFile.txt will be renamed to SameFile.txt. On the other hand, if the directory in lpNewFileName is different, say D:\\HelloCpp\\Original.txt, then the file will be moved to another folder.

Note : If the filename specified in lpNewFileName is already in use by another file, then the MoveFile operation fails. To fix this problem, use MoveFileEx () instead.

c) MoveFileEx () – This function performs the same task as MoveFile (), but there’s a difference. MoveFileEx () can replace an existing file, while MoveFile () can’t.

BOOL MoveFileEx (LPCTSTR lpExistingFileName, LPCTSTR lpNewFileName, DWORD dwFlags)

Here, the first two parameters have already been explained above (in MoveFile () ).

i) lpExistingFileName
:  Explained in MoveFile ().


ii) lpNewFileName
: Explained in MoveFile ().

iii) dwFlags : This parameter has several possible values. However, the most important one for us is MOVEFILE_REPLACE_EXISTING. It lets us replace existing files.

d) DeleteFile () – This function is used for deleting a file.

BOOL DeleteFile (LPCTSTR lpFileName)

lpFileName is the name of the file we want to delete.

The next article File Management Programs in C++ discusses a few sample programs to perform these tasks.

(Visited 263 times, 1 visits today)

2 comments

Leave a Reply

Your email address will not be published. Required fields are marked *