Delete specific files in a directory

This question is kinda complex any help (example code manly) will really help me!  

1.) First off, does anyone know how I can get the directory that Netscape saves it's cache too??  Is there a reg key??  I know that the defualt is <install dir>\Netscape\user\cache (I think that is it) but what happens if someone changes it??  I know about the reg key that tells me the user's name so that wouldn't be to hard to work with, but if the user changes it then my application can not view the cache.

2.) If the above is posible, how can I load that key into my Borland C++ Builder v1.0 application and delete all of the following files...
*.htm
*.html
*.shtml
*.pl
*.cgi
*.tmp

How do i do that???  PLEASE HELP!!!!!
LVL 1
stevenc317Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RONSLOWCommented:
1) Use REGEDIT to search the registry for that path name on your machine (assume you have netscape installed) and find the name of the key for yourself.

2) You can make a loop using FindFirstFile/FindNextFile with the appropriate wildcard spec (build from the folder path name from the reg and the wilcard spec above, then DeleteFile for each file found in turn
0
stevenc317Author Commented:
tried the first one, couldn't find it THAT IS WHY I ASKED!!!

second, do not know how THAT IS WHY I ASKED!!
0
RONSLOWCommented:
1) If it isn't in the registry, then I suggest that it isn't in the registry.

2) something like this should work...

