Solved

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

Posted on 2010-11-30
6
339 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

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.

Question has a verified solution.

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

I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
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.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

763 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