Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VC++
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 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
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 at 19-Nov-12 18:43pm
   
Right, a 5.
--SA
Espen Harlinn at 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
0 OriginalGriff 223
1 Sergey Alexandrovich Kryukov 181
2 nv3 50
3 Mehdi Gholam 45
4 VJ Reddy 45
0 Sergey Alexandrovich Kryukov 6,696
1 OriginalGriff 6,149
2 CPallini 2,473
3 Richard MacCutchan 1,697
4 Abhinav S 1,560


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