PerfectScript Journal

October 1998: Processing a group of files with a macro

Often you will have a need to edit a large group of files, making the same changes to each file. This can be very tedious, especially since you have to open each file separately, make the changes, then save the file.

For example, if your company changes the formatting standard for all documents, or if the name or address of the company changes, you could be forced to tediously open each file created over the last several years, make the required changes, then save the file with the changes.

The FileFind command, available in WPWin 7 and WPWin 8, lets you process files as a batch, making the same changes to each file with a macro, thus saving lots of time -- and your sanity.

I give two methods for using this command here. In the first method, we will do the processing as we find each filename. In the second method, the filenames are assigned to an array variable so that they can be used more than once during the course of the macro. The method you choose to use will depend on the desired end result.

To simplify matters, all the files that require processing should be in the same directory. It may also help if the filenames all have something in common, such as the same filename extension, so that the macro can quickly find just the files you need to work on.

Process each file as it is located

In the first method, we simply find a name, open and process the file, then save and close the file. The filename is not saved or stored anywhere. We just get the work done and move on:

Application(A1; "WordPerfect"; default; "US")
vFilename:=FileFind("c:\myfiles\*.wpd"; ;1)
FileOpen(vFilename)
// do the processing for that file here
FileSave()
CloseNoSave()
Repeat
	vFilename:=FileFind(""; ;1)
	FileOpen(vFilename)
	// do the processing for that file here
	FileSave()
	CloseNoSave()
Until(vFilename="")
          

Each file that matches the specified mask will be opened, processed, then saved and closed.

Assign filenames to an array

There may be times when you need to use the filenames again later, or for a purpose other than opening and editing the files. You can use FileFind to gather the names and assign them to an array. I've created a couple of functions, shown below, to assign the filenames to an array:


vFileArray[]:=FilenameArray(?PathDocument+"*.wpd"; 1)
Fornext(x; 1; vFileArray[0])
	Type(vFileArray[x])
	HardReturn
Endfor

//*********************************************************
// Purpose: Get an array of filenames
// To use: vFileArray[]:=FilenameArray(vPath; vContext)
// Parameters:
//	vPath = the path and mask to the files you want to get
//	vContext = a numeric value, such as 1, 2, or 3. Use
//		a different context for each separate search
// Remarks: Filenames are returned in the order in which they 
//	are stored in the File Allocation Table. 
// Copyright (c) 1998 J. Jeppson All Rights Reserved
//*********************************************************
FUNCTION FilenameArray(vPath; vContext)
vCount:=CountFiles(vPath; vContext)
Declare vFileArray[vCount]
vFileArray[1]:=FileFind(vPath; ; vContext)     
Fornext(x; 2; vCount)
 vFileArray[x]:=FileFind(""; ;vContext)
Endfor
Return(vFileArray[])
ENDFUNC

FUNCTION CountFiles(vPath; vContext)
vFilename:=FileFind(vPath; ;vContext)
vCount:=1
Repeat
	vFilename:=FileFind(""; ;1)
	vCount:=vCount+1
Until(vFilename="")
Return(vCount)
ENDFUNC
//************************************************************

          

In this example, the filenames are assigned to an array variable, and can be used multiple times throughout the macro. For this particular macro, we just type the names out on the screen, but you can add any processing you need instead.

 

Next month: Some of the new commands in WPWin 8

For more information on this topic, get a copy of my book.
My WPWin 8 macro book should be out some time in mid-October.

For information on ordering my book see my web page.

Copyright Notice: The information included in the PerfectScript Journal is protected by US Copyright. The author grants you the right to use the routines in your own macros as needed. You may not sell, distribute, or publish them in any form.
If you choose to use the information here, you do so entirely at your own risk. No representations are made regarding the fitness of this information for your   particular purpose, or for your ability or inability to use the information. You   are advised to make backups of all relevant files before implementing any suggestion  or technique.
© Copyright 1998 by J. Jeppson.