Really appreciate your help on this as I already stuck almost a month figuring out the solution. I am creating some tools to count a codes in text files in the folder. The codes will be match with a Master Code list in another text file and the output result will be the number of codes in the files.
Example : code in file A.txt
E15
E15
E15
E22
Master code.txt
E15
E16
E22
E24
The result will be :
E15 : 3
E16 : 0
E22 : 1
E24 : 0
I am able go extract the code in file A.txt. However, I could not find any way to match and count the code list against the Master Code list.
Thank you, it was due to the codes in Codelist.txt file contains spaces after which probably detected as not match. I get the desired result after removing the spaces. Thanks a lot! another thing Olaf, is there any way the code not in the Codelist.txt to be displayed on the same result?
If I understood you right you’d need another logic to get what you need. If you just like to get the results displayed on the console you could count on the output optimization of the Powershell console and add the following code after the first snippet:
Yes u are right, I just realized… Should the code did not exist in the CodeList.txt, it need to be count as well. Thanks for the code, but I am not sure where can i add the code as for the current result i already add it in a variable as follows :
Because I don’t know what you like to do with the results I don’t know what to recommend for you. If you like it just outputted to console you could simply run both snippets after each other:
Thanks, my objective is to count the code in a.txt file with the CodeList.txt and produce the result in results.txt file. If the code in a.txt file did not exist in CodeList.txt, the program will still count it and produce the output result.txt… is it possible?
Of course you can export the results of both snippets to a single text file. But don’t you think a CSV file is more suitable? Both options would need a parameter -Append to the cmdlet you use … either Out-File or Export-Csv .
Yes. Because it does not have an according code in the code list. If you just want to count the occurences of the codes in one file you can get this easier.