tumblr tracker

ForEach ... In

Parent Previous Next

ForEach ... In keyword


Iterates through each item in an array executing the associated commands.


ForEach ($variable In $array) {

//commands to execute

}


The brackets () enclosing the loop condition and the curly brackets {} enclosing the commands to execute are required. The curly bracket can also start on a line of its own.



Example


//Search for all Html files in C:\Program Files\Moscripto\Samples\ModifyFileContent
$MyHtmlFiles = Folder.FindFiles("C:\Program Files\Moscripto\Samples\ModifyFileContent", "*.html");

//Loop through each file
ForEach ($HtmlFile In $MyHtmlFiles)
{
   //load the file content in a variable
   $HtmlContent = File.ReadAnsi($HtmlFile);
   
   //modify the content in a new variable
   $HtmlModified = String.Replace($HtmlContent, "oldsite.com", "newsite.com");
   
   File.WriteAnsi($HtmlFile, $HtmlModified);
}