A PowerShell script to find instances of unbalanced quote characters in source code. This script flags all lines with an odd number of quote characters.
Instances of unbalanced quotes can be difficult to find. For example, PowerShell can flag the last quote character in a script as a terminating error with the message: The string is missing the terminator: ".
But clearly the line is fine. The missing quote is actually earlier in the code. It can be more than 1,000 lines earlier, where it is not flagged. This script can save a lot of time in these cases.
A few quoted strings can continue on the next line, or even several lines later. The first and last lines will be flagged by this script. But these cases are easily recognized as valid. The script can be run on any source code to find unbalanced quotes, including PowerShell, VBScript, Visual Basic, HTML, and PHP. It can also be run on text files.
A batch file can be used to launch the script and pass 1 optional argument, the name of the file with the source code. The batch file, named cq.bat, would be similar to below, assuming the script file is in a directory called MyFolder.:
@echo off
Rem cq.bat
PowerShell c:\MyFolder\ChkQuotes.ps1 %1
For example, the batch file can be run to check the source code in a file named MyCode.ps1 in the current directory as follows:
cq MyCode.ps1
The output report will be named MyCode.rpt and saved in the current directory. The output file can be similar to below.
------------------------------------------------
PowerShell script: ChkQuotes.ps1
Check source code for unbalanced quotes
Version 1.0 - May 1, 2024
Copyright (c) 2024 - Richard L. Mueller
Check file MyCode.ps1 for unbalanced quotes
Started: 5/1/2024 12:32:44 PM
Report file: MyCode.rpt
------------------------------------------------
Line 1738 has unbalanced quotes
$School = Read-Host "Enter a response by number from this list:
Line 1744 has unbalanced quotes
"
In this case lines 1738 through 1744 of the source code are below.
$School = Read-Host "Enter a response by number from this list:
1: Kansas University
2: Michigan State
3: University of Wiconsin
4: Stanford
5: University of Illinois
"
This not an error. The lines are correct because the quoted string spans the lines. If no unbalanced quotes are found, the output file will be similar to below.
------------------------------------------------
PowerShell script: ChkQuotes.ps1
Check source code for unbalanced quotes
Version 1.0 - May 1, 2024
Copyright (c) 2024 - Richard L. Mueller
Check file frmMain.frm for unbalanced quotes
Started: 5/1/2024 9:56:28 AM
Report file: frmMain.rpt
------------------------------------------------
No unbalanced quote characters found.
To run the script on code in another directory specify the full path to the source code, as in this example:
cq c:\TestFolder\TestCode.htm
The output report in this case will be named TestCode.rpt and saved in the directory with the source code.
ChkQuotes.txt <<-- Click here to view or download the program