I was evaluating what I thought was a string read from a file, then doing an .indexOf on that string. Took me over an hour to debug something so simple.

Trying to get the position of a string within a larger string is pretty basic stuff; yet I was getting the mystery result of -1.
I then printed the file to console, copied into a separate program as a string, and it worked fine.

I was also puzzling why the .LastIndexOf variable didn’t appear in the auto-complete of ISE. That should have been my clue.

When I did the following:

$templateContent = Get-Content $inputTemplate 


this creates an array in $templateContent, and arrays have a similar method called .IndexOf.

The solution was to add the “-raw tag”. I had tried “-encoding ASCII” and various encodings, and that not getting me anywhere.

$templateContent = Get-Content $inputTemplate -raw

Then the code to do the .IndexOf was straight forward. At first, I thought my issue was within the quotes in quotes, so I simplified to something like basic like the word “xml”.

               $valueCustId = "Value=`"%CUSTID%`""; 
               $valueCustId = "xml"; 
               $posValueCustId = $updatedTemplateContent.IndexOf($valueCustId) 
               Write-Host "posValueCustId = $posValueCustId" 

https://stackoverflow.com/questions/49333243/powershell-indexof-is-not-working

Have you ever wanted to make a new Visual Studio solution (even a BizTalk one) as a copy of another, except just change the program names? And it would automatically rename everything, including the solutions, project files, assembly files and so on? I’ve needed this program for years, and finally wrote it!

Remember that the project name is often the Assembly name and the Default Namespace (in the AssemblInfo.cs file). This program renames the files, the folders, and updates the .sln and .csproj/.btproj files (and any other file). The “-exclude” parm on the Get-ChildItem is used to avoid binaries and other files that you do not want to change.

In BizTalk, the project name can also become your “TargetNamespace” for schemas.
Another trick with BizTalk is that some of the files are Unicode and some aren’t. So the rename program has to be conscious of that, and preserve the file type. I’m doing this by testing if the first tow characters of a file are are Byte Order Mark.

NOTE: Use with caution. Make a backup of your project before letting any program rip through and rename and change files!
Make sure your old/string new string are quite unique, so as not to change other random parts of your code. For example, if you use a 20-40 character project name like I have in $oldstring below, you are generally safe.

cls 
$path = "d:\Git_Dev\MyProject" 
cd $path 
$oldstring = "MyCompany.BizTalk.System.OriginalName"
$newstring = "MyCompany.BizTalk.System.NewName"

$files = Get-ChildItem -Path $path *.* -rec -file -exclude obj,debug,bin,*.msi,*.exe,*.dll,*.snk 

$loopCounter = 1 

foreach ($file in $files) 
{
    ##(-join [char[]](Get-Content $file.PSPath -Encoding Byte -TotalCount 2)) -eq 'ÿþ'
    Write-Host "FileName: $($file.Fullname)" 
    $fileContents = Get-Content $file.FullName 
    $fileContentsUpdated = $fileContents -replace $oldstring, $newstring 

    if ($filecontents -ne $null) 
    {
        #Write-Host "---BEFORE----" 
        #Write-Host $fileContents 
        #Write-Host "---AFTER----" 
        #Write-Host $fileContentsUpdated 

        if ($fileContents -ne $fileContentsUpdated) 
        {
            if ($fileContents[0..2] -eq 'ÿþ') 
            {
                Write-Host "Updating File $($file.Name) [Unicode] "
                Set-Content -path $file.FullName -value $fileContentsUpdated -Encoding Unicode 
            }
            else 
            {
                Write-Host "Updating File $($file.Name) [NOT-Unicode] "
                Set-Content -path $file.FullName -value $fileContentsUpdated ## not Unicode 
            }
        }

        $fileNameUpdate = $file.Name -replace $oldstring, $newstring 
        if ($file.Name -ne $fileNameUpdate) 
        {
           Write-Host "Rename $($file.FullName) to $fileNameUpdate" 
           Rename-Item -Path $file.FullName -NewName $fileNameUpdate 
        }

        $loopCounter = $loopCounter + 1 
        if ($loopCounter -gt 2) 
          {
             #exit 
          }

   }
}

// Repeat again, to rename folders 
$files = Get-ChildItem -Path $path *.* -rec -directory -exclude obj,debug,bin

foreach ($file in $files) 
{


        $fileNameUpdate = $file.Name -replace $oldstring, $newstring 
        if ($file.Name -ne $fileNameUpdate) 
        {
           Write-Host "Rename $($file.FullName) to $fileNameUpdate" 
           Rename-Item -Path $file.FullName -NewName $fileNameUpdate 
        }

}