Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

FileExists? 2

Status
Not open for further replies.

squirleegirl

Programmer
Jun 24, 2003
59
US
Greetings! I'm a novice at VB, so be patient with me =-)

I've got a program that searches for a file called flag.txt. This file can be in any folder. If this file(s)is found, then the program gathers the information in the files and sends the info out in an email.

Currently, I've got the file structure partially hard-coded and the rest coming from a value I've pulled from a database (looping through). Here is my code:

fso.FileExists("\\server\share\folder\" & rst("var") & "\flag.txt")

I want to change this so that it will search every file, not just those with a value from the database (this is because some students have found a way to create fake logins to take the tests and it doesn't get recorded under their name if they fail =-)

Since every student has a folder, I used the values of the database to complete the file name; however, I don't know how to complete it for those names not in the database.

Any suggestions?

Thanks in advance,
Squirleegirl
 
First of all, I'd be concerned about how your students are getting around the security on your database... If they have an exploit for creating fake logins, how long before they find a way to amend the results that I presume are stored in your database?

mmilan
 
Actually, they aren't compromising the database. The system in which they login into to do the training doesn't put all the information into a database. It simply creates a folder for the student. Each time they review a lesson or take a test, then it adds information to their folder.

It's not the way I would have designed it, but nonetheless, that is how it works.

 
I guess you should probably be past the 'novice stage' now, as you've been saying that for a year now in these forums! You should be ready for the (slightly) harder stuff.

You can use the FindFirstFile and FindNextFile API to search throughout your drives. Have a look at:


for an example

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'
 
Well, as I only do VB once in awhile, I consider myself to still be novice.

However, as to what I feel my skills/skill level (or lack thereof) to be is not really an appropriate subject for your reply. See, I didn't ask about what you thought about my abilities....I asked about VB.

But, thanks anyways.
 
Ahem, and moving along...

Okay - functions you will probably want to read up on are dir() and getattr().

dir() basically looks for a file matching a mask that you supply, and returns the first file it finds. You can then call it again with no arguments, and it returns the 2nd, etc. etc...

When it runs out of matching files, it simply returns an empty string, which of course you will have to test for.

Here's the kicker though - as far as dir() is concerned, there is no difference between files and directories. It will happily find the name of either!

You'll probably want to use recursion to search down a directory tree, so knowing what is a file and what is a directory is important. You can find out by calling getattr() with the name of an item you're interested in.

It essentially returns a bit field, so you need to test if the bit representing a directory is set. This can be achieved like this:

if (getattr("whateverIfound") and 16) = 16 then
' we're looking at a directory
else
' we're looking at a file
end if

Given your (and I mean no disrespect) level of knowledge in VB, I would go this way as opposed to using the API, but that's just me...

mmilan
 
I should just acknowledge however that Johnwm's proposal is the one that I would use were I doing this myself - I'm just not sure if you are up to the API level...

In defence of Johnwm as well, though I know his post reads bad, I suspect it is intended with the best of intentions. He's trying to push you to move forward.

I've been a member of this forum for a number of years now, and although I was already fairly experienced in other langauges before getting tangled up in VB, I've found "playing around with it" to be a great way of learning...

 
Sorry if you took any offence at my post. None was intended. I thought to offer you a slightly more sophisticated solution as you've now been with us for a while.

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'
 
I have looked into some stuff using dir() but keep getting 'Cannot find Library or Reference' errors. This is a common problem and I have searched for book/listing/reference/etc. that details what libraries/references are needed for certain methods and functions to work, but have yet to find anything. I can find bits and pieces, but nothing that I can actually pick up and read about the components that are available. But, that's a different issue.

I would love to say I am not a novice anymore. I've only been out of school for a year now and as I'm sure most people know - I didn't really learn anything that I really am needing to know =-) This is the only app that runs in VB and unfortunately I don't really have too much time to devote to learning this language as I don't use it as much as others (which I am having to learn on the fly, too!).

I am a fairly slow learner, but hopefully in future years I will be as knowledgeable as those who provide the much needed tips found on this forum and could possibly...just possibly provide assistance to others. However, I am not there yet.

Thanks for the posts. I will try and see what I can come up with. No hard feelings.

Squirleegirl
 
Interesting... Dir should be just sat there in VB, with no need for you to play around with references.

I would suggest re-installing VB and applying the latest service pack (6), or perhaps in the interests of safety, installing to another machine...

See if Johnwm has any input here...


mmilan
 
Just out of curiosity...

What version of VB are you using?

What references do you have set when you start a new Standard EXE project?

(Project>References)

I have:
Visual Basic For Applications
Visual Basic runtime objects and procedures
Visual Basic objects and procedures
OLE Automation


(these are default on my 3 PC's between work and home: Win98, WinXP Home, & WinXP Pro)

If you have those set, you should be good to go...

If they are not set, or if they are and you are using VB6 (or 5?) and it is still not working...
I would suggest reinstalling and make sure you do a FULL install.

Dir() should be a standard part of VB, I have never had to make a reference to it...

Also are you using Standard or Pro/Enterprise Edition?
I use VB6 Pro on all machines...

You should be able to say something like:
MsgBox Dir("C:\Autoexec.bat")
with no problems...

If you get an error, there is definitely something wrong.

Good Luck,
-Josh

Have Fun, Be Young... Code BASIC
-Josh
cubee101.gif


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
These days I try to avoid using FSO - something to do with a problem we encountered with in on Windows 2000 that meant we had to recompile and update 400 sites...

Besides, why go into the scripting stuff when there's no need?

I'm not an advocate of Dir() normally either (I prefer Johnwm's API approach), but given Squirlee's apparent level with VB, I thought that would just cause her problems.

mmilan
 
well, just thought she uses fso.FileExists and seems to be
familiar with using fso
 
It's a question of preference - if she wants to use FSO then she can - so long as she has a filename, getattr will still do the job.

I just didn't want her to hit the same pitfalls as I have in the past.

I'm not saying FSO is wrong, I'm just proposing an alternative.

mmilan.

Ps. For a start, given Squirlee's apparent knowledge level, do you think she will be making a point of killing the object when she's done? Why force her into objects at all? (Though I suppose, again, pushing her forward...)

 
I've been looking and trying different things out and as of right now I am using the FindFile method. So far so good. It is working and it does correctly find the first file - I'm now working on having it loop through to search for all the files.

PS Also, I am using VB6 - and I reloaded it and can now use dir() by default with the same default libraries loaded as CubeE101.

Progress, not perfection...eh?
 
You've gone for the FindFirstFile eh? Mucho cudos to Johnwm then for proposing it...

FindFirst gives you a handle that you can then use in a call to FindNextFile, allowing you to step through all the files matching your criteria...

That having been said, since you're looking at testing for a specfic file in each folder, as opposed to looking for matches to a mask, you're probably not going to have much use for FindNextFile.

Remember to close you're handle though after you're done.

mmilan
 
If you are looking for All the Files in a folder... try this:
Code:
Dim MyFile As sting, MyFiles() As string, i as long
MyFile = Dir("C:\WhereEver\*.*")
While MyFile <> ""
  Redim Preserve MyFiles(i)
  MyFiles(i) = MyFile
  i = i + 1
  MyFile = Dir
Wend

If you want to display the files in a Text box here is a quick command:
Code:
Text1 = Join(MyFiles, vbCrLf)

The first Dir command:
MyFile = Dir("C:\WhereEver\*.*")
Sets the search pattern and returns the first file (if it finds it, "" otherwise)

The Second Dir command:
MyFile = Dir
Uses the same pattern as the first, and picks up where it left off...


Good Luck,
Josh

Have Fun, Be Young... Code BASIC
-Josh
cubee101.gif


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top