#target indesign
Main();
function Main() {
// the following 4 lines declare variables
// usually I declare all vars at the beginning of the function
// when you declare a variable with var, you limit its scope to the function
// (making them local),
// otherwise the scope is global which may cause you problems
// a rule-of-thumb is to use global vars only you really need them
var i, link, newFile, file2,
doc = app.activeDocument, // set variable doc to the front most document
links = doc.links, // set variable links to collection of all links
newPath = 'c:/'; // path to the folder
for (i = links.length-1; i >= 0; i--) { // loop through all links backwards
// from the last to the first item
// it's very important when you modify or remove items from collection
// to process them back to front, never use in such cases direct loop like so:
// for (i = 0; i < links.length; i++) {
link = links[i]; // set variable link to the current link
file2='name.indd';
newFile = new File(newPath + file2); // create file object
if (newFile.exists) link.relink(newFile); // relink only if the file exists
try { // if memory serves me right, unlike CS3,
// CS4 doesn't need update() command after using relink()
// if you use it , it would cause an error
// I use try-catch block to make the script compatible with CS3 and above
// In other words, if an error occurs here, skip it over and go on
link.update();
}
catch(err) {}
}
}