Solved

Choose the older file FSO

Posted on 2016-10-04
6
59 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
[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
  • 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
Technology Partners: 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!

 

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

Independent Software Vendors: 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!

Question has a verified solution.

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

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…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

729 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