File Management with win32

We can delete a file by calling the DeleteFile() function and providing it with the file name. The DeleteFile() function returns a Boolean value indicating whether or not the file was successfully removed. Remember here that all absolute pathnames start with a drive letter or server name.

#include <Windows.h>
#include <stdlib.h>
#include <stdio.h>

int _tmain(int argc, _TCHAR* argv[])
{
	//only works if a file 
	//named 'testfile.txt' isn't in the current directory
	//share mode is zero
	//so no other process can open this file
	HANDLE hFile = CreateFile(_T("testfile.txt"), GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
	//check for INVALID_HANDLE_VALUE to make sure
	//the CreateFile() function worked
	if(hFile==INVALID_HANDLE_VALUE){
		printf("Error creating file.\n");
		exit(EXIT_FAILURE);
	} else {
		printf("File 'testfile.txt' created.\n");
	}

	//close handle so we can delete the file.
	CloseHandle(hFile);

	if(DeleteFile(_T("testfile.txt"))){
		printf("File deleted.\n");
	} else {
		printf("File not deleted.\n");
	}

	exit(EXIT_SUCCESS);

}

The CopyFile() function can copy an entire file. The CopyFile() function takes three arguments, a pointer to the string containing the name of the source file, a pointer to a string containing the name of the new file, and a Boolean value that states whether the function should fail if the file already exists. We should pass FALSE for the third argument if we only want the file to be copied if the target doesn’t exist.

#include <Windows.h>
#include <stdlib.h>
#include <stdio.h>

int _tmain(int argc, _TCHAR* argv[])
{
	TCHAR szFilename[] = _T("thefile.txt");
	TCHAR szTargetName[] = _T("thefilescopy.txt");

	HANDLE hFHandle = CreateFile(szFilename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

	if(hFHandle==INVALID_HANDLE_VALUE){
		printf("Problem creating file handle.\n");
		exit(EXIT_FAILURE);
	} else {
		_tprintf(_T("File %s created.\n"), szFilename);
		CloseHandle(hFHandle);
	}

	//this should work
	if(CopyFile(szFilename, szTargetName, TRUE)){
		_tprintf(_T("File copied to %s\n"), szTargetName) ;
	} else {
		_tprintf(_T("File not copied to %s\n"), szTargetName) ;
	}

	//this shouldn't work
	if(CopyFile(szFilename, szTargetName, TRUE)){
		_tprintf(_T("File copied to %s\n"), szTargetName) ;
	} else {
		_tprintf(_T("File not copied to %s\n"), szTargetName) ;
	}

	//but this should work
	if(CopyFile(szFilename, szTargetName, FALSE)){
		_tprintf(_T("File copied to %s\n"), szTargetName) ;
	} else {
		_tprintf(_T("File not copied to %s\n"), szTargetName) ;
	}

	//let's delete the copied file
	if(DeleteFile(szTargetName)){
		_tprintf(_T("File %s deleted.\n"), szTargetName);
	}

	exit(EXIT_SUCCESS);

}

Note the use of _tprintf().

We have two functions to move a file. The MoveFile() function takes two arguments and moves a file, but fails if the new file already exists. The MoveFileEx() takes an additional third argument of type DWORD and specifies whether or not to replace an existing file and whether or not to use the CopyFile() with the DeleteFile() function in the case of moving a file to another volume.

#include <Windows.h>
#include <stdio.h>
#include <stdlib.h>


int _tmain(int argc, _TCHAR* argv[])
{
	TCHAR szSource[] = _T("source.txt");
	TCHAR szTarget[] = _T("target.txt");

	HANDLE hFH = CreateFile(szSource, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

	if(hFH!=INVALID_HANDLE_VALUE){
		_tprintf(_T("%s created.\n"), szSource);
		CloseHandle(hFH);
	} else {
		exit(EXIT_FAILURE);
	}

	if(MoveFile(szSource, szTarget)){
		_tprintf(_T("%s moved to %s\n"), szSource, szTarget);
	} else {
		_tprintf(_T("%s not moved to %s\n"), szSource, szTarget);
	}

	if(MoveFileEx(szSource, szTarget, MOVEFILE_REPLACE_EXISTING)){
		_tprintf(_T("%s moved to %s\n"), szSource, szTarget);
	} else {
		_tprintf(_T("%s not moved to %s\n"), szSource, szTarget);
	}

	exit(EXIT_SUCCESS);

}

Note that the MOVEFILE_REPLACE_EXISTING option is used to replace an existing file in the MoveFileEx() function.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s