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”