The format of the command line for rebuilding an index is: 
rebuild in-file[,out-file]
[-c] [-d] [-e] [-i] [-k] [-n] [-p] [-s] [-v]
The out-file parameter is optional, but recommended, because if you do not specify it, and the Rebuild operation fails, you will lose your original file.
There are a number of reasons why an indexed file becomes corrupt, for example:
- A close operation on an indexed file fails.
- A power surge occurs while the indexed file is open.
- The machine is rebooted or powered off while an indexed file is open.
When a COBOL program attempts to open a corrupt indexed file, the run-time system detects that the file is corrupt and returns an extended file status code.
Rebuild can recover corrupt indexed files for you:
- If the data portion of the file is intact and only the index portion is corrupt, then Rebuild can generate a new index (.idx) file from the data file with no loss of data.
- If the data portion of the file is corrupt then Rebuild can rebuild a complete new indexed file, both data and index portions, but there will be loss of data as Rebuild discards any corrupt data records that it encounters.
Note: When you use Rebuild to generate a new index file from an existing data file, the file can grow in size. One reason is that information held in the original index about free space in the data file is lost, which means that the free space cannot be reused. Another reason is that if you specify the -d option, index records are created individually as with normal I/O operations, leading to index nodes that are only partially filled. To overcome this problem, you should reorganize the file after you have rebuilt the index.
 
In the following example Rebuild reads the .idx file to get the key information, and creates a new index file, by reading the data file, infile.dat:
rebuild infile.dat
In the following example Rebuild reads the input file to get the key information, and creates a new indexed file, by working through the data in physical order, skipping over duplicate records:
rebuild infile.dat, outfile.dat -d -e 
If the index file is not present, you must supply information about the key structure using the -k option.