For each loop file on file iterate twice (not the same as the other post)

Hi again,

T’m getting an issue that’s getting me crazy. I get all the files in a folder, iterate over it, and still I’m messing with all the lines, trying on other computers, replacing the code with test code, and still it does it twice.

If someone can help, thanks.

Here is the code, in the folder, there are 28 files, and in the output, c value is 56:

Function get-folder()

Function to open a dialog to select folder

{

[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms")|Out-Null

$foldername = New-Object System.Windows.Forms.FolderBrowserDialog

$foldername.Description = "Select a folder"

$foldername.rootfolder = "MyComputer"

if($foldername.ShowDialog() -eq "OK")

{

    $folder += $foldername.SelectedPath

}

return $folder

}

Function get-files($initialdirectory, $match_string)

Function to get the root folder and a string to filter the file names, returns a list

{

$files = Get-ChildItem -Recurse $initialdirectory | Where-Object { ! $_.PSIsContainer } | Select-Object Name, FullName

$match_list = @()

Write-Output $files

foreach($file in $files){

    if($file.Name -match $match_string){

        $match_list+=$file

    }

}

return $match_list

}

Function get-dates($file, $regex)

Function to get a date from file with a format

{

$date = Get-Content $file.FullName -First 1

$null = $date -match $regex

$date = $Matches[0]

return $date

}

Function write-file($filetxt, $split_num, $output_file, $machine){

Add-Content -Path $output_file -value $content

foreach($line in Get-Content $filetxt.FullName){

    # replace the dots by commas

    $line = $line -replace '.', ","

    ## format it to get semicolon separated values

    $line = $line -replace '\s\s+', ";"

    # split the line and take only the desired column

    $content = $line.Split(";")[$split_num]

    # add the date and the machine to the line

    $content = $date+";"+$machine+";"+$content

    # add to the file

    Add-Content -Path $output_file -value $content

}

}

Get the root folder of the machines

$root_folder = get-folder

$sw = [Diagnostics.Stopwatch]::StartNew()

create the final folder

$final_folder = $root_folder+“\FINAL”

if(-not(test-path $final_folder)){mkdir $final_folder}

iterate over the root_folder subfolders

$folders = Get-ChildItem $root_folder -Directory

foreach($folder in $folders){

$files = get-files $folder.FullName "sa"

if(($files | Measure-Object).Count -gt 0){

    $c = 0

    foreach($file in $files){

        $c++

    }

    write-output $c

}

}

$sw.Stop()

Write-Output "Tiempo total: “$sw.Elapsed " ms”

I think you are overcomplicating things. Try to keep it as simple as possible.

Function Get-Folder() {
    [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null
    $foldername = New-Object System.Windows.Forms.FolderBrowserDialog
    $foldername.Description = "Select a folder"
    $foldername.rootfolder = "MyComputer"
    if ($foldername.ShowDialog() -eq "OK") {
        $foldername.SelectedPath
    }
}
Function Get-Files($initialdirectory, $match_string) {
    Get-ChildItem -Recurse $initialdirectory -File |
    Where-Object -Property Name -Match -Value $match_string
}

$root_folder = Get-Folder
$sw = [Diagnostics.Stopwatch]::StartNew()
$final_folder = $root_folder + "\FINAL"
if (-not(test-path $final_folder)) { mkdir $final_folder }

$folderList = Get-ChildItem $root_folder -Directory -Exclude 'FINAL'
foreach ($folder in $folderList) {
    $fileList = get-files $folder.FullName 'sa'
    [PSCustomObject]@{
        Folder    = $folder
        FileCount = $fileList.count
    }
}
$sw.Stop()
Write-Output "Tiempo total: $($sw.elapsed.TotalMilliseconds)  ms"

The code is a bit over-complicated with many non-required loops. Here is a basic example to get files matching a pattern ‘fi’ under C:\Scripts and get a count of files:

$initialdirectory = 'C:\Scripts'
$pattern = 'fi'

$files = Get-ChildItem -Path $initialdirectory -Recurse -File | 
         Where-Object { $_.Name -match $pattern } | 
         Select-Object Name, FullName

$files.Count

Output:

PS C:\Users\rasim> $files

Name                 FullName                       
----                 --------                       
AsBuiltReport.config C:\Scripts\AsBuiltReport.config
file1.txt            C:\Scripts\file1.txt           
file2.txt            C:\Scripts\file2.txt           


PS C:\Users\rasim> $files.Count
3

Hi,

Thanks for the replies, Olaf code worked fine, the goal of the code is not simply count files, is more like read content from files, split it into smaller files, and putting some of the values together.

But it works with Olaf reply, thank you very much :smiley: