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!

Extract Files from General Fields

Utility Program

Extract Files from General Fields

by  baltman  Posted    (Edited  )
There are only a couple of tricks to extracting files out of general fields. First you have to isolate just one file into it's own .FPT file, and secondly you have to strip the correct number of characters off the start of the resulting file before you write out the stored file.

Even files with the same extension can have vastly different headers, so this code is definitely not one-size fits all and will work best when all or most of the files were created consistiently so that the header matches the sample file.

I suppose someone could build a library of 'starter strings' and/or how VFP handles loading/storing each one.

I also noticed that while the program works with Excel files too, the extracted file opens into Excel as hidden and needs to be unhidden. Weird huh?

This code assumes you have a filename without a path stored in the dbf... if you don't have this situation you can always make one by adding a character field and populating it with transform(recno()).

Anyway, just copy and paste this code into a prg and run.

Code:
CLOSE ALL
lcSafe=SET("safe")
SET safe off
messagebox("Please select the table you want to extract files from.")
lcTargetTable=FULLPATH(GETFILE([dbf]))

lcFilename = INPUTBOX("Type in the name of the field with your file names.")
lcGeneralFieldname = INPUTBOX("Type in the name of the general field.")

messagebox("Please select an example of the file type you want to extract e.g. a Word file")
lcTestStr=LEFT(FILETOSTR(GETFILE()),10)

SET DEFAULT TO JUSTPATH(lcTargetTable)

USE (lcTargetTable)
SCAN
	lcOutputNameAndPath=SYS(5)+SYS(2003)+[\]+&lcFilename
	COPY FIELD (lcGeneralFieldname),&lcFilename TO MyTempFile NEXT 1
	lcFile=FILETOSTR("MyTempFile.FPT")
	lcFile=RIGHT(lcFile,LEN(lcFile)-(ATC(lcTestStr,lcFile)-1))
	IF ATC(lcTestStr,lcFile)<1000 &&otherwise assume it's a different file type
		STRTOFILE(lcFile,lcOutputNameAndPath)
	ENDIF 
ENDSCAN

DROP TABLE MyTempFile
SET SAFETY &lcSafe

Please let me know if this helps [smile]
Brian
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top