I have 3 folders with some files and 3 extensions inside. I want to count the total of the files for each folder, then I want to write the output to an XML file. I tried this, but I can only write the output to an XML file for one folder.
$FindFolder = Get-ChildItem -Directory "D:\"
foreach ($folder in $FindFolder) {
$jpg = Get-ChildItem -Name "D:\$folder" -Recurse -File -Include *.jpg |
Measure-Object |
ForEach-Object {$_.Count}
$png = Get-ChildItem -Name "D:\$folder" -Recurse -File -Include *.png |
Measure-Object |
ForEach-Object {$_.Count}
$gif = Get-ChildItem -Name "D:\$folder" -Recurse -File -Include *.gif |
Measure-Object |
ForEach-Object {$_.Count}
# Write to xml file
$Output = @"
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="Tool.css"?>
<report>
<heading>Final Report</heading>
<foldername>$folder</foldername>
<jpg>$jpg</jpg>
<png>$png</png>
<gif>$gif</gif>
</report>
"@
$Output | Out-File "D:\Reports.xml" -NoNewline -Force
}
My output with this script above is:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="Tool.css"?>
<report>
<heading>Final Report</heading>
<foldername>folder1</foldername>
<jpg>11</jpg>
<png>11</png>
<gif>0</gif>
</report>
The expected output is:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="Tool.css"?>
<report>
<heading>Final Report</heading>
<foldername>folder1</foldername>
<jpg>11</jpg>
<png>11</png>
<gif>0</gif>
<foldername>folder2</foldername>
<jpg>1</jpg>
<png>18</png>
<gif>9</gif>
<foldername>folder3</foldername>
<jpg>16</jpg>
<png>13</png>
<gif>1</gif>
</report>
The total of <foldername>NN</foldername> is depend on how many folder I have.