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

Text file manipulation

Status
Not open for further replies.

admdev

Programmer
May 17, 2006
56
US
Hi all,

Hope somebody can help me with this.

I need to be able to read the contents of a directory and return the file name of the file with the latest date. Then, copy it into a different directory.

Thanks in advance.
 
DSummZZZ,

Thanks for replying!

According to the Fox Pro help, the following is the format expected by command.

ADIR(ArrayName [, cFileSkeleton [, cAttribute [, nFlag]]])

Where in here can I type the directory I am trying to browse. Additionally, the file extensions are unique; in other words, the application generates a random extension as file.123, file.234, file.789, file.546, etc.

Thanks.
 
Admdev,

Where in here can I type the directory I am trying to browse.

This is in the cFileSkeleton parameter.

For example, if the directory is c:\foxpro\data, and you want to get files whose names are like File.123, File.789, etc., the command would be:

Code:
ADIR(MyArray, "c:\foxpro\data\file.*")

If the filenames don't follow any particular pattern (e.g. they don't all start with "file"), do this instead:

Code:
ADIR(MyArray, "c:\foxpro\data\*.*")

Once you've got the filenames into the array, use ASCAN() to sort it on the third column. The last item in the sorted array will be the most recent file.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

My Visual FoxPro site: www.ml-consult.co.uk
 

Once you've got the filenames into the array, use ASCAN() to sort it on the third column. The last item in the sorted array will be the most recent file.

Mike,
You, probably, meant ASORT(), because ASCAN() is searching an array for a particular element, not sorting it.
 
Thanks guys for your help on this!

How would I select the file. Below is the code I am using.
Am I going to assign it to a variable? What I need to do is select the latest file received within this directory and move it into another folder say "D:\Temp\FilesBack".


ADIR(Files, "D:\Testfiles\Incoming\*.*")
ASORT(Files,3)

Thanks in advance.
 
After using the ADIR() commsnd, the list of file names is now stored as character strings in the array which you named Files

You merely determine how many files are in the array by either modifying the original ADIR() command to
mnFileCount = ADIR(whatever)
or you can subsequently use the ALEN() command on the resultant array.
example: mnFileCount = ALEN(Files,1)

Now with the filecount you could loop through the array getting the individual filenames and putting them into a variable
example:
Code:
FOR FileCntr = 1 to mnFileCount
   mcSourceFile = Files(FileCntr,1)
   <Do Whatever To mcSourceFile>
ENDFOR

And then within the loop, depending on how you wanted to use the file you could
Code:
COPY FILE (mcSourceFile) TO <mcDestination>
IF FILE (mcDestination)
  ERASE (mcSourceFile)
ENDIF

Good Luck,
JRB-Bldr
 
Code:
IF ADIR(laFiles, "D:\Testfiles\Incoming\*.*") > 0
   ASORT(laFiles,3)
   lcFileSource = [D:\Testfiles\Incoming\]+laFiles[1,1]
   lcFileTarget = [D:\Temp\FilesBack\]    +laFiles[1,1]
   RENAME (lcFileSource) TO (lcFileTarget)
ENDIF

Borislav Borissov
VFP9 SP1, SQL Server 2000/2005.
MVP VFP
 
Admdev,

After you've done the sort, the required filename will be in the first column of the last row of the array. So, you can access it like this:

Code:
lcRequiredFile = ;
  MyArray(ALEN(MyArray, 1), 1)

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

My Visual FoxPro site: www.ml-consult.co.uk
 
Oops, After I saw Mike's answer I understood that I gave you a wrong answer, you must either use Mike's suggestion to get last file either sort array descending:
Code:
ASORT(laFiles,3,-1,1)

Borislav Borissov
VFP9 SP1, SQL Server 2000/2005.
MVP VFP
 
Thanks for all your help guys!

It was really helpful. I got it to work.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top