Solved

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

Posted on 2010-11-30
6
336 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
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.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

813 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

10 Experts available now in Live!

Get 1:1 Help Now