Click here to Skip to main content
12,295,252 members (55,139 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: VisualC++
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
 
good
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
  Permalink  
Comments
Sergey Alexandrovich Kryukov 19-Nov-12 18:43pm
   
Right, a 5.
--SA
Espen Harlinn 19-Nov-12 18:46pm
   
Thank you, Sergey :-D
Rate this: bad
 
good
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
	if(fileAtt == 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";
	else
		cout << "test.txt does not exists!\n";
 
	return 0;
}</iostream></windows.h>

use this code, you can ensuare file exist or not. if it is exist
you can use
CFile::Remove
  Permalink  
v2

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


Advertise | Privacy | Mobile
Web02 | 2.8.160525.2 | Last Updated 5 Nov 2012
Copyright © CodeProject, 1999-2016
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