Solved

Choose the older file FSO

Posted on 2016-10-04
6
39 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 32

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 32

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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 32

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:  The Exchange of information …
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

911 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

16 Experts available now in Live!

Get 1:1 Help Now