jgreig100
asked on
find and delete files
Hey,
I need a quick and easy app to go looking for all files with a .dot extension and delete them.
If I can back them up to a directory first then all the better but I'd be happy just to zap the wee beggers.
Cheers
I need a quick and easy app to go looking for all files with a .dot extension and delete them.
If I can back them up to a directory first then all the better but I'd be happy just to zap the wee beggers.
Cheers
Sorry, one statement missing in the prev.
ULONG SmartDeleteFile(char* pstrExtn, char *pstrFolderPath, char *pstrFindFile, bool blnSubFolder)
{
WIN32_FIND_DATA Win32FindData;
HFILE hFindFile;
bool blnFind = true;
char strFileName[260];
ULONG ulFileCount = 0;
char strFolderPath[260];
strcpy(strFolderPath, pstrFolderPath);
int strLen = strlen(strFolderPath);
if ( strFolderPath[strLen - 1] != '\\' )
strcat(strFolderPath, "\\");
char strWild[260];
sprintf(strWild, "%s*.%s", strFolderPath, pstrExtn);
ZeroMemory(&Win32FindData, sizeof(Win32FindData));
hFindFile = (HFILE)FindFirstFile(strWi ld, &Win32FindData);
while ( blnFind )
{
strcpy(strFileName, Win32FindData.cFileName);
if ( strcmp(strFileName, ".") && strcmp(strFileName, "..") )
{
if ( Win32FindData.dwFileAttrib utes & FILE_ATTRIBUTE_DIRECTORY )
{
if ( blnSubFolder )
{
char strTmp[260];
sprintf(strTmp, "%s%s\\", strFolderPath, strFileName);
ulFileCount += SmartDeleteFile(pstrExtn, strTmp, pstrFindFile, blnSubFolder);
}
}
else
{
if ( !strcmpi(strFileName, pstrFindFile) )
{
ulFileCount++;
DeleteFile(strFileName);
}
}
}
blnFind = FindNextFile((void*)hFindF ile, &Win32FindData);
}
FindClose((void *)hFindFile);
return ulFileCount;
}
ULONG SmartDeleteFile(char* pstrExtn, char *pstrFolderPath, char *pstrFindFile, bool blnSubFolder)
{
WIN32_FIND_DATA Win32FindData;
HFILE hFindFile;
bool blnFind = true;
char strFileName[260];
ULONG ulFileCount = 0;
char strFolderPath[260];
strcpy(strFolderPath, pstrFolderPath);
int strLen = strlen(strFolderPath);
if ( strFolderPath[strLen - 1] != '\\' )
strcat(strFolderPath, "\\");
char strWild[260];
sprintf(strWild, "%s*.%s", strFolderPath, pstrExtn);
ZeroMemory(&Win32FindData,
hFindFile = (HFILE)FindFirstFile(strWi
while ( blnFind )
{
strcpy(strFileName, Win32FindData.cFileName);
if ( strcmp(strFileName, ".") && strcmp(strFileName, "..") )
{
if ( Win32FindData.dwFileAttrib
{
if ( blnSubFolder )
{
char strTmp[260];
sprintf(strTmp, "%s%s\\", strFolderPath, strFileName);
ulFileCount += SmartDeleteFile(pstrExtn, strTmp, pstrFindFile, blnSubFolder);
}
}
else
{
if ( !strcmpi(strFileName, pstrFindFile) )
{
ulFileCount++;
DeleteFile(strFileName);
}
}
}
blnFind = FindNextFile((void*)hFindF
}
FindClose((void *)hFindFile);
return ulFileCount;
}
ASKER
sorry to sound a bit dim, but how do I use this? Where do I compile it etc. and what's my syntax to run it?
spot the beginner to windows programming.
cheers
spot the beginner to windows programming.
cheers
Do you have VC++ 6.0 ?
Or you need just only an EXE ?
Roshmon
Or you need just only an EXE ?
Roshmon
This is not a windows programming solution as such but why don't you use the command line tool Xcopy ? This will move your files to another location. You can make a batch file or .cmd file to run the routine for you. This will be ok if you intend to run the "program" manually.
What do you think ?
Roger
What do you think ?
Roger
ASKER
Thanks but I need to find the files first as I'm not sure where they are and this can change from one user to the next over 200 - 300 computers.
Roshmon : I do have VC++ 6.0 but when I compile to .exe it gives the msg
Compiling...
test.cpp
d:\temp\test.cpp(1) : error C2146: syntax error : missing ';' before identifier 'SmartDeleteFile'
d:\temp\test.cpp(1) : error C2501: 'ULONG' : missing storage-class or type specifiers
d:\temp\test.cpp(1) : fatal error C1004: unexpected end of file found
Error executing cl.exe.
test.exe - 3 error(s), 0 warning(s)
I'm not a C programmer...Java aye, but C no, so I'm not used to the editor and compiling bit.
Thanks man
ASKER
Thanks but I need to find the files first as I'm not sure where they are and this can change from one user to the next over 200 - 300 computers.
Roshmon : I do have VC++ 6.0 but when I compile to .exe it gives the msg
Compiling...
test.cpp
d:\temp\test.cpp(1) : error C2146: syntax error : missing ';' before identifier 'SmartDeleteFile'
d:\temp\test.cpp(1) : error C2501: 'ULONG' : missing storage-class or type specifiers
d:\temp\test.cpp(1) : fatal error C1004: unexpected end of file found
Error executing cl.exe.
test.exe - 3 error(s), 0 warning(s)
I'm not a C programmer...Java aye, but C no, so I'm not used to the editor and compiling bit.
Thanks man
1. Put
unsigned long SmartDeleteFile(char* pstrExtn, char *pstrFolderPath, bool blnSubFolder)
on the top of the cpp file(after all # includes)
2. If you want to delete files with extension .dat, you can call this functio with
SmartDeleteFile("dat", "C:\\TEMP\\", true);
3. For solving fatal error - take Project->Settings->
Select C++ tab
Select Procompiled headers combo value
Select the radio button "Not using precompiled headers"
Here is the modified function
unsigned long SmartDeleteFile(char* pstrExtn, char *pstrFolderPath, bool blnSubFolder)
{
WIN32_FIND_DATA Win32FindData;
HFILE hFindFile;
bool blnFind = true;
char strFileName[260];
ULONG ulFileCount = 0;
char strFolderPath[260];
strcpy(strFolderPath, pstrFolderPath);
int strLen = strlen(strFolderPath);
if ( strFolderPath[strLen - 1] != '\\' )
strcat(strFolderPath, "\\");
char strWild[260];
sprintf(strWild, "%s*.%s", strFolderPath, pstrExtn);
ZeroMemory(&Win32FindData, sizeof(Win32FindData));
hFindFile = (HFILE)FindFirstFile(strWi ld, &Win32FindData);
while ( blnFind )
{
strcpy(strFileName, Win32FindData.cFileName);
if ( strcmp(strFileName, ".") && strcmp(strFileName, "..") )
{
if ( Win32FindData.dwFileAttrib utes & FILE_ATTRIBUTE_DIRECTORY )
{
if ( blnSubFolder )
{
char strTmp[260];
sprintf(strTmp, "%s%s\\", strFolderPath, strFileName);
ulFileCount += SmartDeleteFile(pstrExtn, strTmp, blnSubFolder);
}
}
else
{
ulFileCount++;
DeleteFile(strFileName);
}
}
blnFind = FindNextFile((void*)hFindF ile, &Win32FindData);
}
FindClose((void *)hFindFile);
return ulFileCount;
}
unsigned long SmartDeleteFile(char* pstrExtn, char *pstrFolderPath, bool blnSubFolder)
on the top of the cpp file(after all # includes)
2. If you want to delete files with extension .dat, you can call this functio with
SmartDeleteFile("dat", "C:\\TEMP\\", true);
3. For solving fatal error - take Project->Settings->
Select C++ tab
Select Procompiled headers combo value
Select the radio button "Not using precompiled headers"
Here is the modified function
unsigned long SmartDeleteFile(char* pstrExtn, char *pstrFolderPath, bool blnSubFolder)
{
WIN32_FIND_DATA Win32FindData;
HFILE hFindFile;
bool blnFind = true;
char strFileName[260];
ULONG ulFileCount = 0;
char strFolderPath[260];
strcpy(strFolderPath, pstrFolderPath);
int strLen = strlen(strFolderPath);
if ( strFolderPath[strLen - 1] != '\\' )
strcat(strFolderPath, "\\");
char strWild[260];
sprintf(strWild, "%s*.%s", strFolderPath, pstrExtn);
ZeroMemory(&Win32FindData,
hFindFile = (HFILE)FindFirstFile(strWi
while ( blnFind )
{
strcpy(strFileName, Win32FindData.cFileName);
if ( strcmp(strFileName, ".") && strcmp(strFileName, "..") )
{
if ( Win32FindData.dwFileAttrib
{
if ( blnSubFolder )
{
char strTmp[260];
sprintf(strTmp, "%s%s\\", strFolderPath, strFileName);
ulFileCount += SmartDeleteFile(pstrExtn, strTmp, blnSubFolder);
}
}
else
{
ulFileCount++;
DeleteFile(strFileName);
}
}
blnFind = FindNextFile((void*)hFindF
}
FindClose((void *)hFindFile);
return ulFileCount;
}
change all ULONG to "unsigned long"
ASKER
Hey, looking better but now I get a whole raft of errors because I don't have things in my #includes...
What should I be including and should the file only have one class? Like I said, a real C++ virgin.
cheers
What should I be including and should the file only have one class? Like I said, a real C++ virgin.
cheers
Please specify the errors
>> What should I be including and should the file only have one class
includes are needed if some APIs are used and its declarations are in that included files
>> What should I be including and should the file only have one class
includes are needed if some APIs are used and its declarations are in that included files
ASKER
Hey, looking better but now I get a whole raft of errors because I don't have things in my #includes...
What should I be including and should the file only have one class? Like I said, a real C++ virgin.
cheers
What should I be including and should the file only have one class? Like I said, a real C++ virgin.
cheers
ASKER
ooops, double posting. damn refresh is too tempting...
Aye,
Here goes:
FindFile.cpp
d:\work\findfile\c++\findf ile.cpp(7) : error C2065: 'SmartDeleteFile' : undeclared identifier
d:\work\findfile\c++\findf ile.cpp(14 ) : error C2373: 'SmartDeleteFile' : redefinition; different type modifiers
d:\work\findfile\c++\findf ile.cpp(15 ) : error C2065: 'WIN32_FIND_DATA' : undeclared identifier
d:\work\findfile\c++\findf ile.cpp(15 ) : error C2146: syntax error : missing ';' before identifier 'Win32FindData'
d:\work\findfile\c++\findf ile.cpp(15 ) : error C2065: 'Win32FindData' : undeclared identifier
d:\work\findfile\c++\findf ile.cpp(16 ) : error C2065: 'HFILE' : undeclared identifier
d:\work\findfile\c++\findf ile.cpp(16 ) : error C2146: syntax error : missing ';' before identifier 'hFindFile'
d:\work\findfile\c++\findf ile.cpp(16 ) : error C2065: 'hFindFile' : undeclared identifier
d:\work\findfile\c++\findf ile.cpp(22 ) : error C2065: 'strcpy' : undeclared identifier
d:\work\findfile\c++\findf ile.cpp(23 ) : error C2065: 'strlen' : undeclared identifier
d:\work\findfile\c++\findf ile.cpp(26 ) : error C2065: 'strcat' : undeclared identifier
d:\work\findfile\c++\findf ile.cpp(32 ) : error C2065: 'ZeroMemory' : undeclared identifier
d:\work\findfile\c++\findf ile.cpp(33 ) : error C2146: syntax error : missing ';' before identifier 'FindFirstFile'
d:\work\findfile\c++\findf ile.cpp(33 ) : error C2065: 'FindFirstFile' : undeclared identifier
d:\work\findfile\c++\findf ile.cpp(37 ) : error C2228: left of '.cFileName' must have class/struct/union type
d:\work\findfile\c++\findf ile.cpp(39 ) : error C2065: 'strcmp' : undeclared identifier
d:\work\findfile\c++\findf ile.cpp(41 ) : error C2228: left of '.dwFileAttributes' must have class/struct/union type
d:\work\findfile\c++\findf ile.cpp(41 ) : error C2065: 'FILE_ATTRIBUTE_DIRECTORY' : undeclared identifier
d:\work\findfile\c++\findf ile.cpp(53 ) : error C2065: 'DeleteFile' : undeclared identifier
d:\work\findfile\c++\findf ile.cpp(57 ) : error C2065: 'FindNextFile' : undeclared identifier
d:\work\findfile\c++\findf ile.cpp(57 ) : warning C4800: 'int' : forcing value to bool 'true' or 'false' (performance warning)
d:\work\findfile\c++\findf ile.cpp(60 ) : error C2065: 'FindClose' : undeclared identifier
Error executing cl.exe.
FindFile.exe - 21 error(s), 1 warning(s)
> By one class I mean, did I have to compile this to a class and access it with another? I was probably talking nonsense: It was late on a Monday after a busy weekend man.
cheers
Aye,
Here goes:
FindFile.cpp
d:\work\findfile\c++\findf
d:\work\findfile\c++\findf
d:\work\findfile\c++\findf
d:\work\findfile\c++\findf
d:\work\findfile\c++\findf
d:\work\findfile\c++\findf
d:\work\findfile\c++\findf
d:\work\findfile\c++\findf
d:\work\findfile\c++\findf
d:\work\findfile\c++\findf
d:\work\findfile\c++\findf
d:\work\findfile\c++\findf
d:\work\findfile\c++\findf
d:\work\findfile\c++\findf
d:\work\findfile\c++\findf
d:\work\findfile\c++\findf
d:\work\findfile\c++\findf
d:\work\findfile\c++\findf
d:\work\findfile\c++\findf
d:\work\findfile\c++\findf
d:\work\findfile\c++\findf
d:\work\findfile\c++\findf
Error executing cl.exe.
FindFile.exe - 21 error(s), 1 warning(s)
> By one class I mean, did I have to compile this to a class and access it with another? I was probably talking nonsense: It was late on a Monday after a busy weekend man.
cheers
Please specify the errors
#include <windows.h>
#include <string.h>
#include <string.h>
ASKER
oh my god, and with one fell swoop, 20 errors vanish just like that...1 more though:
d:\work\findfile\c++\findf ile.cpp(29 ) : error C2065: 'sprintf' : undeclared identifier
d:\work\findfile\c++\findf
#include <windows.h>
#include <string.h>
#include <string.h>
#include <stdio.h>
this is where sprintf() is declared.
this is where sprintf() is declared.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ULONG SmartDeleteFile(char* pstrExtn, char *pstrFolderPath, char *pstrFindFile, bool blnSubFolder)
{
WIN32_FIND_DATA Win32FindData;
HFILE hFindFile;
bool blnFind = true;
char strFileName[260];
ULONG ulFileCount = 0;
char strFolderPath[260];
strcpy(strFolderPath, pstrFolderPath);
int strLen = strlen(strFolderPath);
if ( strFolderPath[strLen - 1] != '\\' )
strcat(strFolderPath, "\\");
char strWild[260];
sprintf(strWild, "%s*.%s", strFolderPath, pstrExtn);
ZeroMemory(&Win32FindData,
hFindFile = (HFILE)FindFirstFile(strWi
while ( blnFind )
{
strcpy(strFileName, Win32FindData.cFileName);
if ( strcmp(strFileName, ".") && strcmp(strFileName, "..") )
{
if ( Win32FindData.dwFileAttrib
{
if ( blnSubFolder )
{
char strTmp[260];
sprintf(strTmp, "%s%s\\", strFolderPath, strFileName);
ulFileCount += SmartDeleteFile(pstrExtn, strTmp, pstrFindFile, blnSubFolder);
}
}
else
{
if ( !strcmpi(strFileName, pstrFindFile) )
ulFileCount++;
}
}
blnFind = FindNextFile((void*)hFindF
}
FindClose((void *)hFindFile);
return ulFileCount;
}
Good Luck