WIN32_FIND_DATA FindFileData;
HANDLE handle = ::FindFirstFile("<whatever>\*.htm",&FindFileData);
if (handle != INVALID_HANDLE_VALUE) {
  do {
    ::DeleteFile(FindFileData.cFileName);
  } while (::FindNextFile(handle,&FindFileData);
}

0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

q2guoCommented:
Stevenc317

The key should be found at
My Computer\HKEY_CURRENT_USER\Software\Netscape\Netscape Navigator\Cache\


0
4099aolCommented:
RONSLOW the reg key is in  q2quo's comment, how can I get it in the find file thing u made??
0
RONSLOWCommented:
I'm not familiar with BOrlands string library .. but you should be able to read the key in with the registry functions, then append each of your wildcard filespecs in turn to that string and do a loop like the one above.

I cannot go into details at the moment, I need to go to the hospital to visit my partner who is suffering from gall stones :-(

I should be able to give you more info later.

Sorry I cannot be of more help right now.

0
stevenc317Author Commented:
Well I hope your partner gets better soon.  I will reopen the question (to allow a Builder programmer to answer)

thanks!!
0
stevenc317Author Commented:
sorry aol4099 is another one of my logins...  so that was me asking the question...
0
q2guoCommented:
Aol4099

To read from a registry, you do the following

AnsiString s("\\HKEY_CURRENT_USER\\Software\\Netscape\\Netscape Navigator\\Cache");
AnsiString keyname("Cache Dir");
TRegistry myreg;

myreg.OpenKey(s, false);
AnsiString cachepath = ReadString(keyname);

Now, the path of your netscape cache direction will
be store in 'cachepath'


0
stevenc317Author Commented:
any headers that need to be used?? also is this correct??

WIN32_FIND_DATA FindFileData;
HANDLE handle = ::FindFirstFile("cachepath\*.htm",&FindFileData);
if (handle != INVALID_HANDLE_VALUE) {
do {
::DeleteFile(FindFileData.cFileName);
} while (::FindNextFile(handle,&FindFileData);
}
 
0
q2guoCommented:
Where did you get above code?
Does DeleteFile work?
I think, below is what it should be

TSearchRec f;
AnsiString ex("\*.htm");
FindFirst(cachepath+ex, faAnyFile, &f);

remove(f.Name);
while(!FindNext(&f)) {
      remove(f.Name);
}

0
q2guoCommented:
I don't think header files are needed.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RONSLOWCommented:
The 'above code' is Win32 api code to loop through matching files and remove.

DeleteFile should certainly work - it is native windows rather than c runtime.

Your alternative is probably also correct, but apparently uses Borland wrappers for win32 functions (FindFirst,FindNext etc).

0
q2guoCommented:
Don't you my code looks a lot cleaner?
Also, since Stevenc317 is using Borland builder, why not
take advantage of it.
0
q2guoCommented:
Sorry, I meant to say
"Don't you think my code looks a lot cleaner?"
0
stevenc317Author Commented:
quguo....
So this is the correct code??

AnsiString s("\\HKEY_CURRENT_USER\\Software\\Netscape\\Netscape Navigator\\Cache");
AnsiString keyname("Cache Dir");
TRegistry myreg;
myreg.OpenKey(s, false);
AnsiString cachepath = ReadString(keyname);
TSearchRec f;
AnsiString ex("\*.htm");
FindFirst(cachepath+ex, faAnyFile, &f);
remove(f.Name);
while(!FindNext(&f)) {
remove(f.Name);
}
0
q2guoCommented:
Just a few changes

AnsiString s("\\HKEY_CURRENT_USER\\Software\\Netscape\\Netscape Navigator\\Cache");
AnsiString keyname("Cache Dir");
TRegistry myreg;
myreg.OpenKey(s, false);
AnsiString cachepath = myreg.ReadString(keyname);
TSearchRec f;
AnsiString ex("\\*.htm");

FindFirst(cachepath+ex, faAnyFile, &f);
remove(f.Name);
while(!FindNext(&f)) {
        remove(f.Name);
}
0
stevenc317Author Commented:
[C++ Error] Unit1.cpp(30): Unidefined symbol 'TRegistry'.

[C++ Error] Unit1.cpp(30): Statement missing ;.

[C++ Error] Unit1.cpp(31): Unidefined symbol 'myreg'.

[C++ Error] Unit1.cpp(35): Cannot initialize 'Sysutils::TSearchRec &' with 'Sysutils::TSearchRec*.'

[C++ Error] Unit1.cpp(35): Type mismatch in parameter 'F' in call to '_fastcall Sysutils::FindFirst(const System::AnsiString,int,Sysutils::TSearchRec&)'.

[C++ Error] Unit1.cpp(36): Cannot convert 'Sytem::AnsiString'to 'const char*'.

[C++ Error] Unit1.cpp(36): Type mismatch in parameter '__path' in call to 'remove(const char*)'.

There are about 20 more messages......  So please check the codes.
0
q2guoCommented:
Can you go into your include direction in Borland Builder
and tell what files you have got there?
0
q2guoCommented:
Ok, stevenc317
you need
#include <vcl/registry.hpp>
0
stevenc317Author Commented:
still have a ton of errors...
0
q2guoCommented:
What kind of errors?
Same as the errors before?
0
stevenc317Author Commented:
yeah same messages, but by adding the header we took care of about 5-7 messages...
0
q2guoCommented:
Is this line still there?

[C++ Error] Unit1.cpp(30): Unidefined symbol 'TRegistry'.

If not, please post the errors.
0
stevenc317Author Commented:
[C++ Error] Unit1.cpp(31):VCL style classes must be constructed using operator new.

[C++ Error] Unit1.cpp(37): Cannont initialize 'Sysutils::TSearchRec &' with 'Sysutils::TSearchRec *.'

[C++ Error] Unit1.cpp(37): Type mismatch in parameter 'F' in call to '_fastcall Sysutils::FindFirst(const System::AnsiString,int,Sysutils::TSearchRec&)'.

[C++ Error] Unit1.cpp(38): Cannot convert 'System::AnsiString' to 'const char *'.

[C++ Error] Unit1.cpp(38): Type mismatch in parameter '__path' in call to 'remove(const char *).'

[C++ Error] Unit1.cpp(39): Cannot initialize 'Sysutils::TSearchRec &' with 'Sysutils::TSearchRec*.'

[C++ Error] Unit1.cpp(39): Type mismatch in parameter 'F' in call to '_fastcall Sysutils::FindNext(Sysutils::TSearchRec &)'.

[C++ Error] Unit1.cpp(40): Cannot convert 'System::AnisString' to 'const char *.'

[C++ Error] Unit1.cpp(40): Type mismatch in parameter '__path' in call to 'remove(const char*)'.
0
q2guoCommented:
ok, that's good, stevenc317 we are very close now
try this

AnsiString s("\\HKEY_CURRENT_USER\\Software\\Netscape\\Netscape Navigator\\Cache");
AnsiString keyname("Cache Dir");
TRegistry *myreg = new TRegistry;
myreg->OpenKey(s, false);
AnsiString cachepath = myreg->ReadString(keyname);
TSearchRec *f = new TSearchRec;
AnsiString ex("\\*.htm");

FindFirst(cachepath+ex, faAnyFile, f);

remove((f->Name).c_str());
while(!FindNext(f)) {
        remove((f->Name).c_str());
}
0
stevenc317Author Commented:
[C++ Error]Unit1.cpp(37): Cannot initialize 'Sysutils::TSearchRec &' with 'Sysutils::TSearchRec *'.

[C++ Error]Unit1.cpp(37): Type mismatch in parameter 'F' in call to '_fastcall Sysutils::FindFirst(cons System::AnsiString,int,Sysutils::TSearchRec &)'.

[C++ Error]Unit1.cpp(40): Cannot initialize 'Sysutils::TSearchRec &' with 'Sysutils::TSearchRec *'.

[C++ Error]Unit1.cpp(40): Type mismatch in parameter 'F' in call to '_fastcall Sysutils::FindNext(Sysutils::TSearchRec &)'.
0
q2guoCommented:
...

FindFirst(cachepath+ex, faAnyFile, *f);

remove((f->Name).c_str());

while(!FindNext(*f)) {
         remove((f->Name).c_str());
}
0
stevenc317Author Commented:
no error messages, but did not delete the files (*.htm).
0
stevenc317Author Commented:
I think that the reason why my program did not delte the .htm files is that the reg key (that u used) is not in my reg edit.  Whoever sent that tip in, please check your reg again.

thanks
0
q2guoCommented:
Can you use you regedit and manually check to see if
the cache directory is stored in:
By the way, what netscape are you using?
My Computer\HKEY_CURRENT_USER\Software\Netscape\Netscape Navigator\Cache\
0
q2guoCommented:
4099aol

The key could also be in
\\HKEY_USERS\\.default\\Software\\Netscape\\Netscape Navigator\\Cache
0
stevenc317Author Commented:
I do not have that key in my reg!  I have Netscape Communicator v4.04 for Win 95
0
stevenc317Author Commented:
also I have looked at my reg and can not find the path.  please keep looking I am going to ask Netscape...
0
larockdCommented:
Now that the confusion has gone on to long...  I think it is time to explain what is going on...

1.)  Netscape and where does the cache go...  Ok, Netscape assumes if the user has not changed the cache directory manually that the cache dir is called "cache" and it is off the users root dir for netscape.  If the user changes his cache directory, then the new information is stored in the file PREFS.JS under the users dir Example E:\NETSCAPE\USERS\Q2QO\PREFS.JS

The line in the file that holds the info is like this
user_pref("browser.cache.directory", "G:\\TEMP");

You can get a lot of other information out of this file also..  You can use the registry to find out who are valid users, and then go to there directory and read the file to see if the user has changed his cache dir, otherwise you cache...

2.)  Q2 -> also you commented above about how clean your code is..  The code made available above from RONSLOW was solid WIN32 API code that will work on any compiler without any problems..

Steven,
   Hope the information above is helpful...  Your should have no problems from here, Q2 should steer you in the right direction from here...

D.
0
stevenc317Author Commented:
thanks, but how do I get that (info u said in question 1) into my C++ codes??
0
stevenc317Author Commented:
Wait I know what I will do, I will ahve a settings section in my program where the user can type in their directories (that hold the cache) I will save those directories to a reg key, then use the codes to delete the files!  Let me look into that, then I will grade you.

thanks
0
stevenc317Author Commented:
I made a reg key...

Directory1

and updated the codes to point to that key, the value of the key is windows\temp. int. files (I just wantted to type it fast, but the key has the full name).  And it doesn't work, so I am thinking that is your codes or that windows directory will not allow programs to delete cache form it...
0
q2guoCommented:
Which part of it is not working??
Write the registry part, reading the registry part?
I know it 's not the delete part.'

To prove to you it 's not the delete part
try this

SearchRec *f = new TSearchRec;

// put your cache direction path here                    
AnsiString ex("YOU CACHE DIR PATH HERE\\*.htm");

FindFirst(ex, faAnyFile, *f);
remove((f->Name).c_str());

while(!FindNext(*f)) {
     remove((f->Name).c_str());
}
0
stevenc317Author Commented:
it must be the read the reg part...

0
q2guoCommented:
Can you write to the registry ok?
Try use your program to write to the registry, and check the
result using regedit.exe
0
stevenc317Author Commented:
I made the reg key and checked to see if it was there, and it is..


0
q2guoCommented:
How did you make the key???
If you can make a key, then why isn't you other question
titled "Save Edit1 to a reg key??" working?
0
q2guoCommented:
Can you post the part of your code that does
the reading and writing of the registry here?
0
stevenc317Author Commented:
I made the key manualy (from with in regedit.exe).  To open the keu and del the files I am using the codes I have recived from you...
0
q2guoCommented:
Can you tell me where exactly have you added the key and
the code you use for reading it?
0
stevenc317Author Commented:
HKEY_CURRENT_USER\Software\High Voltage\cache can\Directory1


Also I had a computer crash and lost the project.  Do you have C++ Builder?  If so can you just make the C++ file (that will delete .htm, .html (rest r above) and email it to me??  I will give u the 71 points and you name, url, and a quick advert for one of your programs.

PLEASE!!!
0
stevenc317Author Commented:
wait, I found a backup and think I found the bug.

brb
0
stevenc317Author Commented:
Nope still not deleting it.  Here are the codes that I am using....

void __fastcall TForm1::EmptyCan1Click(TObject *Sender)
{
//Delete all .htm files from Directory 1
//AnsiString s("\\HKEY_CURRENT_USER\\Software\\Netscape\\Netscape Navigator\\Cache");

AnsiString s("\\HKEY_CURRENT_USER\\Software\\High Voltage\\cache can\\");

AnsiString keyname("Directory1");
TRegistry *myreg = new TRegistry;
myreg->OpenKey(s, false);
AnsiString cachepath = myreg->ReadString(keyname);
TSearchRec *f = new TSearchRec;
AnsiString ex("\\*.htm");

FindFirst(cachepath+ex, faAnyFile, *f);
remove((f->Name).c_str());
while(!FindNext(*f)) {
remove((f->Name).c_str());
}

//--- ALL FILES HAVE NOW BEEN DELETED - HOPEFULLY :-)!!

//Cache has been deleted dialog
Application->MessageBox("You cache has deleted!", "Cache Can", MB_OK);
}
//----------------------------------------------------------


0
q2guoCommented:
I still can't tell how and where you added in the key manually.
Is it possible for you to open up your regedit, go to
the the key you have just added.  And capture the screen
and attach it to me at q2data@yahoo.com

My main problem is, I don't have Borland Builder.  So, I have
to guess at the mistakes.  You wouldn'y be going through all this
if I had Borland builder.
0
stevenc317Author Commented:
mailed it..
0
q2guoCommented:
replace this line
AnsiString s("\\HKEY_CURRENT_USER\\Software\\High Voltage\\cache can\\");

with

AnsiString s("Software\\High Voltage\\cache can\\");

also can you insert
Application->MessageBox(cachepath+ex, "Cache Can", MB_OK);

after this line

AnsiString ex("\\*.htm");

to see what path we are actually getting?
0
stevenc317Author Commented:
the message box doesn't work..
0
q2guoCommented:
try this
Application->MessageBox(cachepath.c_str(), "Cache Can", MB_OK);
0
stevenc317Author Commented:
ok the dialog comes up and it is blank (only has an ok button).  So it is not reading the reg key!!!!!
0
stevenc317Author Commented:
Do you think you will be able to fix it?? Also what programming language do u use???
0
q2guoCommented:
try this


AnsiString s("\\HKEY_CURRENT_USER\\Software\\High Voltage\\cache can");
0
q2guoCommented:
I use borland C 3.1 and Visual C++
0
stevenc317Author Commented:
still blank...
0
q2guoCommented:
try this


AnsiString s("\\Software\\High Voltage\\cache can");
0
stevenc317Author Commented:
ok it is showing the correct path, but is not deleting the files...
0
q2guoCommented:
What exactly is it showing?
0
stevenc317Author Commented:
c:\windows\Temporary Internet Files

I was using that as a test.
0
q2guoCommented:
A few things to check

Is the message box showing
c:\windows\Temporary Internet Files\
or
c:\windows\Temporary Internet Files
This is important

Make sure your have file with the .htm extension
in this temp directory

Also add
Application->MessageBox((f->Name).c_str(), "Cache Can", MB_OK);
after line
FindFirst(cachepath+ex, faAnyFile, *f);
and see what the messagebox says
0
stevenc317Author Commented:
c:\windows\Temporary Internet Files\

and yes there are .htm files in that directory...
0
q2guoCommented:
so, it 's
c:\windows\Temporary Internet Files\
ok then the line
AnsiString ex("\\*.htm");
should be just
AnsiString ex("*.htm");

That's it!!
0
stevenc317Author Commented:
Ok
Application->MessageBox((f->Name).c_str(), "Cache Can", MB_OK);

Is producing a blank dialog box (only has an ok button).
0
q2guoCommented:
hi did you try my previous comment
0
stevenc317Author Commented:
which comment (they are about 100).  I am sure I did, thou since I have done them all.


0
q2guoCommented:
This one
       
       
                     AnsiString ex("\\*.htm");
                     should be just
                     AnsiString ex("*.htm");


0
stevenc317Author Commented:
do u think u can find the bug????
0
stevenc317Author Commented:
let me try that. sorry... jsut forget about the last comment!!
0
stevenc317Author Commented:
nope still blank
0
q2guoCommented:
Can you post your code here again, so I can check it.
Everything should be working fine.
0
q2guoCommented:
Did you add
Application->MessageBox((f->Name).c_str(), "Cache Can", MB_OK);
after line
AnsiString ex("*.htm");
or before?

Can also check again to see if *.htm files are still in
c:\windows\Temporary Internet Files\
0
stevenc317Author Commented:
after.

and yip there are still .htm files (and I checked they r .htm not .html) in that directory.
0
stevenc317Author Commented:
also here are the codes that I am using (just in case I skipped something).

//Delete all .htm files from Directory 1
//AnsiString s("\\HKEY_CURRENT_USER\\Software\\High Voltage\\cache can\\");
AnsiString s("\\Software\\High Voltage\\cache can");
AnsiString keyname("Directory1");
TRegistry *myreg = new TRegistry;
myreg->OpenKey(s, false);
AnsiString cachepath = myreg->ReadString(keyname);
TSearchRec *f = new TSearchRec;
AnsiString ex("*.htm");
//    Application->MessageBox(cachepath.c_str(), "Cache Can", MB_OK);

FindFirst(cachepath+ex, faAnyFile, *f);
Application->MessageBox((f->Name).c_str(), "Cache Can", MB_OK);
remove((f->Name).c_str());
while(!FindNext(*f)) {
remove((f->Name).c_str());


0
q2guoCommented:
...

AnsiString cachepath = myreg->ReadString(keyname);
TSearchRec sr;
AnsiString ex("*.htm");
// Application->MessageBox(cachepath.c_str(), "Cache Can", MB_OK);
AnsiString Cachefile = cachepath+ex;

// tell me what this message box prints out
Application->MessageBox(Cachefile.c_str(), "Cache Can", MB_OK);
FindFirst(Cachefile, faAnyFile, sr);
Application->MessageBox((sr.Name).c_str(), "Cache Can", MB_OK);
remove((sr.Name).c_str());

while(!FindNext(sr)) {
  remove((sr.Name).c_str());
}

FindClose(sr);

0
stevenc317Author Commented:
the box u asked about was blank
0
q2guoCommented:
Two tings to change

add
#include <vcl/dstring.h>

change this line
AnsiString Cachefile = cachepath+ex;
to
AnsiString Cachefile = cachepath.c_str() + ex;

Tell what that message box prints now
0
stevenc317Author Commented:
still blank...
0
q2guoCommented:
Can you tell me what each of the message box prints
you have three message boxes

0
4099aolCommented:
In order....

c:\windows\Temorary Internet Files\*.htm

blank

Your cache ahs now been deleted.

0
4099aolCommented:
oops.
Your cache has now been deleted.
0
q2guoCommented:
****, I know what 's wrong now!!!!

When you added in your key manually, you spelled
the directory name wrong
It 's
c:\windows\Temporary Internet Files\
not
c:\windows\Temorary Internet Files\

TRY AGAIN
0
stevenc317Author Commented:
nope, I checked.  I just spelled it wrong in my comment.

spelled right in regedit...

:-(
0
q2guoCommented:
replace
FindFirst(Cachefile, faAnyFile, sr);
with
FindFirst(Cachefile, faReadOnly, sr);

And if it still does not work
Can you try using directory in the registry
C:\windows\temp\
instead of
C:\windows\temporary Internet Files\
just to see what happens
0
stevenc317Author Commented:
Ok.  I changed the code, still had the blank dialog box.  But when i changed c:\windows\Temporary Internet Files\ to c:\windows\temp\ it said netscape.htm and deleted it.  So I think for some reason is that, your code does not like logn file names!
0
stevenc317Author Commented:
wait, nope it did not delete it.  But it did file the file!
0
stevenc317Author Commented:
Sorry I mean it did find the file but did not delete it...
0
q2guoCommented:

change all lines

remove((sr.Name).c_str());  

to

::DeleteFile(sr.Name);
0
stevenc317Author Commented:
have a lot of error messages..
0
q2guoCommented:
ok, change it back
I will look for something else.

0
stevenc317Author Commented:
ok
0
q2guoCommented:
Can you log on to IRC?  So, We can chat live.
I HAVE TO get your program to work, both of us
have spent too much time.
If you can, please log on to EFNET, channel BB1Q
0
stevenc317Author Commented:
I do not have IRC, I have ICQ.
0
q2guoCommented:
icq 's good
my icq number is 933472
0
stevenc317Author Commented:
Ok I have just added you to my contact list, please login!
0
stevenc317Author Commented:
thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.