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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Don't create empty xls files 1

Status
Not open for further replies.

nicktagz

IS-IT--Management
Mar 20, 2017
5
US
I'm currently using the copy to command to export dbf tables to xls for certain criteria. My question is if there is no data to export is there a way to not create the xls file?
 
You say you want to export "for certain criteria". I take that to mean that you want to skip the XLS if there is no data that meets the criteria.

If that's so, then the easiest way would be something like this:

Code:
SELECT MyTable
COUNT TO lnCount FOR x > y  && or whatever criteria you want to apply
IF lnCount > 0
  COPY TO etc. etc. TYPE XLS
ENDIF

But if my assumption is wrong, that is, if you only want to skip the XLS if the original DBF is completely empty, then use Tom's method. But note that that won't skip the copy if all the records in the file have been deleted but not packed, which is probably not what you want.

Mike



__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Run a simple "Select * from yourtable where <yourcriteria> into array laDummy" first, and only continue with the export if _tally>0. Or even simpler, Select into a cursor, and then if _tally>0, use the cursor as source for the export.
 
Well, _TALLY also counts the number of copied records of COPY TO, so it's a good advice to use it, but it's not necessary to first SQL-Select data.
One simple idea would be to delete the file you just created, IF _TALLY=0.

An elegant way to not wear out SSDs or in general hard drives, would be using a RAM Disc as first stage and then copy to a real drive if _TALLY>0. It's a bit too late for that nowadays, as SSDs today don't wear out so much anymore on one side and since I actually don't know if there is a RAM disc capable to work on Windows 10 anymore, rather using a virtual hdd or crypto container, which also uses a drive virtualisation. Still a bit over the top, you could really simply write out the file you want to and remove it, when _TALLY=0.

Bye, Olaf.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top