?
Solved

Choose the older file FSO

Posted on 2016-10-04
6
Medium Priority
?
65 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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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…
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…
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 …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

770 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