Solved

Choose the older file FSO

Posted on 2016-10-04
6
30 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
Comment Utility
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
Comment Utility
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
Comment Utility
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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:jsbx
Comment Utility
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
Comment Utility
change

Set oldestFile = oldestfile

to

Set oldestFile = x
0
 

Author Closing Comment

by:jsbx
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

744 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

17 Experts available now in Live!

Get 1:1 Help Now