Solved

In asp, how can I make my program wait until a specific new file is created

Posted on 2010-11-30
6
331 Views
Last Modified: 2012-05-10
I have an online screen that runs a batch process.  I want to wait until that batch process has created a certain file, which may take a minute or more.

How can I test to see if the file is created before letting the user continue?
0
Comment
Question by:d2fox
  • 3
  • 2
6 Comments
 
LVL 1

Expert Comment

by:pjpatricio
ID: 34240107
You can combine some JavaScript in the browser that calls a web service or a page that will check the existence of the file in the server.
So:
1-  create a timer in JavaScript
2-  When the timer event happens, call your web service using Jquery or if you are not familiar with jquery just use an iframe calling a page that will check if the file exists and returns something like yes or no.
3 - Test the output of what you called on step 2 and change the interface to let the user continue if the file is created and kill the timer. If the file is not created another event will occur and you are back into step 2.

Let me know if you need sample code samples for any of the steps, just giving you an overall about the process.

0
 
LVL 9

Expert Comment

by:cb1393
ID: 34240457
If using ASP.net, you can use System.IO.FileSystemWatcher to monitor a directory for the creation of a file.
0
 

Author Comment

by:d2fox
ID: 34240572
cb1393,  

Can I do it for a particular file, or  is it just for 'any' file in that directory?  This is the direction I was pursuing, but all I saw was 'a file created', and I need a specific file.

Thanks,
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 1

Accepted Solution

by:
pjpatricio earned 500 total points
ID: 34240698
If can use File.Exists to check for the presence of a specific file.
string filePath = @"c:\temp\yourfile.txt";
while(true){
if(File.Exists(filePath)){
 break;
}

Thread.Sleep(3 * 1000); //3 seconds
}

Open in new window

0
 
LVL 9

Expert Comment

by:cb1393
ID: 34240910
Yes, you can use it for any file by specifying a filename filter, which also allows for wildcard matches.
0
 
LVL 9

Expert Comment

by:cb1393
ID: 34241210
There's a number of ways it can be implemented on a page, here's one example I whipped up and tested that does not rely on timers/loops:

Test.aspx
<%@ Page Language="VB" AutoEventWireup="false" Inherits="test_page" Codebehind="test.aspx.vb" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Test Page</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>

<form id="form1" runat="server">

Click the button below to run some code and wait for the "mylog.txt" file to appear in the
"/test/" directory of the web app. The page will reload when the file is present.
<br /><br />

<asp:Label ID="Label1" runat="server" Text="Waiting for file..."></asp:Label>
<br /><br />

<asp:Button ID="Button1" runat="server" Text="Begin Watching" />

</form>
</body>
</html>

Open in new window


Codebehind
Imports System.IO

Partial Class test_page
    Inherits System.Web.UI.Page

    Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load

    End Sub

    Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
        Dim fsw As New FileSystemWatcher(Server.MapPath("~/test/"), "mylog.txt")
        fsw.NotifyFilter = NotifyFilters.FileName
        fsw.EnableRaisingEvents = True

        '------------------ PROCESS TO GENERATE "mylog.txt" GOES HERE ------------------

        'THE FOLLOWING LINE WILL PAUSE FURTHER PROCESSING UNTIL THE FILE IS PRESENT
        Dim cr As IO.WaitForChangedResult = fsw.WaitForChanged(WatcherChangeTypes.Created)

        Label1.Text = "File has been created!"
    End Sub

End Class

Open in new window

0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video discusses moving either the default database or any database to a new volume.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now