Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Scan folder and files

Status
Not open for further replies.

johann59

Programmer
Mar 3, 2005
32
0
0
US
Are there functions in VFP 9 that would allow me to:

-Scan a specific folder at certain time intervals (let's say at 1 hour intervals) for files (Excel/Text, etc. files) that are dropped in that folder. That folder would usually be empty and the program would check it every hour it would scan (check) the folder to see if it has files, and if it does:

-Send an email to the user when a file is dropped

-Rename the file with the date that it is received. <File>_20190627-10:26AM for example

-And move the file to another specific folder

Is this something that can be done in VFP9 or should I look into another programming language?

Thanks
 
Yes, everything you ask for is possible.

Scanning a folder

If you are looking for one specific file (the name of which you already know), use the FILE() function, passing it the full filename and path. It returns .T. if the file is present.

If you are looking for any file, use ADIR(). This fills an array with all files from a given folder. You can then scan the array, for example to find files that were created after a certain time.

Scanning at hourly intervals

Add a timer object to your application. Set its Interval property to one hour. In the Timer event, write code to do the scan, as above.

Send an email

There are lots of ways of doing this. Look at the FAQs in this forum for some specific suggestions.

Rename the file

Use the RENAME command.

Move the file to another folder

The same RENAME command can do this. Just set the target name to include the destination folder.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Moving file to another folder requires a strategy to deal with duplicate file names.

You might want to put (1) on the end of the file name if it already exists.

Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.

I'm trying to cut down on the use of shrieks (exclamation marks), I'm told they are !good for you.
 
And an hour is a long time.

Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.

I'm trying to cut down on the use of shrieks (exclamation marks), I'm told they are !good for you.
 
You can use several non-VFP helpers to achieve that with less effort.

1. Scan folder: ADIR is there for that. Also SYS(2000)

2. Hourly run: To run something regularly Windows offers Scheduled Tasks. This includes the facility to inform about good and bad runs via exit code (eg monitoring a network folder might not work if network has an outage, which of course means you ideally do this at the computer or server where the folder is on a local drive, VFP runs on server OSes, too, so no problem here)

3. Email about it: There are tons of ways to send a mail and the big advantage over sending a mail from a scheduled task is you can set the text body to anything, TEXT TO text merging here is helpful in using texts with placeholders.

4. Renaming the file and moving it: This is one thing with RENAME, as Mike said. If the target folder is on the same physical drive or you can manage this to go there, using Windows API function MoveFile or MoveFileEx would be a good idea as these API functions move files very fast, even large files. They don't really move much on HDD, they only modify their table of content entry, like changing which folder the file belongs to with file metadata not part of the file itself. Only possible when the file stays on the same partition, though. Every file in the file system has TOC information and I'm not talking of file attributes but the main core information about a file not part of it.

So assume these files are CSV data coming from a third party, you process them and put them away for backup storage, it wouldn't matter if it's 100MN or GB sized files, they move to the target folder in the blink of an eye with MoveFileEx. Been there, done that.

So overall What actually remains from your task as the VFP executable portion is the code necessary to do bullet points 1,3, and 4.

There's one more thing you could use: WMI, that allows monitoring folders, see [URL unfurl="true"]https://devblogs.microsoft.com/scripting/use-powershell-to-monitor-for-the-creation-of-new-files/[/url]. You can do very many things in Powershell scripts more than just displaying the name of a new file, so you could also do the whole task with Powershell and WMI event monitoring as is used there.

And to add to that, the target folder may also be one that's connected up to cloud drive space like Dropbox, Google Drive, One Drive, etc. So your executable has nothing to do about backing up processed files as archives source of data reference other than putting processed files into that target folder.

As you should install that on a server where an "inbox" folder typically is situated, the only concern about using VFP for an executable doing the job instead of anything lower level like a Powershell script is what I experienced more than once: The server admins are not in touch with departments putting up such mechanism on the server and even though the company knows you're the vendor of this (or you're even from the department needing this feature itself), this comes up as failing or even missing after a server is replaced or moves. And something based on Powershell then can have a higher chance of survival.

Though I wouldn't bash on admins too much. It's just important to have an IT/DevOps department, which cares for more than the main SQL Server or Web server running on the in-house servers and a VFP app can simply be copied over as this task has no 3rd party dependency (if you don't pick a mail option needing some registered ActiveX component). You can put the VFP runtimes into the folder of your EXE and it runs without a setup to be reapplied on a new or moved or virtualized server.

Just be warned, such things are not seldom put up in a shoot and forget fashion and it's hard to keep dedication about things like this even if you establish a wiki or other means of reference documentation.

Bye, Olaf.

Olaf Doschke Software Engineering
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top