Quantcast
Channel: PowerShell General
Viewing all articles
Browse latest Browse all 10624

Suggestions for improvement/A little help

$
0
0

Hi,

 

First time posting, so let me know if you need any more information. I haven't been using PS for long, so bear with me. I have this code, which repeats 12 times in a script for different match patterns for file names:

 

Get-ChildItem C:\Users\$env:username\Desktop\$mydir | 

Where-Object {

             $_.Name -match "^[^8]*_101_.*\.txt" -AND $_.length -gt 0

             } | 

Foreach-Object {

                $dbn = $_.Name.Substring(0,6)

                Get-Content -Path C:\Users\$env:username\Desktop\$mydir\$_ | 

                select -skip 1 | 

                        Foreach-Object {

                                        "$_`t${dbn}" |

                                        Out-File C:\Users\$env:username\Desktop\$mydir\101Master.txt -Append

                                        } 

                }

 

$f = Get-ChildItem C:\Users\edarling\Desktop\Test\ | 

Where-Object {

             $_.Name -match "^[^8]*_101_.*\.txt" -AND $_.length -gt 0

             } | Select-Object -First 1 

 

$b = Get-Content C:\Users\$env:username\Desktop\$mydir\$f -totalcount 1

 

$a = (Get-Content C:\Users\$env:username\Desktop\$mydir\101Master.txt) |

Foreach-Object {

                $_ -replace $b, ""

               } | ? 

                  {

                   $_.trim() -ne "" 

                  }

 

$b = $b -replace '&','_amp'

$b = $b -replace '\$',"SS"

$b = $b -replace "[^a-zA-Z0-9`$`t]",""

$b = $b -replace "^\d",""

$b = $b -replace "`t\d","`t"

$b = $b -replace "\W\d+","`t"

$b = $b + '`tdbn'

 

Set-Content C:\Users\$env:username\Desktop\$mydir\Headers\Master101headers_cleaned.txt -value $b

Set-Content C:\Users\$env:username\Desktop\$mydir\Cleaned\Master101_cleaned.txt -value $b, $a

 

Can any of this be done better? It feels clunky to me, but I'm not experienced enough with PS to see where I could cut down on code.

 


Viewing all articles
Browse latest Browse all 10624

Latest Images

Trending Articles



Latest Images