Click here to Skip to main content
12,954,601 members (56,796 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Hi all ,

I want to check if the file at given path exists and if it does then want to delete this it .
How can i do this .

Thnx all
Posted 4-Nov-12 23:25pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

consider the following example
#include <windows.h>
#include <iostream>
using namespace std;
bool exists(char* filePath)
	//This will get the file attributes bitlist of the file
	DWORD fileAtt = GetFileAttributesA(filePath);
	//If an error occurred it will equal to INVALID_FILE_ATTRIBUTES
		//So lets throw an exception when an error has occurred
		throw GetLastError();
	//If the path referers to a directory it should also not exists.
	return ( ( fileAtt & FILE_ATTRIBUTE_DIRECTORY ) == 0 ); 
int main()
	if (exists("test.txt"))
		cout << "test.txt exists!\n";
		cout << "test.txt does not exists!\n";
	return 0;

use this code, you can ensuare file exist or not. if it is exist
you can use
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

You can use _stat[^] to check whether the file exists or not. If it does, then remove[^] will delete the file.

Best regards
Espen Harlinn
Sergey Alexandrovich Kryukov 19-Nov-12 18:43pm
Right, a 5.
Espen Harlinn 19-Nov-12 18:46pm
Thank you, Sergey :-D

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 6,539
CHill60 3,490
Maciej Los 3,153
ppolymorphe 2,030
Jochen Arndt 1,975

Advertise | Privacy | Mobile
Web02 | 2.8.170525.1 | Last Updated 5 Nov 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100