Shoutbox

Check whether file exists - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Skype & Technology (/forumdisplay.php?fid=9)
+---- Forum: Tech Talk (/forumdisplay.php?fid=17)
+----- Thread: Check whether file exists (/showthread.php?tid=73287)

Check whether file exists by ryxdp on 04-04-2007 at 07:13 AM

More VB 2005 Express dilemmas :(.
I need a way of finding out whether a file exists (i know this bit), but it needs to keep on checking from Form1.Load to kill.

I don't like using timers.

Is it possible?


RE: Check whether file exists by vikke on 04-04-2007 at 07:26 AM

You can start with telling us what programming language you're programming. If you're programming with the Win32 API, you should use CreateFileEx.
If you're using a dotNet based language, I think you can find it easily at  Google.

vikke


RE: RE: Check whether file exists by TheSteve on 04-04-2007 at 08:57 AM

quote:
Originally posted by vikke
you should use CreateFileEx.

There is no CreateFileEx... How about CreateFile instead?
If the file fails to open, make sure that GetLastError returns ERROR_FILE_NOT_FOUND or ERROR_PATH_NOT_FOUND or ther could be some other issue other than a missing file.

However you choose to keep checking you've got to put forth some effort.  A thread is more difficult in some languages than timers.
RE: Check whether file exists by vikke on 04-04-2007 at 09:00 AM

Yeah, my bad, I meant CreateFile.

And make sure you call GetLastError directly after your CreateFile call, otherwise you could get: "Error: Operation Successfully Completed". :)

vikke


RE: Check whether file exists by TheSteve on 04-04-2007 at 09:04 AM

If you're using .NET, this may be of some interest.
http://msdn2.microsoft.com/en-us/library/system.io.file.exists.aspx


RE: Check whether file exists by Ezra on 04-04-2007 at 09:28 AM

I think he already knows how to check if a file exists, but he wants to know a way to keep continually checking untill the program exits. Without using Timers

I think the best way is to check if the file exists the first time and after that register a handle to watch filechanges in that directory.

EDIT: Watching filechanges is possible with FileSystemWatcher in .NET


RE: Check whether file exists by vikke on 04-04-2007 at 09:39 AM

If you open a file with CreateFile, the file cannot get deleted, only renamed.
If you don't want it to get removed, that's probably the best way.

vikke


RE: Check whether file exists by ryxdp on 04-04-2007 at 10:14 AM

quote:
Originally posted by vikke
You can start with telling us what programming language you're programming.
quote:
Originally posted by me
More VB 2005 Express dilemmas

The program it needs to be in opens and sends a file, to be exact "PP5022.mi4" to a dynamic drive label, and all the while there is a Label that will either say "Theme not loaded. Please load theme." or "Theme loaded". Is there something you can put in Form1.Load that will just keep on checking?

If you don't get me, here's the diagram as I see it:

Private Sub Form1.Load [all the rest of that args stuff]
If Form1.Active Then
Keep.On.Checking.If.File.Exists(tillKill)
Using My.Computer.FileSystem.FileExists("E:\PP5022.mi4")
RE: Check whether file exists by Ezra on 04-04-2007 at 10:22 AM

quote:
Originally posted by Ryxpia
The program it needs to be in opens and sends a file, to be exact "PP5022.mi4" to a dynamic drive label, and all the while there is a Label that will either say "Theme not loaded. Please load theme." or "Theme loaded". Is there something you can put in Form1.Load that will just keep on checking?

If you don't get me, here's the diagram as I see it:

Private Sub Form1.Load [all the rest of that args stuff]
If Form1.Active Then
Keep.On.Checking.If.File.Exists(tillKill)
Using My.Computer.FileSystem.FileExists("E:\PP5022.mi4")


Like I said, first do a check to see if the file exists the normal way,

If it exists then register a filesystemwatcher event   'to see file changes in the folder where that file is placed

'now everytime a file gets deleted the function you registered will fire, in this function just

check if the deleted file is that file.
RE: Check whether file exists by John Anderton on 04-04-2007 at 10:39 AM

Never tried this in .NET but I suppose the classic, create a file pointer and check if its null should work perfectly fine :)
ie handle to file is null, file doesn't exist, else it does :)


RE: Check whether file exists by ryxdp on 04-04-2007 at 10:40 AM

Not exactly what I wanted... (or maybe it was...? I'm not too flashy with VB)

anyway, I've set the path in Form1.Load, now all I need is to link up the FileSystemWatcher with _exist_ (the label) somehow, and keep it checking whether PP5022.mi4 exists or not.

So how is that to be done?


RE: Check whether file exists by Ezra on 04-04-2007 at 11:32 AM

I made a little example:

code:
Public Class Form1

    Private File As String = "C:\DataFile.dat"
    Private mywatcher As New IO.FileSystemWatcher 'Create new instance of FileSystemWatcher

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        'Check the first time if the File Exists
        If System.IO.File.Exists(File) Then
            mywatcher.Path = File 'Specify Path to File or Directory. Which ever you need
            mywatcher.IncludeSubdirectories = False
            mywatcher.NotifyFilter = IO.NotifyFilters.DirectoryName Or IO.NotifyFilters.FileName Or IO.NotifyFilters.LastWrite Or IO.NotifyFilters.Security

            AddHandler mywatcher.Deleted, AddressOf OnDeleted  'Use intellisense to see what others are open to you

            mywatcher.EnableRaisingEvents = True     'Start the process
        End If
    End Sub


    Private Sub OnDeleted(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs)
        If e.FullPath = File And e.ChangeType = IO.WatcherChangeTypes.Deleted Then
            'FILE HAS BEEN REMOVED!!
        End If
    End Sub

End Class



WDZ implement auto code coloring :'(
RE: RE: Check whether file exists by vikke on 04-04-2007 at 11:39 AM

quote:
Originally posted by Ryxpia
quote:
Originally posted by vikke
You can start with telling us what programming language you're programming.
quote:
Originally posted by me
More VB 2005 Express dilemmas

The program it needs to be in opens and sends a file, to be exact "PP5022.mi4" to a dynamic drive label, and all the while there is a Label that will either say "Theme not loaded. Please load theme." or "Theme loaded". Is there something you can put in Form1.Load that will just keep on checking?

If you don't get me, here's the diagram as I see it:

Private Sub Form1.Load [all the rest of that args stuff]
If Form1.Active Then
Keep.On.Checking.If.File.Exists(tillKill)
Using My.Computer.FileSystem.FileExists("E:\PP5022.mi4")

Sorry, but there is no programming language called VB 2005 Express. I suppose you mean VB.Net.

The code by Ezra looks really nice. You should try it! ;)

vikke
RE: Check whether file exists by ryxdp on 04-05-2007 at 05:40 AM

Ezra, will your code work as I want:

-App is executed
-Code starts
-Person enters and sends a file
-Code sees this and changes label to appropriate text
-App is killed

Hope this is a bit clearer.

EDIT: It seems to work. How do I make an event for -Code sees this and changes label to appropriate text ?


RE: Check whether file exists by roflmao456 on 04-06-2007 at 05:34 AM

make an event and pop in this code:

code:
objname.Text = "whatever"
' blah blah blah