Solved

Choose the older file FSO

Posted on 2016-10-04
6
49 Views
Last Modified: 2016-10-04
Hello,

I have a folder that receives xml files from an external service.
I have to read, process and move each file.
I reload an .asp webpage once a second.

I want to make sure I don't pick up a file that's in the middle of being created.

Right now I use this code to choose one file, but I know it is not correct:

<%
Function PrimerFile(carpeta)
dim FSO,fo,x
set FSO=Server.CreateObject("Scripting.FileSystemObject")
set fo=FSO.GetFolder(Server.MapPath(carpeta))
loopnum=1
for each x in fo.files
  PrimerFile = x.name
  if loopnum=1 then exit for
next
set fo=nothing
set FSO=nothing
end function
%>

Picking up the older file is an aproach, but if I only have 1 file it can fail.
I would prefer to pick up files that are 2 seconds old (since saved to the disk), for example.

Thank you.
band.
0
Comment
Question by:jsbx
  • 3
  • 3
6 Comments
 
LVL 33

Expert Comment

by:Big Monty
ID: 41827846
This should get you the oldest file in your folder regardless of the number of files:

OldestFile = Nothing
for each x in fo.files
  If (oldestFile is Nothing) Then
    Set oldestFile = x
  ElseIf (x.DateLastModified < oldestFile.DateLastModified) Then
    Set oldestFile = file
  End If
next

PrimerFile = oldestFile.name
0
 

Author Comment

by:jsbx
ID: 41828115
Hello Big Monty,

It worked but when there are no files left I got this error:

Microsoft VBScript runtime error '800a01a8'

Object required: 'OldestFile'

/dtebankrecepcion/Funciones.asp, line 37

----------------------------

<% 'versión mejorada que selecciona sólo el más antíguo de todos para procesarlo.
Function PrimerFile(carpeta)
dim FSO,fo,x
set FSO=Server.CreateObject("Scripting.FileSystemObject")
set fo=FSO.GetFolder(Server.MapPath(carpeta))

set OldestFile = Nothing
for each x in fo.files
  If (oldestFile is Nothing) Then
    Set oldestFile = x
  ElseIf (x.DateLastModified < oldestFile.DateLastModified) Then
    Set oldestFile = oldestfile
  End If
next

PrimerFile = oldestFile.name
set fo=nothing
set FSO=nothing
end function
%>

Line 37: PrimerFile = oldestFile.name
0
 
LVL 33

Expert Comment

by:Big Monty
ID: 41828149
simple enough to fix, you just need to check the COUNT property:

<% 'versión mejorada que selecciona sólo el más antíguo de todos para procesarlo.
Function PrimerFile(carpeta)
dim FSO,fo,x
set FSO=Server.CreateObject("Scripting.FileSystemObject")
set fo=FSO.GetFolder(Server.MapPath(carpeta))

if fo.files.count > 0 then
set OldestFile = Nothing 
for each x in fo.files
  If (oldestFile is Nothing) Then
    Set oldestFile = x
  ElseIf (x.DateLastModified < oldestFile.DateLastModified) Then
    Set oldestFile = oldestfile
  End If
next

PrimerFile = oldestFile.name
end if
set fo=nothing
set FSO=nothing
end function
%>

Open in new window

0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Comment

by:jsbx
ID: 41828186
Thank you, but something is wrong, it doesn't chhose the oldest one always? look:
https://www.youtube.com/watch?v=LPpYiq1ybY8
0
 
LVL 33

Accepted Solution

by:
Big Monty earned 500 total points
ID: 41828192
change

Set oldestFile = oldestfile

to

Set oldestFile = x
0
 

Author Closing Comment

by:jsbx
ID: 41828282
Thank you very much!
Here's the final code:

<% 'versión mejorada que selecciona sólo el más antíguo de todos para procesarlo.
Function PrimerFile(carpeta)
dim FSO,fo,x
set FSO=Server.CreateObject("Scripting.FileSystemObject")
set fo=FSO.GetFolder(Server.MapPath(carpeta))

if fo.files.count > 0 then
set OldestFile = Nothing
for each x in fo.files
  If (oldestFile is Nothing) Then
    Set oldestFile = x
  ElseIf (x.DateLastModified < oldestFile.DateLastModified) Then
    Set oldestFile = x
  End If
next

PrimerFile = oldestFile.name
end if
set fo=nothing
set FSO=nothing
end function
%>
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Title # Comments Views Activity
How to hide jquery hemiintro in returning user? 1 84
Select case on click 3 19
CREATE DATABASE 3 28
send email part1 9 23
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…
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

808 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