0

I am getting better at scripting in powershell, but I am still a novice and I am having problems finding a solution to this one..

I have a group of textfiles (without ending) exported from a financial system in an "in" folder. The lines of the content have always the same structure and lenght.

At character positions 72-77 of every line is a date in the format YYMMDD. I need to replace this date with a fixed value: YY = actual year, MM = actual month, DD = 01 but only if the original date is from the past month. Then I have to write all modified files to an "out" folder.

How the data will look:

00201079980000000000000000007719308672900000286850040040014092914092914093000923000100000000000000

I need to replace "140930" (character position 72-77) because the month september (09) has passed already with "141001".

1
  • have a look at the .substring() method of the string object, it should allow you to get the data you need. after that just compare it to the reference date and if you want replace it
    – Paul
    Oct 27, 2014 at 15:35

2 Answers 2

0

There definitely ways to do this in PowerShell, and while I (and many others) could assist you in writing a script to do that what I would suggest you do it download the PowerShell Community Extensions. It now (as of Oct 24th 2014) has a cmdlet included named Edit-File that does exactly what you are looking for. This could be as simple as:

get-childitem c:\path\to\files\*.txt |
    Where{(Get-Content $_.FullName -TotalCount 1).substring(73,2) -lt (get-date -f "MM")}|
        Edit-File -pattern '(?s)((?<=^.{71})\d{6})' -replacement (Get-date -f "yyMM01") -force
0

Thank you @Paul and @TheMadTechnician. I did have a look at .substring() and came up with this, it works fine. Now I have one more question: Always the last line of every file should not be processed, how do I do this the easiest way?

$PathIn = "C:\in\"
$PathOut = "C:\out\"

Get-ChildItem $PathIn * -recurse |
    Foreach-Object {
        $file = ($_ | Get-Content)
        
        write-host $_
        $CompletePathOut = $PathOut + $_
        $LineNumber = 1
        
        foreach($line in $file)
        {
            $LineNumber++
            $OrigValutaDate = $line.Substring(71,6)
            $Before = $line.Substring(0,71)
            $After = $line.Substring(77)
                        
            $d = Get-Date
            
            if ($OrigValutaDate.Substring(2,2) -ne ($d.Month).ToString())
                {
                $NewValutaDate = ($d.Year).ToString().Substring(2,2) + ($d.Month).ToString() + "01"
                $out = $Before + $NewValutaDate + $After
                }
            else
                {
                $out = $Before + $OrigValutaDate + $After
                }
                
            if ($out -eq $line) {write-host $LineNumber "Valutadate original"} else {write-host $LineNumber "Valutadate replaced"}
            $out | out-file -filepath $CompletePathOut -append 
            
        }
        
    }

1
  • Always the last line of every file should not be processed, how do I do this the easiest way in script above?
    – marquiiis
    Nov 3, 2014 at 16:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.