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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File Exists... better way? 2

Status
Not open for further replies.

CubeE101

Programmer
Nov 19, 2002
1,492
US
Does anyone have a better method of doing this...

Dim myLof as Long
Open "A File.txt" for Append as #1
myLof = LOF(1)
Close
If myLof =<1 then
Kill &quot;A File.txt&quot;
else
'Read in the file
end if

I don't like using this because it creates a file if it does not exist... and if it does but it's empty... it deletes it anyway...

is there a solution using VBscript FSO (FileSystemObject)?

I scanned through the doc's but I didn't see anything at a glance...

I was just wondering if anyone else has had this issue before and found a better solution. And if so, if they have any comments of the other method (the Ups and Downs)

Code:
Thnx -N- Advnc
Josh Stribling
Sometimes... the BASIC things in life are the best...
cheers.gif

or at least the most fun ;-)
-Josh Stribling
 
The FileScriptingObject has the method you need. I wrap it in a help function as follows:

Public Function FileExists(strFileName As String) As Boolean
Dim fs As New FileSystemObject

FileExists = fs.FileExists(strFileName)

Set fs = Nothing
End Function Dave Robinder, MCSD
Consultant
Booz Allen Hamilton
Colorado Springs, CO
(719) 590-6041
 
If you are looking for a simple &quot;file exists&quot; funtion, use Dir().

e.g.
If (Dir(&quot;MyFile.txt&quot;) = &quot;&quot;) Then
'File DOES NOT Exist
Else
'Open, then process file
End If
 

Or...
[tt]
Public Function FileExists(strFileName As String) As Boolean

If Dir(strFileName) <> &quot;&quot; Then FileExists = True

End Function
[/tt]

Good Luck

 
Thanks to all of you...
I was trying to avoid the scripting method...
The Dir() was more along the lines of what I was wanting...
And now I know how to do both ways...
That was some quick responding though...
Starz 4 all 3 Sometimes... the BASIC things in life are the best...
cheers.gif

or at least the most fun ;-)
-Josh Stribling
 
I don't understand why you want to keep avoiding class libraries, Josh...
 
because...
when I am trying to teach a group of people how to be able to do what I am doing I don't want to have to give them a list of 50 classes to reference...

If you can do everything within VB standard library... why use external libraries...???

I try to keep it as simple as possible...
Plus when you start using external classes other than the standard windows objects classes... you run into the issue where not everyone has the same classes... and what I do is for internal use within the company... So I don't use package and deploy utilities...
Just File>Make ...exe and email it to everyone.

Does that make sense to anyone else? Sometimes... the BASIC things in life are the best...
cheers.gif

or at least the most fun ;-)
-Josh Stribling
 
Well, just as an example, the scripting control library has been included as part of the OS since the release of W98, and with all versions of IE since 5.0. And vbscript.dll, which provides Regular Expressions (to name but two that have been recently touched upon in this forum), has been included in the OS since W95 OSR2. So both of these could be considered a standard libraries. There are plenty of others that don't actually require redistribution through any sort of P&D tool.

Whilst I appreciate that you have your reasons for avoiding all this stuff, it does strike me as odd that you should choose to use a language that has a load of features that you consider superfluous and pointless.
 
Ah - just seen your comments about SOlidWorks etc in the other thread. That might explain it...
 
that... and...
I think I said this in another thread but...

I grew up on the Basic language since
GWBasic (... lol GeeWizBasic was the full name) and Basica
(I still don't know what the difference was in those 2)

Then I discovered Qbasic... Which unlocked a world of possibilities for me at the time...

I remember the first time I tried to use Turbo Pascal 5.5...
(lol) I was trying to type basic code into it and compile it... needless to say... IT DIDN'T WORK!... (can you image why???;-))

Then windows 95 came out... and I had about 5 years of progamming experience... (I was 15 at the time)

I tried to write Windows in QB... that didn't work... so I made my own windows :) (with a few less features)... I thought it was pretty cool at the time though... I emulated threads using timers and calling different subs that just ran through them for the other windows... I made a chess game that functioned correctly (2 player, no ai), a simon game, a sprite maker/animator, and a 3d looking tetris game.
I did all this on a 4 color (shades of grey to blue) NEC laptop that had 1 meg of memory (Hard drive) and a 286...
Dos was built into the ROM.

then I started playing with VB5 on my dads computer...

a couple years later I got a computer with W98 when it came out... And bought a copy of VB6 and VC++6 (if I was smart I would have just bought VisualSuite)

I played around with it on and off but continued to use QB... I liked making games... in QB better because it was always fullscreen...

I started messing with 3D then assembly the making Libs with C, Pascal, and asm and using them in QB...

Then I messed with Java, html, and a few others and did a loop right back to VB...

Now at work I use VB and VBA all the time...

The MAIN thing I do is Drafting (which I also teach a VoTech Night class for solidworks) but I am always using VB to tie programs together... I know I could use VC++ but I prefer VB because I am used to it... the most I ever did in VC++ was make a couple of Sonique Plug-ins... and modifying existing projects... I have had VC++ for about 5 years now and I am still not used to the IDE... where as I was using, runing, and compiling in VB within the first 30 minutes the first time I ever used it...

That is the LONG version of &quot;Why I Use VB&quot;
(I might publish it into a book someday ;-))
O..K my program just finished compiling so I am going home... (after an hour of waiting)
L8r
-Josh Sometimes... the BASIC things in life are the best...
cheers.gif

or at least the most fun ;-)
-Josh Stribling
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top