Reputation: 148
I am using powershell script to check and see which java file in the directory is the main java file by checking the contents of each file and seeing if it contains "main". The problem is, when the script hits the Contains() method line, it returns false, even when I can see, when debugging, that the contents of my $javafilecontents clearly has the phrase "main". What am I missing, or how to I get Contains to return true for this case?
Just to test thoroughly, I also tried other small key words that should be in the file such as $javafilecontents.Contains("import"), which is literally the very first word in the file, but it still returns false
I found a similar situation here: Why is string.contains() returning false?
I assume this might have something to do with the string being too long. In their advice, they say to add the @ symbol in front of the long string. I haven't tested this yet because I'm not sure how I would do that since my long string is a variable set from Get-Content
Code:
foreach ($userjavafile in $userjavafiles)
{
$javafilepath = "" + $destination + "\" + $userjavafile
$javafilecontents = Get-Content -Path $javafilepath
$mybool = ($javafilecontents.Contains("main"))
if ($mybool)
{
$mainjavafile = $userjavafile
}
$spaceseparatedjavafiles += "" + $userjavafile + " "
}
$destination is the path to the files that I created earlier in the code.
$javafilecontents has the contents of a .java file which includes the line "public static void main(String[] args){"
These two are tested to be correct since I can see in the debugger that the contents are correctly placed into the variable.
$mybool stays false, but the way I thought I understand Contains() it should be true
Upvotes: 1
Views: 4394
Reputation: 25021
Get-Content -Path $javafilepath
produces an array of strings where each line of the file is an array element. You will have issues using string method Contains()
as PowerShell is actually using the Contains()
method of the Array class. For the Array class Contains()
method to return true
, you would have to match an entire line.
Get-Content -Path $javafilepath -Raw
will produce better results because it produces a single string. Then the string class Contains()
method will allow for a substring match. Keep in mind the string class method is case-sensitive.
$javafilecontents = Get-Content -Path $javafilepath -Raw
$mybool = $javafilecontents.Contains("main")
The other option is to continue without the -Raw
switch and loop through each line. Then you can use the string class Contains()
against each line, which will be a single string. The where()
method can filter an array based on a condition. Then you can cast the success of that filter to [bool]
. A match will yield True
and no match will yield False
.
$javafilecontents = Get-Content -Path $javafilepath
$mybool = [bool]($javafilecontents.where{$_.Contains("main")})
The same approach as the above method can be used with the case-insensitive -match
operator, which produces a cleaner solution. -cmatch
is the case-sensitive version.
$javafilecontents = Get-Content -Path $javafilepath
$mybool = [bool]($javafilecontents -match 'main')
Upvotes: 2