Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2010-11-30
6
Medium Priority
?
358 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 1

Accepted Solution

by:
pjpatricio earned 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

604 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