Solved

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

Posted on 2010-11-30
6
337 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

861 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